Search Unity

Scripts in Editor Folders Throwing Build Errors When Building AssetBundles for Mobile

Discussion in 'Editor & General Support' started by Kevin-Phunware, Jul 11, 2017.

  1. Kevin-Phunware

    Kevin-Phunware

    Joined:
    Jun 7, 2017
    Posts:
    3
    I have several EditorWindow and Editor scripts within Editor folders, and when I build AssetBundles for mobile platforms (eg iOS), these scripts Editor scripts throw errors for symbols they're referencing within non-Editor scripts - which are all within #if UNITY_EDITOR blocks.

    It seems very strange to me that AssetBundle builds, while set to iOS or Android, will not build #if UNITY_EDITOR code within modules not residing within Editor folders, but it WILL build modules within Editor folders. Furthermore, this code, which is "Editor Only" for utilities and other functions is completely unrelated and unnecessary for building AssetBundles.

    Am I missing something? How do I hide both from AssetBundle builds or expose #if UNITY_EDITOR code to the AssetBundle build? Btw, I tried wrapping my scripts in Editor folders in #if UNITY_EDITOR blocks, but this didn't change anything.

    Followup Edit -
    I commented out all references in modules residing in Editor folders to non Editor modules and I was able to build AssetBundles successfully for mobile.
    So it seems there's some problem in AssetBundle builds with Editor scripts referencing #if UNITY_EDITOR code blocks in non-Editor scripts.

    Followup Edit 2 -
    So I've narrowed down the offending situation. I've illustrated this in pseudo code below (in C#).
    File in Assets/Editor
    --------------------------
    #include UnityEditor;

    [CustomEditor(typeof(MyClass))]
    public class MyClassEditor : Editor
    {
    public override void OnInspectorGUI()
    {
    MyClass.InternalMethodOne(); // this is ok, since its declaration is outside #if block
    MyClass.InternalMethodTwo(); // this throws an unknown definition compile error
    }
    }

    File in Assets/Scripts -- will throw compile errors when building AssetBundles for mobile since function symbol declarations are inside an inactive conditional compilation block
    ----------------------------
    #include UnityEngine;

    public class MyClass : Monobehaviour
    {
    public void InternalMethodOne()
    {
    }

    #if UNITY_EDITOR
    public void InternalMethodTwo()
    {
    }

    public void InternalMethodTwo()
    {
    }

    public void InternalMethodThree()
    {
    }

    public void InternalMethodFour()
    {
    }
    #endif
    }

    To fix this, we have to do the following instead:
    public class MyClass : Monobehaviour
    {
    public void InternalMethodOne()
    {
    }

    public void InternalMethodTwo()
    {
    #if UNITY_EDITOR
    #endif
    }

    public void InternalMethodTwo()
    {
    #if UNITY_EDITOR
    #endif
    }


    public void InternalMethodThree()
    {
    #if UNITY_EDITOR
    #endif
    }

    public void InternalMethodFour()
    {
    #if UNITY_EDITOR
    #endif
    }
    }

    This is slightly more annoying and I'd say less than Ideal since we're forced to build stub symbol definitions into production code. I think Unity should be able to address this issue.
     
    Last edited: Jul 11, 2017
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    can you show the exact error message you're getting?

    Also, the correct syntax is "using UnityEngine" and not #include
     
  3. wderstine

    wderstine

    Joined:
    Nov 16, 2016
    Posts:
    2
    The exact same is happening with a plugin's code (that's not even tied to my bundles I think) which is within an
    #if 
    #endif
    The compiler errors are showing up in Visual Studio as well. It's odd that a seemingly unrelated script would derail asset bundles from building.
     
  4. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    604
    Edit: I made a mistake reading your post. I now understand that the reference was Editor code pointing to non Editor code. Rethinking the problem. Leaving original post below.

    Edit #2: My best guess is that the build process does a full build before it then strips the Editor assemblies before building the other code and packaging it. That might not be intended - maybe a bug. But the workaround doesn't seem to heavy, luckily.

    --- OP

    What you've posted above is exactly how it should work. The "to fix this" is how you need to do it (and is the intended way). That, or you wrap the call to those methods in a compilation flag as well.

    You cannot have a line of code outside of #if UNITY_EDITOR that references anything inside of that compilation flag. You will always have compile errors, because - given your above example - "MyClass.InternalMethodTwo();" does not exist. It is the same thing as if you were to literally type this "GooglyBoogly.HelloWorld()" right now into your code. Both GooglyBoogly and HellowWorld() don't exist in your project (I am willing to bet).

    Same thing happening here when the code is compiled out for a package that does not define the compile flag "UNITY_EDITOR", such as anything that is not running in the editor.
     
    Last edited: Jun 13, 2018