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.

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.

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

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.

Not exactly the answer you want, but it is possible to expose private members to the inspector window. Just use the [SerializeField] attribute.

It's not possible without a custom inspector

How can this be expanded for arrays?

You could write a custom editor window that inspects selected objects using .NET reflection to view any field or property of the objects.

/shameless plug/ I know this is possible because I’ve done it :wink: If you’d like to help beta test a new version of the tool shown here please drop me a message at yoyo(at)zeroandone(dot)ca.

I think that combining OnValidate() with SerializeField and a temp value can get the jobs done. Here an example:

private bool m_activated;

private bool _activated = true;

public bool activated
	get{ return m_activated;}
	set{ DoSomething(value);}

MyClass() or Start() or Awake()
    activated = _activated;

DoSomething(bool value)
    m_activated = _activated = SomeValue;

void OnValidate()
	if(_activated != m_activated)
		activated = _activated;

The SerializeField is only for the editor. If it’s value is not the same than the m_, that mean we need to update the property. It’s important to make sure that at the end, the m_ and the SerializeField are equal.

Here’s another solution, too: GitHub - LMNRY/SetProperty: A PropertyAttribute/PropertyDrawer combination that allows for properties in Unity