Giter Club home page Giter Club logo

asset-bundle-simplified's Introduction

Asset Bundle Simplified

The purpose of this package is to simplify the usage of Asset Bundles. It's main features are:

  • Dynamic loading of asset bundle dependencies (through a manifest).
  • Dependency counter to correctly unload shared dependent asset bundles.
  • "Simulation Mode", to quickly test modifications without rebuilding the asset bundle.
  • Debug window to track which asset bundles are loaded.

Basic Usage

Load Assets

When you load an asset, it's bundle is automatically loaded. You must unload this bundle manually. The following code shows how to load a prefab and instantiate it:

private void CreateObject()
{
    GameObject prefab = BundleResources.LoadAsset<GameObject>("bundleName", "prefabName");
    Instantiate(prefab);
}

It is also possible to load all assets from a given type:

private YourConfig[] LoadConfigs()
{
    YourConfig[] configs = BundleResources.LoadAllAssets<YourConfig>("bundleName");
    return configs;
}

Load Assets Asynchronously

The following code shows how to load a prefab and instantiate it, using an asynchronous coroutine:

private IEnumerator Start()
{
    var request = BundleResources.LoadAssetAsync<GameObject>("bundleName", "prefabName");
    yield return request; // Waits until the asset is loaded.
    Instantiate(request.Asset);
}

You can also register a callback that is called when the asset finishes loading.

private void Start()
{
    var request = BundleResources.LoadAssetAsync<GameObject>("bundleName", "prefabName");
    request.OnComplete += (asset) => {
        Instantiate(asset);
    }
}

The API returns an object similar to the native Unity AsyncOperations.

Load Scenes

The following code shows how to load scenes from an AssetBundle.

private void LoadMenu()
{
    BundleResources.LoadScene("bundleName", "sceneName", LoadSceneMode.Single);
}

You may load them in any LoadSceneMode.

Unload Asset Bundles

The following code shows how to unload an asset bundle.

private void UnloadBundle()
{
    BundleResources.UnloadBundle("bundleName")
}

Build Asset Bundles

To use the API, the asset bundles should be built on the path StreamingAssets/bundles.

[MenuItem("Tools/Asset Bundle Simplified/Build Asset bundles")]
private static void BuildAssetBundles()
{
    string assetBundlesPath = Path.Combine(Application.streamingAssetsPath, BundleResources.PATH_IN_STREAMING_ASSETS);

    assetBundlesPath = assetBundlesPath.Replace(@"\", "/");
    
    if (Directory.Exists(assetBundlesPath))
    {
        Directory.Delete(assetBundlesPath, true);
    }

    Directory.CreateDirectory(assetBundlesPath);

    BuildPipeline.BuildAssetBundles(assetBundlesPath,
        BuildAssetBundleOptions.ChunkBasedCompression
        | BuildAssetBundleOptions.ForceRebuildAssetBundle, EditorUserBuildSettings.activeBuildTarget);

    AssetDatabase.Refresh();
}

Asset Bundle declarations

To use this API the bundles must be organized in a specific way. There are two kinds of bundles:

  • "Root" Asset Bundles: These bundles are directly used through the API. They usually contain scenes, prefabs or configs.
  • "Child" Asset Bundles: These bundles contain assets used by Root Asset Bundles, like textures, meshes, animations, etc. They are loaded automatically when a root bundle that depends on it is loaded. Loading these bundles directly will cause undefined behaviors.

The API keeps track of how many root bundles depend on each child bundle, and unloads them when it is no longer being used, allowing for easy sharing between bundles.

Simulation Mode

The simulation mode allows the usage of asset bundles in the editor without building them first.

You can toggle this feature at Tools/Asset Bundle Simplified/SimulationModes.

Debug Window

The window allows you to keep track of all loaded bundles during the game execution in the editor. It also shows the reference count for the child bundles.

Open the window at Tools/Asset Bundle Simplified/DebugWindow.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.