Unity3D EditorWindow一些小心得

最近在写Editor插件,遇到一个很迷知的现象,在写编辑器的时候,发现一个Item位置不对,需要在代码里面调整下,Unity编译结束后,会发现有些变量被Unity销毁了,有些变量值仍然被保留。这个问题需要关心的目的是,如果写的是一个比较大型的插件,给别人用的时候,发现一些项目资源忘记更新了,就顺手切到目录下更新,有时候会更新到一些程序脚本,再切换回Unity的时候,Unity会自动编译,这样之前做的操作可能就被自动清空掉了,所以了解EditorWindow里面什么类型的变量会在Unity编译时候被销毁,什么类型的不会被销毁,是非常有必要的。

我测试了一下,得到的一些心得。

在EditorWindow类中的基础数据类型(int,float,string,double..),unity里面序列化的对象(Vector3,Quaternion,GameObject,Transform,继承Component,继承ScriptableObject),只要窗口不被关闭,这些变量数据不会因为程序重新编译被销毁。

但是如果这些类型的变量为静态类型,每次编译后,这个静态变量就会被销毁掉。

还有就是需要编译后不被销毁的对象不能这样写:

public int ID{get;set;}


以下是测试的代码:

using UnityEditor;
using UnityEngine;
using System.Collections;

public class TestEditor:EditorWindow {

    [MenuItem("Tools/Test Window")]
    private static void Open()
    {
        EditorWindow.CreateInstance().Show();
    }
    //-------------------------------------------------------//
    private string testString;                                                      //不会销毁
    private static string testStaticString;                                         //会被销毁
    private GameObject testGameObject;                                              //不会销毁
    public static GameObject testStaticGameObject;                                  //会被销毁
    public static string testPublicStaticString;                                    //会被销毁
    private TestDataNoSerialize testNoSerializeData =new TestDataNoSerialize();     //会被销毁
    private TestDataSerialize testSerializeData = new TestDataSerialize();          //不会销毁
    private TestScriptableObject testScriptObject;                                  //不会销毁
    //------------------------------------------------------//
    private void OnGUI()
    {
        testString =EditorGUILayout.TextField("Test String", testString);
        testStaticString = EditorGUILayout.TextField("Test Static String", testStaticString);
        testPublicStaticString = EditorGUILayout.TextField("Test Public Static String", testPublicStaticString);
        testGameObject = EditorGUILayout.ObjectField("Test Game Object",testGameObject,typeof(GameObject),true) as GameObject;
        testStaticGameObject = EditorGUILayout.ObjectField("Test Static Game Object", testStaticGameObject, typeof(GameObject), true) as GameObject;
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("No Serialize Data");
        testNoSerializeData.Name =EditorGUILayout.TextField("Name", testNoSerializeData.Name);
        testNoSerializeData.ID = EditorGUILayout.IntField("ID", testNoSerializeData.ID);
        testNoSerializeData.Obj = EditorGUILayout.ObjectField("Game Object", testNoSerializeData.Obj, typeof(GameObject), true) as GameObject;
        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Serialize Data");
        testSerializeData.Name = EditorGUILayout.TextField("Name", testSerializeData.Name);
        testSerializeData.ID = EditorGUILayout.IntField("ID", testSerializeData.ID);
        testSerializeData.Obj = EditorGUILayout.ObjectField("Game Object", testSerializeData.Obj, typeof(GameObject), true) as GameObject;
        if (testScriptObject == null)
        {
            testScriptObject =ScriptableObject.CreateInstance();
            Debug.Log("Create Instance");
        }

        EditorGUILayout.Space();
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("ScriptableObject Data");
        testScriptObject.Name = EditorGUILayout.TextField("Name", testScriptObject.Name);
        testScriptObject.ID = EditorGUILayout.IntField("ID", testScriptObject.ID);
        testScriptObject.Obj = EditorGUILayout.ObjectField("Game Object", testScriptObject.Obj, typeof(GameObject), true) as GameObject;
    }
}

public class TestDataNoSerialize {
    public string Name;
    public int ID;
    
    public GameObject Obj;
}

[System.Serializable]
public class TestDataSerialize
{
    public string Name;
    public int ID                                                                   //会被销毁
    {
        get; set;
    }
    public GameObject Obj;
}

public class TestScriptableObject:ScriptableObject{
    public string Name;
    public int ID;
    public GameObject Obj;
}


你可能感兴趣的:(Unity3D)