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

}

则不会出现上述错误。

时间: 2024-11-13 09:46:54

unity, SerializedObject.FindProperty不要写在Editor的OnEnable里,要写在OnInspectorGUI里的相关文章

当写listview的onItemClick的方法时写Toast的参数context写成this出现can&#39;t resolve method ’make text(OnClickListener,java.lang.String,int)&#39;的错误,原因

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Fruit fruit = fruitList.get(position); //Toast.makeText(this,fruit.getName(),Toast.L

每天写的叫工作日志,每周写的总结叫周报,每月写的叫月报

有些时候,老板会突发让您求每天都要写工作周报,什么项目什么任务,完成情况,完成花费的时间等,然后汇总部门周报:也不是写不出,只是不知道有时候重复做一个项目,到底每天有什么好写?不知道大家公司是否写周报,是怎么写的怎么管理的,一起分享一下吧? 以下是达人的一些见解: A: 解释一下:每天写的叫工作日志,每周写的总结叫周报,每月写的叫月报: 针对每天的工作日志,可以简单记录以下你每天工作开展情况,例如今天处理什么事情,落实什么事项,领导安排你开展什么工作,安排别人做什么事项等等:第2天记录可以依据第

代写C语言,C语言代写,C语言作业代写,C语言编程代写

代写C语言,C语言代写,C语言作业代写,C语言编程代写 我是一线IT企业程序员,目前接各种代码代写业务: 代写C语言.代做C语言.C语言作业代写.C语言代写 代写C++.代做C++.C++作业代写.C++作业代写 代写Python.代做Python.Python作业代写.Python作业代做 代写Java.代做Java.Java作业代写.Java作业代做 代写编程作业.代做编程.编程代写.编程代做 先写代码再给钱,不要任何定金!价钱公道,具体见图,诚信第一!(涉及图形化界面.或领域类知识如金融数

自己动手写神经网络,自己真的可以动手写神经网络嘛?

人类--上帝创造的最完美的生物,在这个星球上支配着一切.是什么让看似脆弱的人类在这个星球上拥有如此的地位?我们既没有鸟类灵活的身躯和翅膀,也没有虎豹锋利的爪牙,但与此同时,我们却比世上任何一只鸟飞得更高,比丛林里任何一只猛兽更具力量--这种一种从大脑里迸发出来的智慧之光.圣经中记载:上帝按照自己的样子创造了人类,而自从亚当和夏娃偷食了禁果,人类便消除了与上帝之间唯一的区别--智慧.没错,从这个角度说我们已经是不折不扣的"上帝".现在,时机已经到来,轮到我们来创造自己的产物,一种模拟着我

自己动手写Java大整数《4》扩展欧几里得和Mod逆

/* *我把这个大整数的系列写成了Code中的项目,见https://code.csdn.net/XUE_HAIyang/bignumber */ 之前已经完成了大整数的表示.绝对值的比较大小.取负值.加减法运算.乘法运算以及除法和余数运算.具体见我的主页前三篇博客(自己动手写Java系列 ). 这篇博客添加求大整数GCD.扩展欧几里得算法和求Mod逆的算法. 扩展欧几里得算法 说道扩展的欧几里得算法,首先我们看下简单的欧几里得算法.经典的欧几里得算法就是 计算两个整数的最大公因子的算法,所基于

代写java binary search trees|代写Java Data Structures CS作业|代写Java作业|Java 编程作业代写|Java作业代写

CS2230 Computer Science II: Data Structures Homework 7 Implementing Sets with binary search trees 30 points Goals for this assignment ? Learn about the implementation of Sets using binary search trees, both unbalanced and balanced ? Implement methods

代写Matlab编程作作、代写Engineering Modeling

代写Matlab编程作作.代写Engineering ModelingEGR 102: Introduction to Engineering Modeling(Spring 2018)EGR 102 Term Project – Flint Water CrisisBackground – The Flint water crisis is a real-world example of the challenges to engineeringmodeling. In 2011, Flint

留学生R经管统计作业代写代做、Stat/ME代写

留学生R经管统计作业代写代做.Stat/ME代写Requirements for Stat/ME 424 Class Project? This is an individual project. You cannot join forces with anyone else in the class,but (if you wish) you can collaborate with someone in your lab or research team.? The basic requir

代写Python Golf Game 作业、代写Python Golf Game 作业

Implementing the Tropical Golf Game Task You are to implement a golf game program as outlined in the following description. Use what you have created for Assignment One, making any changes or improvements Program Features: Short description: ?You are