我最近做了一个自定义检查器,我刚刚意识到在检查器中编辑变量时不会调用我的OnValidate()。关于如何在保留我使用的自定义检查器的同时如何再次调用OnValidate()的任何想法?

最佳答案

答案是在序列化和propertyfield中。

以我的代码为例,此处的第一部分仅显示在我的主脚本中已声明了此内容。现在记住,公共(public)变量已经被序列化了,所以不需要放那个。

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

关于c# - Unity-使用自定义检查器时是否可以访问OnValidate()?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32575754/

10-11 07:26