Table of Contents

TAssetBundle

TAssetBundle is a powerful, integrated asset bundle management system.


Index


Using an TAssetBundle Asset

TAssetBundle provides two ways to load assets.

Using Asset Path

The asset path is the full path to the asset and needs to be modified if the asset is moved.

How do you get the asset path?

get_asset_path

using UnityEngine;
using TAssetBundle;

public class TestLoadAsset : MonoBehaviour
{
    public string prefabPath; //Asset Path        

    void Awake()
    {
        //Load as Asset Path
        var loadAsset = TAssetBundles.LoadAssetAsync<GameObject>(prefabPath);
        loadAsset.OnComplete += (AssetHandle<GameObject> assetHandle) =>
        {
            var asset = assetHandle.Get();

            //use asset
            Instantiate(asset);
        };
    }

    void OnDestroy()
    {
        //Unload as Asset Path
        TAssetBundles.UnloadAsset(prefabPath);
    }
}

Using Asset Reference

Asset references allow you to drag and drop or select assets in the editor.

Asset references remain intact even if the asset is moved.

drag_and_drop_asset

using UnityEngine;
using TAssetBundle;

public class TestLoadAssetReference : MonoBehaviour
{
    [AssetType(typeof(GameObject))]
    public AssetRef prefab;

    private AssetHandle prefabHandle;

    void Awake()
    {
        //Load as Asset Reference
        var loadAsset = TAssetBundles.LoadAssetAsync<GameObject>(prefab);
        loadAsset.OnComplete += (AssetHandle<GameObject> assetHandle) =>
        {
            prefabHandle = assetHandle;            
            var asset = assetHandle.Get();

            //use asset
            Instantiate(asset);
        };
    }

    void OnDestroy()
    {
        //Unload as Asset Handle
        TAssetBundles.UnloadAsset(prefabHandle);
    }
}

Using AssetManager

AssetManager provides methods to load, unload, and manage assets and prefabs using TAssetBundle for efficient asset management. It supports asynchronous loading, unloading of unused assets, and pooling of prefabs.

When an asset loaded using AssetManager is linked to a GameObject, it can be tracked by the Asset Usage Tracker.

Loading Assets and Unloading Unused Assets

using UnityEngine;
using TAssetBundle;
using TAssetBundle.Extensions;

public class TestAssetManagerLoadAsset : MonoBehaviour
{
    public Image imageManualLink;
    public Image imageAutoLink;

    /// <summary>
    /// Path to the sprite asset.
    /// </summary>
    public string spritePath;

    private void Start()
    {
        // Load sprite asynchronously and set it to the image.
        AssetManager.LoadAssetAsync<Sprite>(spritePath).OnCompleted += asset =>
        {
            // Use the loaded sprite
            imageManualLink.sprite = asset.Get();

            // Link the asset to the game object. The asset's usage count decreases automatically 
            // when the game object is destroyed. If usage count reaches zero, the asset will be unloaded.
            // A GameObject can only be linked to one asset.
            imageManualLink.LinkAsset(asset);
        };

        // Load sprite asynchronously and link it to the game object.
        AssetManager.LoadAssetAsync<Sprite>(imageAutoLink.gameObject, spritePath).OnCompleted += asset =>
        {
            // Use the loaded sprite
            imageAutoLink.sprite = asset.Get();
        };
    }

    private void OnGUI()
    {
        if(GUILayout.Button("Remove All"))
        {
            ///When all GameObjects linked to an asset are destroyed, the asset becomes unused.
            GameObject.Destroy(imageManualLink.gameObject);
            GameObject.Destroy(imageAutoLink.gameObject);
        }

        if(GUILayout.Button("Unload Unused Assets"))
        {
            ///Immediately unload assets that are not in use.
            AssetManager.UnloadUnusedAssets();
        }
    }
}

Loading and Instantiating Prefabs with Asset Manager

using UnityEngine;
using TAssetBundle;
using TAssetBundle.Extensions;

public class TestAssetManagerGetPrefab : MonoBehaviour
{
    /// <summary>
    /// Prefab Asset Reference.
    /// </summary>   
    public GameObjectAssetRef prefab;    

    private void Start()
    {
        /// Loads a prefab asset and instantiates it.
        /// Links the instantiated GameObject with the asset to track the asset's usage.
        AssetManager.GetPrefabAsync(prefab, transform).OnCompleted += go =>
        {            
            go.name = prefab.Path;
        };
    }

    private void OnGUI()
    {
        if(GUILayout.Button("Remove GameObject"))
        {
            var go = GameObject.Find(prefab.Path);

            if(go != null)
            {
                //When a GameObject is destroyed, the usage count of the linked asset decreases.
                GameObject.Destroy(go);
            }
        }

        if(GUILayout.Button("Unload Unused Assets"))
        {
            ///Immediately unload assets that are not in use.
            AssetManager.UnloadUnusedAssets();
        }
    }
}

Asset Usage Tracker

Run TAssetBundle/Extensions/Asset Usage Tracker from the menu.

