AssetBundle Workflow – Unity Asia Bootcamp

Some quick notes:

What is an asset?

  • Mesh, Material, Texture, Audio, Script, etc…

What is AssetBundle(AB) about?

  • Compression
  • Dynamic Loading
  • Local Cache (From web)
  • Pro Only :(

AssetBundle v.s. Resource.Load

  • Resource.Load will be deprecated in the future
  • Resource.Load is limited to 2GB
  • Resource.Load can’t be loaded dynamically
  • AssetBundle need to be built by script
  • AssetBundle can be loaded dynamically

Where should I use AssetBundle?

  • Web/Mobile/Desktop, everywhere for better asset management and better manage management

Web Caching License can only cache AssetBundle

Cross-platform: need to be built separately for each platform

Platform Support Table:

Standalone Web Player iOS Android
Editor V V V V
Standalone V V
Web Player V V
iOS V
Android V

How to use?

  • via script
    • BuildPipeline.BuildAssetBundle()
    • Bundle any number of assets to a single AB
    • better for splitting single huge scene
    • BuildPipeline.BuildStreamedSceneAssetBundle()
    • Bundle any number of scenes to a single AB
    • BuildPipeline.BuildAssetBundleExplicitAssetNames()
    • Rarely used
    • Bundle any number of assets to a single AB
    • Can give each bundled asset a name
  • Create Option
    • Complete Assets: Bundle Assets with it’s components
    • CollectDependencies
    • DeterministicAssetBundle
      • Assign each object with a unique hash ID
      • for incremental asset bundle deployment
    • UncompressedAssetBundle

Memory Management

unity ab memory manangement

(1):

WWW www = new WWW(url) (without cache)
WWW.LoadFromCacheOrDownload(url) (w/ cache)
//Cache License Free is limited to 50MB on Web player, 4GB on mobile

(2):

//Load AB object
loadedAssetBundle = www.assetBundle;
//Load AB object(from memory) (encrypted)
WWW www = new WWW (url);
yield return www;

byte[] encryptedData = www.bytes;
byte[] decryptedData = Decrypt(encryptedData);

AssetBundle bundle = AssetBundle.CreateFromMemory(decryptedData);
AssetBundle.CreateFromMemory()

//Load AB from Disk
AssetBundle.CreateFromFile()

(3):

//Load Asset From AB:
AssetBundle.Load()
AssetBundle.LoadAsync()
AssetBundle.LoadAll()

//Load Asset From AB which is built by BuildPipeline.BuildStreamedSceneAssetBundle()
Application.LoadLevel()
Application.LoadLevelAsync()
Application.LoadLevelAdditive()
Application.LoadLevelAdditiveAsync()

(4):

Instantiate

AssetBundle Dependency

  • Reduce size
  • Manage Dependency with BuildPipeline.PushAssetDependencies() and BuildPipeline.PopAssetDependencies()
  • Manual Load Order handling
  • Enable DeterministicAssetBundle when implementing incremental loading

AssetBundle dependency Example


     BuildPipeline.PushAssetDependencies();

     var assets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(MaterialPath);
     BuildPipeline.BuildAssetBundle(assets[0], assets, "AssetBundles/mat.unity3d");

     BuildPipeline.PushAssetDependencies();
     assets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(cubePath);
     BuildPipeline.BuildAssetBundle(assets[0], assets, "AssetBundles/cube.unity3d");
     BuildPipeline.PopAssetDependencies();
     
     BuildPipeline.PushAssetDependencies();
     assets = UnityEditor.AssetDatabase.LoadAllAssetsAtPath(cylinderPath);
     BuildPipeline.BuildAssetBundle(assets[0], assets, "AssetBundles/cylinder.unity3d");
     BuildPipeline.PopAssetDependencies();


     BuildPipeline.PopAssetDependencies();

Scene Dependency Example


     @MenuItem ("Build/BuildWebplayerStreamed")
     static function MyBuild() {
          var level1 : String[] = [ "Assets/Level1.unity"];
          var level2 : String[] = [ "Assets/Level2.unity"];
          var level3 : String[] = [ "Assets/Level3.unity"];

          BuildPipeline.PushAssetDependencies();
          BuildPipeline.BuildStreamedSceneAssetBundle(level1, "Streamed-Level1.unity3d", BuildTarget.WebPlayer);
          BuildPipeline.PushAssetDependencies();
          BuildPipeline.BuildStreamedSceneAssetBundle(level2, "Streamed-Level2.unity3d", BuildTarget.WebPlayer);
          BuildPipeline.PushAssetDependencies();
          BuildPipeline.BuildStreamedSceneAssetBundle(level3, "Streamed-Level3.unity3d", BuildTarget.WebPlayer);

          BuildPipeline.PopAssetDependencies();
          BuildPipeline.PopAssetDependencies();
          BuildPipeline.PopAssetDependencies();
     }

Save AssetBundle dependency into a ScriptObject(XML) and stream it from server first

Others’ note(in chinese)

Advertisements