5
我最近做了一个自定义检查器,我刚刚意识到当我在检查器中编辑一个变量时,我的OnValidate()没有被调用。有关如何在保留我使用的自定义检查器的同时再次将我的调用返回给OnValidate()的任何想法?Unity - 使用自定义检查器时可以访问OnValidate()吗?
我最近做了一个自定义检查器,我刚刚意识到当我在检查器中编辑一个变量时,我的OnValidate()没有被调用。有关如何在保留我使用的自定义检查器的同时再次将我的调用返回给OnValidate()的任何想法?Unity - 使用自定义检查器时可以访问OnValidate()吗?
答案在序列化和属性字段中。
用我的代码的例子,这里的第一部分只是显示在我的主脚本中,我已经声明了这个。现在请记住,公共变量已经被序列化了,所以不需要这么做。
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();
}
}