Quantcast
Channel: Programming – Xinyustudio
Viewing all articles
Browse latest Browse all 284

Unity3D: inspector control value changed event handling

$
0
0

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:

image  image

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:

image 

Now do below modifications:

  1. Add using UnityEditor; line
  2. Add the [CustomEditor(typeof(demo)] and the [CanEditMultipleObjects] attributes to the class, note the “demo” corresponds to name of the MonoBehaviour class.
  3. Change the base class from “MonoBehavior” to “Editor”;
  4. Replace start() function with OnEnable();
  5. Replace Update function with OnInspectorGUI()
         image          image

 

 

 

 

 

 

 

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:

image

  1. You first declare a SerializedProperty SomeVarProperty;
  2. Then in the OnEnable() function, you associate the property SomeVarProperty in CustomEditorDemo class with the SomeVar value in demo class
  3. 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:

image image

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.

image 

Note that:

  • 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!

Happy coding!


Filed under: Programming, Unity 3d Tagged: C#, custom editor, event handler, event handling, game, inspector, notification, Unity 3d, value changed event

Viewing all articles
Browse latest Browse all 284

Trending Articles