• 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 ();
        }

    }

    则不会出现上述错误。

  • 相关阅读:
    C++11 新特性之 变长參数模板
    Cocos2dx 3.6源代码编译错误:syntax error : missing &#39;)&#39; before &#39;{&#39;
    android开发中,在java中怎样使用c提供过来char*
    cocos2d-x 3.1.1 学习笔记[11] http请求 + json解析
    T420s成功加装固态硬盘(SSD)
    我对GFWed的一些自己的见解
    css样式布局中position的那些事儿
    Tokyo Tyrant(TTServer)系列(一)-介绍和安装
    Linux 文件描述符和重定向
    装饰模式
  • 原文地址:https://www.cnblogs.com/wantnon/p/5486191.html
Copyright © 2020-2023  润新知