Unity3D has offered very handy interface to expose variables so that users can edit them in inspector. Take a look at below snapshot: one can define some public fields, and these fields are immediately seen in the inspector, once the script is attached tom some game object:
That is cool! But you might expect another feature: when the value changes, you get notified, and can hook your own action with it!
In C# and winform, it is trivial: SomVarTextBoxControl.TextChanged += YourAction(…)
Unfortunately, this kind of handy event handler does not exist in Unity3D! To get similar functionality, you need use custom editor!
Step 1: Create a folder called “Editor” under “Assets” folder;
Step 2: Create a new script “Assets/Editor/CustomEditorDemo.cs”;
Step 3: Modify the CustomEditorDemo.cs, the automatically generated source looks like below:
Now do below modifications:
Add using UnityEditor; line
Add the [CustomEditor(typeof(demo)] and the [CanEditMultipleObjects] attributes to the class, note the “demo” corresponds to name of the MonoBehaviour class.
Change the base class from “MonoBehavior” to “Editor”;
Replace start() function with OnEnable();
Step 4: Now, you have a chance to hook your own logic when the value of “SomeVar” is changed, you guess right! You can override the function OnInspectorGUI(). But wait, how can you associate the SomeVar value in demo class with the event handler OnInspectorGUI()? Here is how:
You first declare a SerializedProperty SomeVarProperty;
Then in the OnEnable() function, you associate the property SomeVarProperty in CustomEditorDemo class with the SomeVar value in demo class
Then in the OnInspectorGUI function, you update the SomeVarProperty.intValue, i.e. you updated SomeVar, and most importantly, you get a change to add your own action, in this case, to output the latest value n the console!
Now coming back from the code editor (e.g. MonoDevelop or Visual Studio) to unity, and when you change the value in the inspector, you can see your value changed event got handled:
To verify that CustomEditorDemo. SomeVarProperty.intValue == demo.SomeVar, add below line in the demo.Update() function, and run the Unity3D App, you will see in the console, when you change the value in the inspector.
The values are the same, i.e. CustomEditorDemo. SomeVarProperty.intValue == demo.SomeVar
Also note that demo.Update() get frequently fired, even though the value is not altered, whereas CustomEditorDemo.OnInspectorGUI() is fired only when the value changes!!!
The 3rd note is that, even in edit mode, the CustomEditorDemo.OnInspectorGUI() is also fired, indicating that you don’t have to run the unity3d App, and the value change event is properly handled at design time, not only at runtime! This is very useful when you change the value, and immediately update your scene, including graphical output, for instance, some game object’s position, rotation, color etc., this is very useful in designing your game or app, just like the visual designer of VisualStudio!