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頁面的