During play mode in the editor, it tracks the assets loaded through AssetManager and their linked GameObjects.

asset_usage_tracker


Play with Assets Directly (Only Editor)

Using assets directly allows you to run right away without having to configure or build asset bundles.

Select TAssetBundle/Settings Menu

settings_hilght

Select Editor Play Mode as Editor Asset

editor_asset_mode

Play!!


Play with Asset Bundle

TAssetBundle uses your project's folder structure to organize and build asset bundles according to your preferences.

To play with AssetBundles, you must first configure the AssetBundle using TAssetBundleManifest.

What is TAssetBundleManifest

  • You can manually configure AssetBundles for the assets in a folder or automatically configure them using a composition strategy.
  • Enables the setup of built-in options, encryption, tags, etc., for the AssetBundle.
  • Provides information about the configured AssetBundle.

Create TAssetBundleManifest

Create TAssetBundleManifest in the folder where you want to bundle assets.

create_manifest

Select TAssetBundleManifest file

select_manifest

Configure AssetBundle

There are two ways to organize your AssetBundles.

Manual Configuration

add_assetbundle_build_info

Add asset bundle build information

drag_and_drop_assets

Finish!

Auto Configuration

Automatically configure AssetBundles using a Composition Strategy

What is Composition Strategy

  • individual composition strategy to configure AssetBundle in TAssetBundleManifest
  • Run Composition Strategy from the context menu will run the strategies in the order listed.

add_composition_strategy

Add Composition Strategy

select_composition_strategy select_composition_strategy2

Select all_together Composition Strategy

all_together is a strategy to organize assets in a folder into one AssetBundle.

run_composition_strategy

Run Composition Strategy in the context menu of TAssetBundleManifest

Finish!

Build Asset Bundle

build_asset_bundle

Build asset bundle

Editor Play Mode to AssetBundle

Select TAssetBundle/Settings Menu

setting_playmode_assetbundle

Select Editor Play Mode as Asset Bundle

Play!!


Remote Assets Download

Flow of Remote Assets Download

  1. Check for Catalog Update
  2. Update the Catalog
  3. Get the Download Size
  4. Download the Assets
  • Downloaded assets are cached and will not be downloaded again.
using UnityEngine;
using TAssetBundle;

public class RemoteDownloadAssets : MonoBehaviour
{
    public string remoteDownloadUrl; //your remote storage address
    public GameObjectAssetRef needDownloadAsset;

    private void Awake()
    {
        //[BuildTarget] in url will be changed to your BuildTarget
        TAssetBundles.SetRemoteUrl(remoteDownloadUrl);
    }

    private void OnGUI()
    {
        GUILayout.BeginVertical(GUI.skin.box);

        if (GUILayout.Button("Check Catalog Update"))
        {
            var checkCatalogUpdateAsync = TAssetBundles.CheckCatalogUpdateAsync();

            checkCatalogUpdateAsync.OnComplete += (result) =>
            {
                text.text = "need catalog update - " + result;
            };
        }

        if (GUILayout.Button("Update Catalog"))
        {
            var updateCatalogAsync = TAssetBundles.UpdateCatalogAsync();

            updateCatalogAsync.OnComplete += (result) =>
            {
                text.text = "update catalog - " + result;
            };
        }

        if (GUILayout.Button("Get Download Size All"))
        {
            var downloadSizeAsync = TAssetBundles.GetDownloadSizeAsync();

            downloadSizeAsync.OnComplete += size =>
            {
                text.text = "download size - " + FileSizeFormmater.FormatSize(size);
            };
        }

        if (GUILayout.Button("Download All"))
        {
            var downloadAsync = TAssetBundles.DownloadAsync();
            downloadAsync.OnProgress += OnDownloadProgress;
            downloadAsync.OnComplete += OnDownloadComplete;
        }

        if (GUILayout.Button("Get Download Size By Tags"))
        {
            var downloadSizeAsync = TAssetBundles.GetDownloadSizeByTagsAsync(new string[] { "character" });

            downloadSizeAsync.OnComplete += size =>
            {
                text.text = "download size - " + FileSizeFormmater.FormatSize(size);
            };
        }

        if (GUILayout.Button("Download By Tags"))
        {
            var downloadAsync = TAssetBundles.DownloadByTagsAsync(new string[] { "character" });
            downloadAsync.OnProgress += OnDownloadProgress;
            downloadAsync.OnComplete += OnDownloadComplete;
        }

        if (GUILayout.Button("Get Download Size By Assets"))
        {
            var downloadSizeAsync = TAssetBundles.GetDownloadSizeByAssetsAsync(new AssetRef[] { needDownloadAsset });

            downloadSizeAsync.OnComplete += size =>
            {
                text.text = "download size - " + FileSizeFormmater.FormatSize(size);
            };
        }

        if (GUILayout.Button("Download By Assets"))
        {
            var downloadAsync = TAssetBundles.DownloadByAssetsAsync(new AssetRef[] { needDownloadAsset });
            downloadAsync.OnProgress += OnDownloadProgress;
            downloadAsync.OnComplete += OnDownloadComplete;
        }

        if (GUILayout.Button("Load TestScene"))
        {
            TAssetBundles.LoadSceneAsync(testScene);
        }

        if (GUILayout.Button("Clear Cached Asset Bundles"))
        {
            Util.ClearCachedAssets();
        }

        GUILayout.EndVertical();
    }

