Oninspectorgui



Oninspectorgui

You should not override the OnGUI method in OdinEditorWindows. Instead, if you want to inject custom GUI code, then you override either the DrawEditors method or you can use the OnInspectorGUI attribute on a custom GUI method. If you do have to override the OnGUI method for some reason, then make sure to call base.OnGUI. Hi everyone, this is my first post. I’m excited to be able to share knowledge regarding what I’ve learned while using unity. I’ve decided to write my first blog post about how to create a custom drop down field for a custom inspector. OnSceneGUI works just like OnInspectorGUI except it runs in the Scene view. To help you make your own editing controls, you can use the functions defined in the Handles class. All functions in there are designed for working in 3D Scene views.

Unity Custom Inspector

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:

ButtonOninspectorguiList

Now do below modifications:

Unity Oninspectorgui Update

  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 Update function with OnInspectorGUI()

Oninspectorgui

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:

  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:

Oninspectorgui Update

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.

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!

Oninspectorgui Dropdown

Happy coding!