x


Force inspector to use getter/setter?

I need to expose a variable on the inspector. But when I change that variable through the inspector, I need other things to happen. So I tried making a setter for that variable. But when I change that exposed variable, it gets changed directly and the inspector bypasses the setter. Is there a way to force the inspector to use the setter? Or is there another way to force things to happen once a variable gets changed in the inspector? Thanks in advance!

Cheers!

more ▼

asked Aug 02 '12 at 07:40 PM

Filippopotamus gravatar image

Filippopotamus
18 4 6 7

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I would write a custom inspector for this problem. To start off you would write your normal Monobehavior script like the following and attach it to a gameObject where you will be able to see the public property visible. But when you get/set the value the Debug.Logs inside Setter/Getter would not print.

using UnityEngine;    
using System.Collections;

public class UseGetterSetter : MonoBehaviour 
{
 // This will be displayed in the inspector as usual
 public int publicVariable;

 // public property of the variable
 public int PublicVariableProperty
 {
 get 
 { 
 Debug.Log("Getter!");
 return publicVariable; 
 }
 set 
 { 
 Debug.Log("Setter!");
 publicVariable = value; 
 }
 }
}

Now you have to create a custom inspector like such to take advantage of your getter/setter.

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(UseGetterSetter))]
public class CustomInspector : Editor {
 public override void OnInspectorGUI () 
 {
 base.OnInspectorGUI();

 // Take out this if statement to set the value using setter when ever you change it in the inspector.
 // But then it gets called a couple of times when ever inspector updates
 // By having a button, you can control when the value goes through the setter and getter, your self.
        if (GUILayout.Button("Use setters/getters"))
 {
 if(target.GetType() == typeof(UseGetterSetter))
 {
 UseGetterSetter getterSetter = (UseGetterSetter)target;
 getterSetter.PublicVariableProperty = getterSetter.publicVariable;
 Debug.Log(getterSetter.PublicVariableProperty);
 }
 }
 }
}

like I explained in the comment, you could take out the 'button' line in the inspector code (if (GUILayout.Button("Use setters/getters"))) so that what ever you do in the editor to that variable will be reflected back on it through our getter/setter. But it gets called many times. If you don't wish that. You could simply set the value and press the button when ever you want to take advantage of using your getter/setter.

more ▼

answered Aug 04 '12 at 01:09 AM

hdsenevi gravatar image

hdsenevi
329 1 3 6

Very neat! I really appreciate the help. Thanks for taking the time! :)

Aug 04 '12 at 01:17 AM Filippopotamus

No worries. Glad to be of help.

Aug 04 '12 at 01:29 AM hdsenevi

for publicVariable property in first class you need to include [SerializeField] attribute on it

Oct 10 '12 at 10:25 AM nicloay
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x467
x10
x5

asked: Aug 02 '12 at 07:40 PM

Seen: 814 times

Last Updated: Oct 10 '12 at 10:25 AM