C# Properties showing up in Unity Editor

Hi!

I am used to develop large enterprise applications and we alway pay very much attention to code structure etc.

To be able to get a good structure of large script modules, I really want to use properties instead of public variables. However, from what I understand, it is not possible to make public properties of a MonoBehaviour class show up in the Unity Editor.

Does anybody have a nice code design alternative while waiting for Unity to support class properties in its editor?

  • Can I get a hint that a public member variable changed automatically - either in editor or runtime?
  • Can I extend the Unity Editor my self to support properties?
  • Can I put things in an external dll and access its class properties that way from the Editor?
  • Am I being stupid? I really want to be able to react when any other code is changing one of the values of my class - am I missing some obvious thing?

Otherwise, I cannot use the Editor for my scripts and that works but it is a bit booring since the editor is so great.

Any suggestions?

/Ricky

m_myNumber will show in the editor inspector
but will not call set accessor when changed in the editor
the property will work as it should at runtime

    public float m_myNumber; 
    public float myNumber{
        get{return m_myNumber;}
        set {m_myNumber =value;}
    }

you can also:

    [SerializeField]
    private float m_myNumber; 
    public float myNumber{
        get{return m_myNumber;}
        set {m_myNumber =value;}
    }

if you want to call the accessors in the editor you may wan’t write a custom inspector for you class

you can :

   void OnEnable() {
     myNumber=myNumber;
  }

which will call the set accessor and apply the result of the get accessor
this is useful for initialization

as far as external dlls are concerned only
a class that inherits from System.Object
that are [System.Serializable]

[System.Serializable]
public MySubClass : System.Object  {
    public float m_myNuber; 
    public float myNuber{
        get{return m_myNuber;}
        set {m_myNuber=value;}
    }
    
    public string m_myName;
    public string myName{
        get{return m_myName;}
        set {m_myName=value;}
    }

}

i believe that if the above is compiled in a dll
it will appear in the unity editor if implemented like :

    public class MySuperClass : MonoBehaviour {
        public MySubClass m_subClass;
        public MySubClass subClass{
            get{return m_subClass;}
            set {m_subClass=value;}
        }
    }

I know that this works if you place MySubClass.cs in the Plugins folder
let unity compile the dll
and grab Library/ScriptAssemblies/Assembly - CSharp - first pass.dll
which can rename and drop into a new project

I am not sure that it shows up in the inspector if compiled with mcs

you may want to look at
UnityEditor.SerializedProperty

Thank you very much for the in-depth answer!!!

I will look into the alternatives and post my own conclusion, conserning clean code, accessibility and encapsulation.

/Ricky

@zelk / Ricky - You done looking into the alternatives yet :wink:

1 Like