zoukankan      html  css  js  c++  java
  • 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 ();
        }

    }

    则不会出现上述错误。

  • 相关阅读:
    Codeforces Round #518 Div. 1没翻车记
    BZOJ4310 跳蚤(后缀数组+二分答案)
    后缀数组备忘
    洛谷 P3573 [POI2014]RAJ-Rally 解题报告
    洛谷 P1503 鬼子进村 解题报告
    洛谷 P2375 [NOI2014]动物园 解题报告
    洛谷 P2797 Facer的魔法 解题报告
    【模板】三分法
    那些神奇的DP建模
    洛谷 P1136 迎接仪式 解题报告
  • 原文地址:https://www.cnblogs.com/wantnon/p/5486191.html
Copyright © 2011-2022 走看看