Giter VIP home page Giter VIP logo

assetbundlemanager's Introduction

Asset Bundle Manager (ABM)

Yet another asset bundle manager for Unity.

Why

We felt the AssetBundleManager provided by Unity was complicated and out-dated... So like typical programmers we decided to write our own! We wanted something with few frills and easier to trace & maintain.

Requirements

  • Unity 2017.1 or greater.

Recommendations

This module pairs well with Unity's Asset Bundle Browser (ABB) tool.

How

Initializing

First you need to build your bundles. By default the ABB puts bundles in PROJECT\AssetBundles\PLATFORM and ABM can take advantage of it.

Once the bundles are built you can start accessing them with the manager.

When you are done testing your bundles you need to upload them to a server. They can go anywhere in the server as long as they are contained in a PLATFORM folder. For example, builds for iOS bundles should be accessable from http://www.example.com/AssetBundles/iOS. The full list of supported targets can be found in AssetBundleUtility.cs.

var abm = new AssetBundleManager()

if (Application.isEditor)
    abm.UseSimulatedUri();
else
    abm.SetBaseUri("https://www.example.com/bundles");

abm.Initialize(OnAssetBundleManagerInitialized);

UseSimulatedUri() configures ABM to use ABB's default folder structure to retrieve bundles. This convenience means you don't have to upload your bundles to a remote server in order to test them, you can use your local files instead.

The SetBaseUri(...) function configures ABM to point to a remote server that contains your bundles.

Calling Initialize(...) causes ABM to download the manifest file for your bundles. Once this file is downloaded and processed you are ready to begin downloading bundles.

If you prefer to use a coroutine instead of a callback for initializing:

var abm = new AssetBundleManager();
// ...
var initializeAsync = abm.InitializeAsyn();
yield return initializeAsync;
if (initializeAsync.Success) {
    // ...
}

Both initialize calls will return a boolean to indicate whether the manifest was downloaded successfully or not.

Downloading

Just like initializing you can use either callbacks or coroutines to download bundles.

Callback

public function GetMyBundle() 
{
    abm.GetBundle("MyBundle", OnBundleDownloaded);
}

public function OnBundleDownloaded(AssetBundle bundle)
{
    if (bundle != null) {
        // Do something with the bundle
        abm.UnloadBundle(bundle);
    }
}

Coroutine

/// Coroutine
var bundle = abm.GetBundleAsync(MyBundle);
yield return bundle;

if (bundle.AssetBundle != null) {
    // Do something with bundle.AssetBundle
    abm.UnloadBundle(bundle);
}

If ABM is unable to download the bundle it will log an error describing the problem and return a null bundle. Therefore it's important to check whether the bundle is null before attempting to use it.

By default bundles are cached using Unity's caching system. The exception to this is the manifest file, which is never cached and always downloaded fresh on initialization. You can override this behaviour on non-manifest bundles by including the DownloadSettings parameter on a GetBundle call:

var bundle = abm.GetBundle("MyBundle", OnBundleDownloaded, DownloadSettings.DoNotUseCache);

Notice that the above examples call UnloadBundle(...) after they are done using the bundle. This is to help ABM manage memory and bundle usage. If two scripts download the same bundle then that bundle is reused for both scripts and will remain in memory until BOTH of those scripts unload the bundle. If memory usage is important to you then you must ensure that every script that loads a bundle also unloads the bundle. If you want to keep the bundle in memory and available at any time then feel free to skip the UnloadBundle(...) call.

StreamingAssets

ABM supports pre-caching your bundles with the use of the StreamingAssets folder in Unity. Once your bundles are built you can copy the manifest and any number of bundles to the StreamingAsests\PLATFORM folder in your project. For example if you wanted to pre-cache the SomeBundle iOS bundles you would have a structure like:

PROJECT
  \Assets
    \StreamingAssets
      \iOS
        \iOS
        \iOS.manifest
        \SomeBundle
        \SomeBundle.manifest

When you make a GetBundle(...) call ABM will check to see if that bundle exists in the StreamingAssets folder first and use it if its hash matches the hash of the remote server. If the file does not exist OR the hash is different then the remote bundle is used. You can change this behaviour when initializing ABM by changing the prioritization strategy:

abm.SetPrioritizationStrategy(PrioritizationStrategy.PrioritizeStreamingAssets);

This will tell ABM to always use the StreamingAssets bundle if it exists. If the bundle doesn't exist in StreamingAssets the remote one will be used.

Cleanup

There are two patterns you should follow when using ABM. The first, as mentioned before, is to always unload the bundle when you are finished with it:

abm.UnloadBundle(bundle);

If no other scripts are using this bundle it will be unloaded from memory. Likewise, when you are completely done with ABM (maybe because you're switching scenes and don't need the bundles anymore) you can dispose of it:

abm.Dispose();

This will force ALL bundles (and their objects) to be unloaded.

assetbundlemanager's People

Contributors

danoli3 avatar foddermk avatar giawa avatar rdeluxe avatar sadpandastudios avatar

Watchers

 avatar

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.