[英]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.