Search Unity

  1. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  4. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Custom editor show static fields in inspector - also editable

Discussion in 'Extensions & OnGUI' started by eses, Sep 9, 2018.

  1. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    804
    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:
    804
    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:
    426
    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.