Unity - 当使用自定义检视面板时,是否可以访问OnValidate()方法?

9

我最近制作了一个自定义检视面板,但我发现当我在检视面板中编辑变量时,我的 OnValidate() 回调函数没有被调用。你有什么想法如何保留我使用的自定义检视面板并使我的 OnValidate() 回调函数能够被再次调用吗?

1个回答

2
答案在序列化和属性字段中。
以我的代码为例,这里的第一部分只是展示我在我的主脚本中声明了这个。现在记住,公共变量已经被序列化了,所以不需要再进行操作。
public class Original : MonoBehaviour {

// Used for the user to input their board section width and height.
[Tooltip("The desired Camera Width.")]
public float cameraWidth;
}

现在在我的自定义检视面板中,我有这个:

    pubilc class Original_Editor : Editor{
         public override void OnInspectorGUI(){
              serializedObject.Update();
              // Get the camera width.
              SerializedProperty width = serializedObject.FindProperty("cameraWidth");
              // Set the layout.
              EditorGUILayout.PropertyField(width);
              // Clamp the desired values
              width.floatValue = Mathf.Clamp((int)width.floatValue, 0, 9999);
              // apply
              serializedObject.ApplyModifiedProperties();
         }
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接