C# 为什么在编辑器窗口I';m获取异常InvalidOperationException:堆栈为空。?

C# 为什么在编辑器窗口I';m获取异常InvalidOperationException:堆栈为空。?,c#,unity3d,C#,Unity3d,我试图做的是在将editorwindow扩展到底部太多时添加自动垂直滚动: 例如,如果在屏幕截图中,这是原始编辑器窗口大小: 例如,当单击对话并将其折叠时,我希望使用垂直滚动进行编辑: 我可以用鼠标继续向下拉伸窗口,但在这种情况下我想使用滚动: InvalidOperationException:堆栈为空。 System.Collections.Stack.Peek()(位于:0) UnityEngine.GUI.EndScrollView(System.Boolean handleScro

我试图做的是在将editorwindow扩展到底部太多时添加自动垂直滚动:

例如,如果在屏幕截图中,这是原始编辑器窗口大小:

例如,当单击对话并将其折叠时,我希望使用垂直滚动进行编辑:

我可以用鼠标继续向下拉伸窗口,但在这种情况下我想使用滚动:

InvalidOperationException:堆栈为空。 System.Collections.Stack.Peek()(位于:0) UnityEngine.GUI.EndScrollView(System.Boolean handleScrollWheel)(位于C:/buildslave/unity/build/Modules/IMGUI/GUI.cs:1481) UnityEngine.GUILayout.EndScrollView(System.Boolean handleScrollWheel)(位于C:/buildslave/unity/build/Modules/IMGUI/GUILayout.cs:387) UnityEditor.EditorGUILayout.EndScrollView()(位于C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:9203) ConversationsEditorWindow.OnGUI()(位于Assets/Editor/ConversationsEditorWindow.cs:27) System.Reflection.MonMethod.Invoke(System.Object obj、System.Reflection.BindingFlags invokeAttr、System.Reflection.Binder Binder、System.Object[]参数、System.Globalization.CultureInfo区域性)(位于:0) Rethrow as TargetInvocationException:调用的目标已引发异常。 System.Reflection.MonMethod.Invoke(System.Object obj、System.Reflection.BindingFlags invokeAttr、System.Reflection.Binder Binder、System.Object[]参数、System.Globalization.CultureInfo区域性)(位于:0) System.Reflection.MethodBase.Invoke(System.Object obj,System.Object[]参数)(位于:0) UnityEditor.HostView.Invoke(System.String方法名,System.Object对象)(位于C:/buildslave/unity/build/Editor/Mono/HostView.cs:342) UnityEditor.HostView.Invoke(System.String方法名)(位于C:/buildslave/unity/build/Editor/Mono/HostView.cs:336) UnityEditor.HostView.InvokeOnGUI(UnityEngine.Rect onGUIPosition,UnityEngine.Rect viewRect)(位于C:/buildslave/unity/build/Editor/Mono/HostView.cs:310) UnityEditor.DockArea.DrawView(UnityEngine.Rect viewRect,UnityEngine.Rect dockareact,System.Boolean自定义边框,System.Boolean浮动窗口,System.Boolean isBottomTab)(位于C:/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:361) UnityEditor.DockArea.OldOnGUI()(位于C:/buildslave/unity/build/Editor/Mono/GUI/DockArea.cs:320) UnityEngine.Experimental.UIElements.IMGUIContainer.DoOnGUI(UnityEngine.Event evt,UnityEngine.Matrix4x4 worldTransform,UnityEngine.Rect clippingRect,System.Boolean isComputingLayout)(位于C:/buildslave/unity/build/Modules/UIElements/IMGUIContainer.cs:244) gui实用程序:ProcessEvent(Int32,IntPtr)


Stack empty message提供了一个线索—您正在调用:EditorGUILayout.EndScrollView();(尝试从gui堆栈中弹出)而无需事先调用BeginScrollView()

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

public class ConversationsEditorWindow : EditorWindow
{
    [MenuItem("Window/Editor Window Test")]
    static void Init()
    {
        // Get existing open window or if none, make a new one:
        ConversationsEditorWindow window = (ConversationsEditorWindow)EditorWindow.GetWindow(typeof(ConversationsEditorWindow));
    }

    void OnGUI()
    {
        GameObject sel = Selection.activeGameObject;
        ConversationTrigger targetComp = sel.GetComponent<ConversationTrigger>();

        if (targetComp != null)
        {
            EditorGUILayout.BeginVertical();
            var editor = Editor.CreateEditor(targetComp);
            var tar = editor.targets;
            editor.OnInspectorGUI();
            EditorGUILayout.EndScrollView();
            EditorGUILayout.EndVertical();
        }
    }
}
EditorGUILayout.EndScrollView();