如何在编辑器窗口中显示和修改数组?

Ili*_*hin 3 window editor unity-game-engine

我在从 UnityEngine.Editor 派生的 CustomEditor 类中有 GameObject 数组字段。我需要能够显示(绘制)并让用户能够修改该数组。

就像 Unity 的 Inspector 如何为从 ScriptableObject 派生的对象的 Serializable 字段执行此操作一样。例如在检查器中显示材料数组: .

小智 7

关于 SerializedObject 参考你的编辑器对象,然后找到任何需要的属性,绘制它,并应用修改:

public class MyEditorWindow : EditorWindow
{
    [MenuItem("Window/My Editor Window")]
    public static void ShowWindow()
    {
        GetWindow<MyEditorWindow>();
    }

    public string[] Strings = { "Larry", "Curly", "Moe" };

    void OnGUI()
    {
        // "target" can be any class derrived from ScriptableObject 
        // (could be EditorWindow, MonoBehaviour, etc)
        ScriptableObject target = this;
        SerializedObject so = new SerializedObject(target);
        SerializedProperty stringsProperty = so.FindProperty("Strings");

        EditorGUILayout.PropertyField(stringsProperty, true); // True means show children
        so.ApplyModifiedProperties(); // Remember to apply modified properties
    }
}
Run Code Online (Sandbox Code Playgroud)

原始答案在这里