【发布时间】:2018-06-13 05:12:56
【问题描述】:
我正在制作一个自定义编辑器窗口。非常简化的版本如下所示:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
namespace testDllForUnity {
[Serializable]
public class WindowTestDllForUnity : EditorWindow {
string myString = "Hello World";
private static float x = 0;
private static float y = 0;
private static int yOffset = 10;
private static float width = 100f;
private static float height = 40f;
private static List<Rect> buttonsRectList = new List<Rect>();
[MenuItem("Window/WindowTestDllForUnity")]
static void Init() {
WindowTestDllForUnity window = (WindowTestDllForUnity)EditorWindow.GetWindow(typeof(WindowTestDllForUnity));
buttonsRectList.Add(new Rect(x, y, width, height));
window.Show();
}
void OnGUI() {
GUILayout.Label(myString, EditorStyles.boldLabel);
if (GUILayout.Button("AddButton")) {
y += height + yOffset;
buttonsRectList.Add(new Rect(x, y, width, height));
}
for (int i = 0; i < buttonsRectList.Count; i++) {
if (GUI.Button(new Rect(buttonsRectList[i]), "button_" + i))
Debug.Log("button_" + i + " clicked!");
}
}
}
}
其实我的窗口比这更复杂。
我用 VisualStudio 将这段代码构建为 dll 并放入 Unity 的“Assets/Editor/”文件夹中。
它工作正常,但是...如果我在 Unity 中添加任何 C# 脚本,写任何东西并保存它 - Unity 启动他的自动编译,我的自定义窗口变为空,只是清除窗口。
我应该怎么做才能防止我的窗口被清除?是否可以要求Unity 在编译其他脚本时不要重建窗口?或者我应该每次都保存数据并在重新编译后恢复它?这不是一个坏方法吗?
【问题讨论】: