Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Show static variables (Inspector)

Discussion in 'Scripting' started by Shushustorm, Jul 6, 2022.

  1. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Hey everyone!

    For some cases, it would be quite useful to be able to show static variables in the Inspector. I'm building some paths at runtime and for debugging purposes, having Inspector access would be great.
    Is there a way to do this? There is [HideInInspector], but no way to explicitly show it?

    Best wishes,
    Shu
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,192
    You'd need to use reflection to do this. The addon Odin Inspector has a small built in tool that does this.

    Otherwise static values cannot be serialised.
     
    Shushustorm likes this.
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    static stuff is never serialized, thus it does not get displayed. Also its not like the static variables differ per component instance.
     
    Shushustorm likes this.
  4. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Thanks for the replies!
    @spiney199
    That's great info! I may have a look, but I do try to avoid third party content in order to get better maintainability. Maybe I'll have a look if there's an easy way to Editor script this.
    @passerbycmc
    That's unfortunate, though, I think. There are plenty of scenarios where I don't need per instance info, yet getting the info would be great, just as I mentioned when I build paths at runtime. There may be an issue with some of the paths and I can't views them directly. So I have to log them to the console. Works, of course. But just looking at the inspector would be more straight forward.
    I guess it all depends on which solution is more time effective. Doing some logging here and there isn't too much of an issue, but it would have been great if there's a built-in solution for displaying static variables.
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,113
    You can write a custom inspector for your class and add the static variables manually at the end. Though as spiney said, you may have to use reflection to actually access them unless they are public variables. So creating a custom editor for your specific class would be quite trivial. Providing a general solution is more difficult. What kind of static variables are we talking about? Are your variables public?
     
    Shushustorm likes this.
  6. irene_reko

    irene_reko

    Joined:
    Feb 14, 2019
    Posts:
    12
    You could bundle your static values into a separate MonoBehaviour. Create one instance and reference to it via static variables.
     
    Shushustorm likes this.
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    would just keep them static and write a custom editor window to display the values
     
    Shushustorm likes this.
  8. Shushustorm

    Shushustorm

    Joined:
    Jan 6, 2014
    Posts:
    1,084
    Interesting ideas! Thanks!
    However, I'd need a solution to display all static variables within the entire project (all non editor scripts) rather than continuously having to write custom editor scripts to include each and every variable. Moving the variables somewhere else would make things rather weird to work with, having to put data together that doesn't have anything to do with each other.
    I guess a custom editor script that deals with all script files and looks for static variables, drawing them at their place in the variable list, would be a solution, but may be not that straight forward, especially considering possible interference with other editor scripts (not sure if those can have custom execution order as well). Logging them to console may be the least work after all. Weird though Unity doesn't support this out of the box. (Why wouldn't it? Static variables could use different UI style if they want to make sure the user knows it's static.)
     
  9. vishnupai

    vishnupai

    Joined:
    Oct 30, 2022
    Posts:
    1
    I have this same problem but I have a solution. I think you should debug.log your static variable to see it. However, this does not work in my case as I have to see a list that is 70 items long.
     
  10. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    367
    I would make custom attribute like [DebugStatic] for fields or whole class.
    Using AppDomain or Assembly classes you can get all or current executing assembly, iterate over all types, find all static fields or all static fields with this attribute, cache expensive reflection stuff, and draw values in a window or something.
    Google how to work with reflection and attributes, google how to show and draw in EditorWindow, should be pretty easy once you understand how these things work.
     
  11. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,192
    Unity has you covered to avoid the manual domain searching:
    https://docs.unity3d.com/ScriptReference/TypeCache.GetFieldsWithAttribute.html
    https://docs.unity3d.com/ScriptReference/TypeCache.GetTypesWithAttribute.html
     
    Kurt-Dekker and Lekret like this.