Search Unity

Custom editor show static fields in inspector - also editable

Discussion in 'Immediate Mode GUI (IMGUI)' started by eses, Sep 9, 2018.

  1. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    hi,

    This might be a bit stupid question. I don't have that much experience with editor scripting - neither with reflection.

    I thought that I could show some common values like int, float, string that are static in inspector for some of my classes.

    With help of reflection I can get something like this (static fields in light grey box):



    When you have non-static fields, you can get them from target. Like this:
    Code (CSharp):
    1. target.health
    But for static fields that are in class, not in class instance, it would be in C# terms something like this:
    Code (CSharp):
    1. MyClass.someStaticField
    It is possible to show this value, but could it be possible to make it editable?
    With class instance specific fields, it would be like this (from manual):
    Code (CSharp):
    1. var myTarget = target as MyClass;
    2. myTarget.experience = EditorGUILayout.IntField("Experience", myTarget.experience);
    But I don't have target as I'd be working with class itself.

    I think I can put the value back with reflection, but how would I do it so the static field would be updated... anyway this might be wasted effort, just an idea.
     
    Last edited: Sep 9, 2018
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    So after testing a little bit more, I noticed static fields don't show up in YAML files...
    which is understandable, as they are not part of class instance but class itself.

    So they don't get serialized at all, only usable run time I guess.

    Well if it is so, then at least it's possible to show the static fields if nothing else.
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    I believe the Odin Inspector asset has the ability to edit static values.

    However I wouldn't recommend it as it breaks expectations about how static fields act.