- Home /
Get preprocessor symbol UNITY_EDITOR for 3rd party Library
Hey, I'm working on an custom Inspector that fully replaces the original Inspector.
I'm compiling it to a library, because i think not doing that would slow down recompilation. correct me if i'm wrong.
But I'm stuck with yet another problem: I can't use #if UNITY_EDITOR anymore. is there a way to import this symbol and make it part of a JIT compilation?
Or do I have to provide a .cs file that takes care of it outside that library. And How would I do that (a Library Handler)?
I'm confused, overwhelmed and do feel freaking stupid right now... ( next level issues I guess^^)
Answer by Bunny83 · Oct 30, 2017 at 01:04 PM
Actually there should never be a reason to use that preprocessor define at all. You generally should seperate runtime and editor code. Editor scripts (and editor assemblies) should be in an editor folder. Also keep in mind that the more recent Unity versions do have special import settings for assemblies which allow you to specify for which platform they are ment for / should be used for. The "editor" is also a platform.
It should be obvious that compiler pre-processor tags can't be used at all since they are only read by the pre-processor which pre-processes your code files before the code is even compiled. So the the actual compiler doesn't see pre processor tags at all. So of course you can't find any traces of those tags in a compiled assembly.
If for some reason you don't want to seperate editor and runtime code (though you should just like Unity itself: UnityEngine.dll and UnityEditor.dll) You would need to compile two versions of your assembly and put both in your project. The "runtime" assembly must be imported to not be used in the editor while the "editor" assembly must only be used on the editor platform. Though such a setup may work for you but i wouldn't recommend to use such an approach when you want to publish it on the asset store.
As i said it's generally a better approach to seperate runtime and editor code. Where / for what do you actually use the tags?
"seperate runtime and editor code.. for what do you actually use the tags" [ExecuteInEdit$$anonymous$$ode] $$anonymous$$onoBehaviors? eg. Object.Destroy vs Object.DestroyImmdiate aka "the bane of my existence"
Actually "ExecuteInEdit$$anonymous$$ode" is most likely misused as "editor script". ExecuteInEdit$$anonymous$$ode was never ment to be an editor script but just a way to see the effect of a runtime-script during edit time. So for example a particle effect, a camera moving / aligning script, updating some sort of animation. So the $$anonymous$$onoBehaviour shouldn't contain any editor related stuff in the first place.
As i said editor code should be seperated into it's own assembly or editor folder.
I want to agree; creating and destroying gameobjects should NOT be "editor stuff", but if I don't call UnityEditor.DestoryImmidiate, it pops up a message in the console (at least it used to, haven't tested it in a while... link text Edit: hmm that link seems to have failed... trying again