Controlling what fields are available in the Editor

I’m wanting to know if the following is possible inside Unity, and if so, how I could go about implementing it.

Right now I know Unity allows you to access things like strings, floats etc from the editor when you make the public. However, is there a way I can control which of these would appear in the editor at a given time? I tried roughing this up using an enum, but it didn’t work.

For instance say I had 3 strings, but I only wanted to have one of them to appear in the editor when it’s attached to a certain object. Then, when I attach the same script to another object, I’d chose to only have the second string appear in the editor and so on.

I know the obvious answer would be to separate these out into different scripts or just deal with the fact I’d have to have everything in the editor. But I don’t want to do either of those.

Any help would be appreciated.

If you want to do it conditionally like you described, you’ll have to extend the editor, which isn’t as hard as it might sound.

Is there no way to do it inside my script without the need to create a new window? Trying to limit the amount of extra clicks / windows that need to be used.

You don’t need to create a new window (subclassed from EditorWindow). You can create a custom inspector view (subclass from Editor). You might be able to get away with a custom property drawer.

In all honesty I’d really question why you want to do this in the first place. If scripts do different things depending on what they are attached to then you could make a strong case for them being different scripts - possibly inheriting from the same base script or implementing the same interface.

But hes probably afraid its hard to get at the values from a component script. :confused:

I’m creating an analytics script. And as opposed to having multiple scripts that each capture only one analytic, like a button press for instance, I’m wanting to create one script that can capture every analytic I want.

How I would use this is say I have a a GUI with 15 buttons and I wanted to capture how many of them got pressed and the length of time a user spent on this screen. I would attached my analytics script to each button and an empty game object to capture the time spent on the screen. With each button I would select the “Button Analytic” option and in in the editor I would fill out the pertinent information that was on display. Then to capture to the screen time length I would use the same screen but select the “Time on screen analytic” and fill out that information and so on.

I’m trying to keep this as a simple as possible so that if I work with designers or who ever wants this information, they can just do from the editor with one script. That way they don’t need to care about using the correct script and filling out loads of different things.

That seems like a pretty convoluted design to me. I’d probably go the route of making an AnalyticContainer class that inherits from MonoBehaviour and gets placed on a GameObject. It has an enum field that determines what type of analytic it’s going to track. Each analytic is its own class (not inheriting from MonoBehaviour) that gets instantiated based on what enum you select. You could serialize the analytic class so it shows up in the Inspector and allows you to set its values from there.

In either case, I personally wouldn’t try to cram it all in a single class - but to each their own. Good luck!