Hi all, we have some runtime scripts which fail to compile unless build target == Windows Store in the Editor. I want to show a warning telling users to switch to Windows Store when Unity loads the project. The problem is Unity halts all script executing if it find compilation error, so my editor script is never called. We only ever target Windows Store so I don’t want to pepper IFDEF around if I don’t need to.
Is there any way to still execute an editor script if runtime scripts have compilation errors? Or is there a compilation error callback hook?
PS: To be clear I’m talking about compilation errors when Unity Editor loads a project, not when I build the project.
Pre-2017.3: You can pre-build your scripts to a DLL (in a Visual Studio DLL “library” project) and then after you drag and drop the DLL into the project, simply exclude all platforms from the DLL definition settings except the one you want.
Post-2017.3: You can have different directories compile to different assemblies using .asmdef files (right click in the project window → Create Assembly Definition) which will allow you to choose the build targets supported for the current assembly. You can then separate out everything in directories depending on which assembly you want to compile to, and what build targets are supported for each.
Beyond that, you might be able to only put a single script as a “build target detector” in Plugins, or in its own little DLL, and that may display the message to the user before the compiler goes crazy (put the UnityEditor.Callbacks.DidReloadScripts attribute on the method to run, if you want to test it), but I honestly doubt it.
I got this working on 5.6. I created a managed plugin as per these instructions:
I then used [InitializeOnLoad] on a class in the plugin:
Then put the plugin in Assets/Plugins/Editor as code in Plugins is executed before other folders:
Just adding [InitializeOnLoad] to a script in Assets/Plugins/Editor didn’t work so I guess Unity tries to execute all compiled code in DLLs first then tries to compile everything.
I can now get code to execute in the Editor before script compilation errors