Search Unity

  1. Unity 2019.1 beta is now available.
    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. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    1,107
    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:
    1,107
    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:
    523
    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.