    private void OnDownloadProgress(AssetBundleDownloadInfo downloadInfo)
    {
        double value = downloadInfo.DownloadedSize / (double)downloadInfo.TotalDownloadSize;

        text.text = string.Format("downloading - {0:0.00}%, {1}/{2}",
            value * 100,
            FileSizeFormmater.FormatSize(downloadInfo.DownloadedSize),
            FileSizeFormmater.FormatSize(downloadInfo.TotalDownloadSize));
    }

    private void OnDownloadComplete(AssetBundleDownloadInfo downloadInfo)
    {
        if (downloadInfo.TotalDownloadSize == 0)
        {
            text.text = "don't need to download";
        }
        else if (downloadInfo.IsDownloadComplete())
        {
            text.text = "download complete";
        }
        else
        {
            text.text = "download fail";
        }
    }
}

TAssetBundleManifest

tassetbundle_manifest

  1. If enabled, it is included in the AssetBundle build target.
  2. If built-in is enabled, the configured AssetBundles are included in the app.
  3. If encrypt is enabled, it encrypts the configured AssetBundle.
  4. Set tags for configured AssetBundles.
  5. A list of composition strategies, strategies are always run in order.
  6. AssetBundle build list, AssetBundle build information that has been configured.

Default Composition Strategies

default_composition_strategy

  • all_separatly: Add assets as individual asset bundles.
  • all_together: Add assets as one asset bundle.
  • clear: Clear asset bundle build information.
  • exclude_objects: Exclude specific assets from asset bundle build information.
  • file_separatly: Add only the files as individual asset bundles.
  • file_together: Add only the files as one asset bundle.
  • fixed_objects: Add only specific assets to one asset bundle.
  • folder_separatly: Add only folders as individual asset bundles.
  • fodler_together: Add only the folder as one asset bundle.
  • regex_match: Only assets whose file names match the regular expression are added to one asset bundle.
  • regex_match_delete: Remove asset bundle build information if asset filename matches regular expression
  • regex_not_match_delete: Remove AssetBundle build information if asset file name does not match regular expression
  • same_name_together: Add assets with the same file name as one asset bundle

TAssetBundle Menu

tassetbundle_menu

  1. Open TAssetBundle Settings File
  2. Remove build cache information for current platform
  3. Build Asset Bundle
  4. Run the composition strategy on all TAssetBundleManifest
  5. Clear AssetBundle build information for all TAssetBundleManifest using Composition Strategy
  6. Open Build Output Folder
  7. Open Download Cached Assets Folder
  8. Remove Download Cached Assets
  9. Open TAssetBundle Browser
  10. Open Tag Editor
  11. Open Web Server Test Window
  12. Open AssetBundle Dependency Checker
  13. Open Asset Reference Tracker

TAssetBundle Browser

The TAssetBundle Browser is a browser that allows you to view all TAssetBundleManifests and current AssetBundle information at once.

Select TAssetBundle/TAssetBundle Browser Menu

tassetbundle_browser

  1. Clear AssetBundle build information for all TAssetBundleManifest using Composition Strategy
  2. Run the composition strategy on all TAssetBundleManifest
  3. Build Asset Bundle
  4. Check dependencies between AssetBundles
  5. Total Asset Bundle Count
  6. A single AssetBundle build name and the assets it contains
  7. TAssetBundleManifest and file path
  8. Count of AssetBundles configured in TAssetBundleManifest
  9. Assets not yet configured as AssetBundle in TAssetBundleManifest

TAssetBundle Settings

All settings used by TAssetBundle

tassetbundle_settings

  1. The name of the catalog file generated when building the AssetBundle
  2. AssetBundle Build Output Folder Name
  3. Catalog file extension
  4. Asset Bundle file extension
  5. Enable catalog compression
  6. Enable catalog encryption
  7. encryption key
  8. Add the hash of the asset bundle file name
  9. Editor Play Mode (Editor Only)
  10. Forced in editor to use only remote AssetBundles. (Editor only)
  11. Maximum count of concurrent web requests
  12. Maximum count of web request retries
  13. Retry request wait duration
  14. Using the build cache only builds the AssetBundles that need to be built.
  15. Enable Debugging Log
  16. Include catalog in app
  17. How to embed AssetBundles into app
  18. Recompress the AssetBundles built-in in your app to lz4 (very efficient, but slightly increases file size).
  19. If enabled, the UnityRemoteAssetBundleProvider is used. When disabled, always uses a SpecificManagedAssetBundleProvider. SpecificManagedAssetBundleProvider are default used for crypto asset bundles.
  20. Set the custom catalog serializer
  21. Set the custom crypto serializer
  22. Set default remote url