- Home /
Is it possible to dynamically disable or validate properties in the inspector?
Without creating custom inspectors, is this possible? For example, I mostly will add public member variables which end up as properties in the inspector if I'm setting up a prefab or something. Is it possible to disable some of them (in the inspector) if a flag is true?
Also, can getters and setters (used for say validation of the properties or dynamic calculation) be used in the inspector? I noticed the property does not show up if a getter or setter is in the code.
thanks :)
Answer by yoyo · Mar 24, 2011 at 06:09 AM
Jesse is correct, you can't dynamically enable/disable variables without a custom editor, and you can't serialize properties.
You can however use the HideInInspector attribute on a public field to prevent it showing up in the Inspector. You can also use the SerializeField attribute to show a private field in the Inspector.
For example, in C#:
[HideInInspector]
public string DontShowMe;
[SerializeField]
private string ButDoShowMe;
or Javascript:
@HideInInspector
var DontShowMe : string;
@SerializeField
private var ButDoShowMe : string;
This is static behaviour though, not dynamic, which isn't quite what you were looking for, and nor does it enable any validation. The benefit of a custom editor is that you can dynamically decide what to edit, and do validation as appropriate.
Custom editors are not that hard to create. One easy thing you could do is allow editing of any fields or properties (you write the code, so you decide what gets edited) and then use DrawDefaultInspector to draw the default inspector, so all you need to implement is your extra bits.
Also note that if you use the little drop-down menu in the upper right of the Inspector to switch from Normal to Debug mode, all custom editors are ignored and you get a default view of all serializable properties, and also a read-only view of private members. Sometimes useful for getting a peek under the covers.
Hope that helps!
Thanks $$anonymous$$ike, edited accordingly. I based my "no private in javascript" on a quick google and a stack exchange answer, oh well.
I appreciate the clarification and the DrawDefaultInspector tip. Thanks for your answer!
Answer by Jesse Anders · Mar 23, 2011 at 10:13 PM
AFAIK:
The only direct way to enable/disable variables based on the state of other variables would be to use a custom inspector. (You might be able to rig something up using the 'execute in edit mode' attribute, but I'm not sure how effective that would be.)
Serialization of properties is currently not supported.
If any of that is wrong (or if there are options available that I'm not thinking of), I'm sure someone else will jump in and clarify.
Answer by idbrii · Mar 26, 2019 at 05:09 PM
You can use a PropertyDrawer to do some basic logic to determine whether to draw a variable (or make it readonly or whatever). See this DrawIf thread which has a solution that does exactly what you want if you pass the DisablingType.ReadOnly argument.
Your answer
Follow this Question
Related Questions
Can I expose public field with getter-setter? 3 Answers
How to display computed getter in inspector? 2 Answers
Force inspector to use getter/setter? 2 Answers
Custom Inspector Layer Mask variable 2 Answers
expose public property (not variable) in inspector 9 Answers