简体   繁体   English

Unity Editor Scripting:如何以编程方式更新 CustomEditor SerializedProperty?

[英]Unity Editor Scripting: How to update a CustomEditor SerializedProperty programmatically?

The headline already says it ...标题已经说了...

I have a custom editor script for a C# class in Unity with that a user can set several parameters via UI.我有一个用于 Unity 中的 C# 类的自定义编辑器脚本,用户可以通过 UI 设置多个参数。 But there's one case where some serialized variables in the class belonging to the CustomEditor class are changed programmatically and the change is not updated in the inspector.但是有一种情况,属于 CustomEditor 类的类中的某些序列化变量以编程方式更改,并且更改未在检查器中更新。

How do I tell the CustomEditor class that it should update the changed variable?我如何告诉 CustomEditor 类它应该更新更改的变量?

Example code:示例代码:

public class Foo
{
    [SerializeField] private float value;

    public void ChangeValue()
    {
        value = 1.0f;
    }
}


[CustomEditor(typeof (Foo))]
internal class FooEditor : Editor
{
    private Foo self;
    private SerializedProperty value;

    internal void OnEnable()
    {
        self = (target as Foo);
        value = serializedObject.FindProperty("value");
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

    }
}

In Foo I want to change value vis script but the value update is not reflected in the editor UI when set via script (only when a user changes it via UI).Foo我想通过脚本更改value但是当通过脚本设置时,值更新不会反映在编辑器 UI 中(仅当用户通过 UI 更改它时)。 How can I make the change reflect also when it's updated via script?当通过脚本更新时,如何使更改也反映出来?

Use this on your override:在您的覆盖上使用它:

public override void OnInspectorGUI ()
{   
    if (GUI.changed)
    {
        value = mynewvalue;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何判断 SerializedProperty 是否是 Unity 中数组的一部分? - How to tell if a SerializedProperty is part of an array in Unity? 如何强制 Unity 的 SerializedProperty 调用 OnValidate 回调? - How to force Unity's SerializedProperty to call OnValidate callback? Unity编辑器管道脚本:如何在OnPostprocessSprites()事件中将Sprite传递给SpriteRenderer? - Unity Editor Pipeline Scripting: how to Pass Sprite to SpriteRenderer in OnPostprocessSprites() Event? Unity 编辑器脚本:如何为 select 和特定的 class 创建自定义弹出窗口 - Unity Editor Scripting: How to create a custom Popup to select a especific class SerializedProperty在Unity3D PropertyDrawers中始终为null - SerializedProperty always null with Unity3D PropertyDrawers Unity:将空白元素添加到 SerializedProperty 的数组中 - Unity: Adding a blank element to an array of SerializedProperty's CustomEditor Unity:一次选择枚举+显示数组 - CustomEditor Unity: chosing enums at once + showing array 在Unity / C#中动态创建CustomEditor类 - Dynamically creating a CustomEditor class in Unity / C# 如何在 Vuforia 中使用 Unity 脚本 - How to use Unity Scripting with Vuforia Unity重新启动后,Unity CustomEditor删除列表参数 - Unity CustomEditor deletes List parameters after Unity Restart
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM