- Home /
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!
It's good to know that the inspector doesn't use them. Cause I've been scratching my head like I was doing something wrong.
Answer by $$anonymous$$ · Aug 04, 2012 at 01:09 AM
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.
Very neat! I really appreciate the help. Thanks for taking the time! :)
for publicVariable property in first class you need to include [SerializeField] attribute on it
Answer by Xorxor · Jan 04, 2016 at 02:46 PM
Try this:
[SerializeField]
private string _titleDisplayString;
public string titleDisplayString
{
get { return _titleDisplayString; }
set
{
_titleDisplayString = value;
// Now do awesome things
AndAwayWeGo(value);
}
}
This code won't do what he's asking. In fact, all it does is expose the private var, making it identical to this, inspector-wise:
[SerializeField]
private string _titleDisplayString;
This solution works if the initial value doesn't need the setter logic but code access does (or if the Awake function takes care of calling the custom setter). Rather handy for my purposes, thanks!
Your answer
Follow this Question
Related Questions
Is it possible to dynamically disable or validate properties in the inspector? 3 Answers
Can I expose public field with getter-setter? 3 Answers
Resources loading vs public linking in inspector 1 Answer
How do i NOT break encapsulation with Unity? 2 Answers
Array of abstract class in inspector 2 Answers