Joe*_*eyL 7 c# unity-game-engine
我最近做了一个自定义检查器,我刚刚意识到当我在Inspector中编辑变量时,我的OnValidate()没有被调用.关于如何在保留我使用的自定义检查器的同时再次将我的调用回到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;
}
Run Code Online (Sandbox Code Playgroud)
现在在我的自定义检查器中我有这个:
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();
}
}
Run Code Online (Sandbox Code Playgroud)