Show static variables (Inspector)

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

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.

1 Like

static stuff is never serialized, thus it does not get displayed. Also its not like the static variables differ per component instance.

1 Like

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.

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?

1 Like

You could bundle your static values into a separate MonoBehaviour. Create one instance and reference to it via static variables.

1 Like

would just keep them static and write a custom editor window to display the values

1 Like

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.)

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.

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.

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

2 Likes