unity, SerializedObject.FindProperty不要写在Editor的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(){

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

     /////DrawDefaultInspector();

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

}

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

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

就会报如下错误:

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:??)

如果将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 ();
    }

}

则不会出现上述错误。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



猜您在找 Controller类的方法上的RequestMapping一定要写在Controller类里吗? Qt中QPushButton、QTabWidget等部件内容文字换行的解决方法(一定要写在源码里,写在翻译文件里不行,因为\n得不到转义) EL表达式判断条件要写在${}内 将javascript函数写在Html标签里 UWP开发之Mvvmlight实践八:为什么事件注销处理要写在OnNavigatingFrom中 axios和后端交互时,参数需要写在body和query中同时写 为什么SpringBoot项目里引入其他依赖不要写版本号 canvas画布的宽高应写在标签里 函数直接写在html页面的