psdev
1
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 :)
yoyo
3
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!
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.
idbrii
4
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.