- Home /
Override functions in editor only
From what I've found, I can replace the inspector properties in the editor using a CustomEditor script or I can simply extend a class and have the child class replace function calls.
What I'm not sure how to do, however, is combine these elements, if possible. What I'm aiming to do is provide rudimentary logic on Shader manipulation in a built environment, while providing the editor with a much more capable system, utilizing ShaderUtil (which cannot be built). Ideally, I'm looking to call the basic form of the script and have the editor-specific version override the functions and features.
As-is, I'm only finding an option to use [CustomEditor(typeof(ScriptTest))] and extend the Editor to automatically load the script and override OnInspectorGUI().
That's far too limited of a scope, however, and I really want to be able to override everything while keeping the editor-specific script in the Editor folder. Then, it can behave one way in the editor and change its behavior when built.
Is there some easy means of doing this that I'm simply missing? Or is this actually a particularly challenging task?
Edit: Specifically, ShaderUtil is letting me grab shader attribute properties not otherwise available. Because ShaderUtil is editor-only, however, not having access to that in a build would essentially mean having to guess at names of properties (such as _MainTex), to the best of my knowledge.
I tried utilizing #if UNITY_EDITOR and #if UNITY_STANDALONE properties, but standalone appears to also be valid while in the editor, so that doesn't really work, since I need one or the other and not a mix of both.
Edit again: Well, I'm a dope for forgetting about the word #else but I still find it weird that "standalone" doesn't exclude the editor.
If you want to write scripts that compile differently for the editor vs. builds, you could use preprocessor directives found here.
But, editor scripts don't compile into builds anyways.
Right, I'm aware that they don't compile into builds. That's exactly what I'm ai$$anonymous$$g for, though. I'm specifically trying to get two different levels of functionality depending on whether it's in the editor or not. For example, ShaderUtil allows for pulling up data on shaders.
That said, it looks like the preprocessor directives will work. Scripts will just have to be a bit bigger. Thank you!
Actually, wait... UNITY_STANDALONE appears to also run while in the editor. That wouldn't really work right for what I'm trying to accomplish, since I need entirely different functionality as a fallback in a build. When it's built, it would have a rudimentary version of the functionality, where the editor would be able to support everything ShaderUtil can add to it.
Answer by Spinnernicholas · Sep 30, 2014 at 07:03 PM
I usually have an editor script to simulate runtime in the editor and a runtime script to actually do the work.
And if you set the runtime script up right, you can use it in your editor script. That way you only have important code in one place.
Hmm... Okay, I'll give that a shot. Seems like I should be able to make this work out now.
Your answer