Search Unity

Terrain doesnt appear when loading from asset bundle

Discussion in 'WebGL' started by nsmith1024, Nov 3, 2018.

  1. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    645
    Hello,

    Im using Unity 2018.2.14f1 When i create a simple scene with a terrain and trees, then save it as an asset bundle, then when i load the asset bundle the terrain (and trees) are always missing from the scene when the scene is run in the browser, but it appears when run in the editor.

    When loading from the browser, the terrain is not there so the player just falls right thu the ground (which isnt there) so everything ends there. But the same thing works properly when running from the editor.

    Does anybody know why this is happening?

    How do i fix?

    Thanks
     
    Last edited: Nov 3, 2018
  2. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    645
    Im using Unity 2018.2.14f1

    See other people have the same issue:

    https://forum.unity.com/threads/bitfieldinsert-and-shader-error.548491/#post-3853921

    Here is the debugger information from the browser after the scene bundle is loaded.

    All i did was create a new unity project, added a blank terrain (no trees or texture on the terrain), moved the terrain data to the Resources folder, then created an asset bundle of the whole scene using this code.

    Code (CSharp):
    1.  
    2. BuildPipeline.BuildAssetBundles("D:/UnityProjects/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.WebGL);
    3.  
    The i loaded the asset bundle scene with this code

    Code (CSharp):
    1.  
    2. string url="http://www/example.com/bundleFile";
    3.  
    4.         WWW www = new WWW(url);
    5.         yield return www;
    6.  
    7.        if (www.assetBundle != null) {
    8.             GlobalData.assetBundle = www.assetBundle;
    9.             string[] scenePath = GlobalData.assetBundle.GetAllScenePaths();
    10.             GlobalData.loadedBundleScene = nameInBundle;
    11.             SceneManager.LoadScene(nameInBundle, LoadSceneMode.Additive);
    12.             Debug.Log("AFTER LOADED SCENE ASSET");
    13.         }
    14.         else {
    15.             Debug.Log("LOAD SCENE ASSET WAS NULL!!!!!");
    16.         }
    17.         sceneLoaded = true;
    18.     }
    This is the output from the browser debugger....

    AFTER LOADED SCENE ASSET

    UnityLoader.js:4
    UnityLoader.js:4 (Filename: ./Runtime/Export/Debug.bindings.h Line: 43)
    UnityLoader.js:4
    UnityLoader.js:4 Could not produce class with ID 154.
    UnityLoader.js:4 This could be caused by a class being stripped from the build even though it is needed. Try disabling 'Strip Engine Code' in Player Settings.
    UnityLoader.js:4
    UnityLoader.js:4 (Filename: Line: 1543)
    UnityLoader.js:4
    UnityLoader.js:4 ERROR: 0:89: 'bitfieldInsert' : no matching overloaded function found
    UnityLoader.js:4 ERROR: 0:90: 'bitfieldInsert' : no matching overloaded function found
    UnityLoader.js:4
    UnityLoader.js:4 Note: Creation of internal variant of shader 'Hidden/TerrainEngine/CameraFacingBillboardTree' failed.
    UnityLoader.js:4 WARNING: Shader Unsupported: 'Hidden/TerrainEngine/CameraFacingBillboardTree' - Pass '' has no vertex shader
    UnityLoader.js:4 WARNING: Shader Unsupported: 'Hidden/TerrainEngine/CameraFacingBillboardTree' - Setting to default shader.
    UnityLoader.js:4 Your current multi-scene setup has inconsistent Lighting settings which may lead to different lighting when loading scenes individually or in a different order! Consider homogenizing the following:
    UnityLoader.js:4 1/2 scenes have different Realtime GI settings.
    UnityLoader.js:4 1/2 scenes use different skyboxes.
    UnityLoader.js:4
    UnityLoader.js:4
    UnityLoader.js:4 (Filename: Line: 124)
    Invalid Layer Index '4'

    (Filename: Line: 898)
    UnityLoader.js:4
    UnityLoader.js:4
    UnityLoader.js:4 (Filename: ./Runtime/Export/Debug.bindings.h Line: 43)
    UnityLoader.js:4
    UnityLoader.js:4 The referenced script on this Behaviour (Game Object '<null>') is missing!
    UnityLoader.js:4
    UnityLoader.js:4 (Filename: Line: 1775)
    UnityLoader.js:4
    UnityLoader.js:4 The referenced script on this Behaviour (Game Object 'TMP SubMeshUI [TextMeshPro/Sprite]') is missing!
    UnityLoader.js:4
    UnityLoader.js:4 (Filename: Line: 1775)
    UnityLoader.js:4
    UnityLoader.js:4 The referenced script on this Behaviour (Game Object 'TMP SubMeshUI [TextMeshPro/Sprite]') is missing!
    UnityLoader.js:4
    UnityLoader.js:4 (Filename: Line: 1775)
     
    Last edited: Nov 4, 2018
  3. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    645
  4. Schubkraft

    Schubkraft

    Unity Technologies

    Joined:
    Dec 3, 2012
    Posts:
    912
    Could you be so nice to submit this repro as a bug report? Thanks in advance!
     
  5. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    645
    I submitted a bug report, do you know how long it takes to fix a bug?

    I need to do a demo to INVESTORS later this week, and i need this to work!

    Right now i cant have any scene with a terrain because of this bug, and all my scenes have terrains!
     
  6. Schubkraft

    Schubkraft

    Unity Technologies

    Joined:
    Dec 3, 2012
    Posts:
    912
    Later this week is not going to be happening, sorry.
     
  7. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    645
    Do you have any idea when :(

    Its just my whole future riding on it, nothing important
     
  8. Schubkraft

    Schubkraft

    Unity Technologies

    Joined:
    Dec 3, 2012
    Posts:
    912
    I can't give you an estimate at all right now, sorry. What is the case ID for your submission?
     
  9. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    204
    It's not really a bug, it's more a side-effect because Unity tries to remove everything that isn't used during the build process to make the download and load times for your users as short as possible. Because your terrain (and whatever else) are in asset bundles, Unity doesn't know these assets are needed when it's building. However you can make Unity aware of your asset bundles without too much effort.

    I recommend you take a look at this answer posted by @JoshPeterson a while back (hope he doesn't mind the mention, he might be able to confirm anything I missed):

    https://answers.unity.com/questions/1432923/asset-bundle-code-stripping-linkxml-not-working.html

    Basically you want to call the build pipeline via code (a simple editor script will do) that points at the root asset bundle manifest file. Basically, keep that manifest file around, you'll need it :).

    A shortened version of the example code:

    Code (CSharp):
    1. using UnityEditor;
    2.  
    3. public class BuildPlayer
    4. {
    5.     [MenuItem("Build/Build WebGL")]
    6.     private static void Build()
    7.     {
    8.         BuildPipeline.BuildPlayer(new BuildPlayerOptions
    9.         {
    10.             scenes = GetSceneNames(),
    11.             locationPathName = @"C:\path\to\your\build\output\folder\",
    12.             assetBundleManifestPath = @"C:\path\to\your\AssetBundles.manifest",
    13.             target = BuildTarget.WebGL,
    14.             options = BuildOptions.None
    15.         });
    16.     }
    17.  
    18.     private static string[] GetSceneNames()
    19.     {
    20.         int sceneCount = EditorBuildSettings.scenes.Length;
    21.         string[] sceneNames = new string[sceneCount];
    22.         for (int i = 0; i < sceneCount; ++i)
    23.         {
    24.             sceneNames[i] = EditorBuildSettings.scenes[i].path;
    25.         }
    26.  
    27.         return sceneNames;
    28.     }
    29. }
    What this does is allow Unity to know what exactly it needs to keep when loading all the asset bundles during runtime.

    This should solve your crash issue.. however you might start seeing pink as we're not quite done yet.

    A bit of a pain here is the shaders unfortunately aren't also referenced in the manifest file (idea for Unity maybe :)). What you'll want to do is to load your scene up with your terrain and everything else in the editor, then save out a ShaderVariantCollection (via the Edit/Project Settings/Graphics Settings menu) (using 'Save to asset' at the bottom) and then add it to the list of 'Preloaded Shaders', again in the Graphics Settings window.

    I was successfully able to load an empty scene and then load in a terrain prefab via an asset bundle. No hackery of adding a 1x1 terrain scene, although that probably works too. Good luck :)
     
    Last edited: Nov 7, 2018
  10. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    645
    The case number is 1098025

    Also there is another bug in WebGL im seeing. If i change scenes more than twice it crashes.

    Changing scenes means unloading the current scene, and loading a new scene asset bundle from the internet.

    If i load one scene, play it, then try to unload current scene asset, and load another one, it crashes. I been using the same unchanged code for almost a year, with no problems, suddenly it crashes.
     
  11. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    645
    Thanks JJJohan

    I tried what you said, but it seems the code you posted is actually building the whole project, but i just need it to just create a scene asset bundle.

    My project is like this. I have several separate unity projects to make the one game.

    The main project contains all the code and some basic assets. The other smaller projects (maybe 10 of them) contains only scenes (objects, terrains, trees grass, textures etc) and but no code. In these smaller projects, the scenes are built as scene asset bundles and each stored in a separate folder on the server.

    When the game runs it loads in the main scene (almost empty), then on top of that scene, it loads one of the other scenes from the other unity projects from the internet as "scene asset bundles".

    This way i can keep adding new scenes without rebuilding anything, i can just upload a new scene to the server, the main program then has many scenes it can choose to load, including new scenes produced later, without rebuilding the main code.

    Each of these "scenes" are saved on a server as a "scene asset bundle" built using this code

    Code (CSharp):
    1.    
    2. [MenuItem("MyMenu/Build AssetBundles WebGL")]
    3.     static void BuildAllAssetBundlesWebGL()
    4.     {
    5.         BuildPipeline.BuildAssetBundles("D:/UnityProjects/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.WebGL);
    6.     }
    7.  
    Your code seems to build the entire project as if its the code base and not just a scene asset.

    The problem is that either the code above is not including the terrain in the scene asset bundle, or when it loads in the scene asset bundle, its not extracting the terrain, or not rendering it, or some other error, i got no idea.

    Thanks for your help anyway!
     
    Last edited: Nov 7, 2018
  12. JJJohan

    JJJohan

    Joined:
    Mar 18, 2016
    Posts:
    204
    You'll want to (re)build your main project at least once the way I described - basically you'll want to fill a throw-away asset bundle with any type of data that you think you'll add in the future (terrains, particles, trees, etc.). You only need to build this project once (in your case) but it needs to know what kind of assets to expect from any future asset bundles.

    Then whenever you create your new asset bundles at a later date and load them in your original scene it should load them without issue because it should have kept all the code necessary to deal with those types of assets.

    To give a simple example:

    Today - Create a temporary asset bundle and put in a terrain, a tree, a mesh and build your main project - pointing at the manifest file of this temporary asset bundle. Then upload your build and delete your temporary asset bundle - you no longer need it.

    Tomorrow - You want to add a new scene with a terrain in it. Create your asset bundle, upload it and load it in your original build. It should load without problems.
     
  13. nsmith1024

    nsmith1024

    Joined:
    Mar 18, 2014
    Posts:
    645
    It worked, i can see the terrain now, its like a miracle!

    Thanks so much!

    Only another bug popped up to kill my joy, video clips stopped playing for some reason, just shows black only, but i hear audio.

    It never stops! Everyday something else!

    Thanks again for your help!!!