Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Bug with loading scene from asset bundle

Discussion in 'Scripting' started by AmitChBB, Feb 21, 2022.

  1. AmitChBB

    AmitChBB

    Joined:
    Feb 16, 2021
    Posts:
    36
    Hi there :)

    I am experiencing a weird bug when loading a scene from an asset bundle -
    for some bizarre reason, the GameObjects within the scene are reorganized.
    Specifically - they are sorted alphabetically.

    For example, I am building a simple mostly empty scene with the following hierarchy:



    However, when loading that scene from the asset bundle, it looks like this:




    The following is my asset bundle building logic:

    Code (CSharp):
    1.  
    2. [MenuItem("Assets/Build AssetBundles")]
    3.     private static void BuildAllAssetBundles()
    4.     {
    5.         string bundleName = "emptyScene";
    6.         string timestamp = DateTime.Now.ToShortTimeString().Replace(":","");
    7.        
    8.         string assetBundleDirectory = "Assets/StreamingAssets";
    9.         if (!Directory.Exists(Application.streamingAssetsPath))
    10.             Directory.CreateDirectory(assetBundleDirectory);
    11.  
    12.         AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
    13.         buildMap[0].assetBundleName = bundleName + "_" + timestamp;
    14.         buildMap[0].assetNames = new string[1]
    15.         {
    16.             "Assets/Scenes/EmptyScene.unity"
    17.         };
    18.        
    19.         BuildPipeline.BuildAssetBundles(assetBundleDirectory, buildMap, BuildAssetBundleOptions.None,
    20.             EditorUserBuildSettings.activeBuildTarget);
    21.     }


    Has anyone experienced this bug before? Perhaps a clue as to what may be the cause of the issue?

    Thanks in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    I don't think the order of root objects is defined. It's just sort of a big bag-o-data. Watching a running game it sorta seems random for the most part.

    Everything that is parented under another GameObject should still be correctly ordered.
     
  3. AmitChBB

    AmitChBB

    Joined:
    Feb 16, 2021
    Posts:
    36
    Hi Kurt-Dekker,

    This isn't a big deal if it comes to random GameObjects, however in the world of UI the order of children directly affects how the objects are rendered on top of each other.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Did you read what I wrote?

    In my "world of UI" everything lives under a Canvas.

    Therefore the items below it are parented.

    Therefore its order is preserved.

    Is that not the case?
     
  5. AmitChBB

    AmitChBB

    Joined:
    Feb 16, 2021
    Posts:
    36
    Oh, you're correct. My bad.
    Not only is the parent-child relationship preserved, but all non-root GameObjects maintain their child index.

    Thanks for the help!
     
    Kurt-Dekker likes this.