unity, SerializedObject.FindProperty不要写在Editor的OnEnable里,要写在OnInspectorGUI里

若是像下面这样写:ide

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine.Assertions.Must;
[CustomEditor(typeof(xxxControl))]
public class xxxControlEditor : Editor
{
    SerializedProperty m_a;
    void OnEnable(){ui

    m_a=serializedObject.FindProperty ("m_a");
    }
    public override void OnInspectorGUI()
    {get

     /////DrawDefaultInspector();it

        serializedObject.Update ();
        EditorGUILayout.PropertyField(m_a,true);
        serializedObject.ApplyModifiedProperties ();
    }io

}class

则在其它Editor或EditorWindow脚本的中调用date

    Editor _editor=Editor.CreateEditor(xxxObj.GetComponent<xxxControl>()); call

就会报以下错误:脚本

NullReferenceException: (null)
UnityEditor.SerializedObject..ctor (UnityEngine.Object[] objs) (at C:/buildslave/unity/build/artifacts/generated/common/editor/SerializedPropertyBindings.gen.cs:72)
UnityEditor.Editor.GetSerializedObjectInternal () (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorBindings.gen.cs:151)
UnityEditor.Editor.get_serializedObject () (at C:/buildslave/unity/build/artifacts/generated/common/editor/EditorBindings.gen.cs:144)
xxxControlEditor.OnEnable () (at Assets/??/Editor/xxxControlEditor.cs:??)di

若是将SerializedObject.FindProperty从OnEnable中改到OnInspectorGUI中,即:

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine.Assertions.Must;
[CustomEditor(typeof(xxxControl))]
public class xxxControlEditor : Editor
{
    SerializedProperty m_a;
    void OnEnable(){
    }
    public override void OnInspectorGUI()
    {

     /////DrawDefaultInspector();

    m_a=serializedObject.FindProperty ("m_a");//serializedObject.FindProperty should be called in OnInspectorGUI() instead of OnEnable()

        serializedObject.Update ();
        EditorGUILayout.PropertyField(m_a,true);
        serializedObject.ApplyModifiedProperties ();
    }

}

则不会出现上述错误。

相关文章
相关标签/搜索