- Home /
expose public property (not variable) in inspector
Hi there,
looking at most of Unitys builtin component through a reflection tool ( .net reflector, monodevelops builtin ), you can see that they dont have any public member variables.
I.e., the Rigidbody component has a property "isKinematic", but there is no public variable "bool m_isKinematic".
So, my question is, how do I expose a custom property to the inspector without writing a custom Component editor? Is this even possible?
Many thanks
[UPDATE] I tried by suggestion of a Unity IRC guy the EditorGUILayout.PropertyField() method, but it seems like that it doesnt expose properties ( the way they are meant in .NET, getters and setters ), instead SerializedObject exposes hidden variables which I believe arent meant to be public.
Still looking in a dark hole.
Answer by jonas-echterhoff · Jun 07, 2010 at 11:17 AM
As Mike said, you would need a custom inspector to do this - writing one should be really simple, though. Basically, you just use EditorGUILayout.TextField (or FloatField, or whatever fits your need) on your property, and then you call DrawDefaultInspector() to draw the normal inspector for all the other properties.
Answer by mift · Jun 07, 2010 at 12:58 PM
Hi Jonas, following your answer I wrote a piece of code which does exactly what you meant.
Link to
unifycommunity
Unity wiki for those interested: Unify wiki entry
Your wiki entry doesn't specify the location of the first class (the attribute), should this be in /Assets/Editor? Unity doesn't appear to be able to find the attribute class this way.
Archived version of the page because it no longer exists https://web.archive.org/web/20111206085547/http://www.unifycommunity.com/wiki/index.php?title=Expose_properties_in_inspector
@sam_bender
The page does still exist, however the unifycommunity wiki got integrated into the Unity website itself:
http://wiki.unity3d.com/index.php?title=Expose_properties_in_inspector
I fixed the link in the answer.
Just so that everybody knows that comes by here. The first file (ExposePropertyAttribute.cs) does not belong in the editor folder, but rather in any other folder with another name. I'd change the wiki, but I need to request an account with my real name + biography.
Answer by Stormwalker · Nov 14, 2013 at 03:24 AM
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnValidate.html
I just ran across this, and I'm kind of wondering if it's new - I certainly never noticed it before, nor in any of these discussion have I seen it mentioned. While this does not strictly speaking solve the problem of exposing properties in the inspector, it does minimize the need to.
My tests so far indicate that this does indeed only fire when editor settings are changed, and more specifically settings of the script in question.
While you can not respond to exactly what was changed, or how much it was changed - this still seems a better option than excessive use of custom inspectors, as both developing them (as an engineer,) and working with them (as an artist or designer) is cumbersome for the development pipeline. (See well known headaches with multi-object editing, and undo.)
On a side note - while this does stretch the definition of the intended purpose of this call - almost anything that responds to inspector changes, and makes appropriate adjustments is arguably validation.
That's good to know ;) I always wanted more callbacks inside the editor. A lot stuff in the pipeline runs in the background without any chance of getting a notice about it.
I checked the release notes from 4.3 back to 3.4 and they didn't mentioned OnValidate anywhere, so they forgot to mention it ;)
However i think it might be quite new.
edit
Ok i just tried it in my Unity 4.0.1 version and it works, so it's there for quire a while, but i haven't seen it before in the docs. So maybe they just updated the docs ;)
Answer by Tetrad · Jun 07, 2010 at 03:42 PM
Not exactly the answer you want, but it is possible to expose private members to the inspector window. Just use the [SerializeField] attribute.