Sunday, August 7, 2011

Asset Management in Unity

Unity has several different ways to manage assets. But which is best for the task at hand?

The Resources.Load() Method (Resource Page)

In the Project pane of Unity GUI, stick your assets in a folder called Resources then load them through code via a call to Resources.Load(path). When using this method, all assets in the resources folder are packed into files (one for each scene, I believe) that are included alongside the game's executable. When a scene is loaded, this assets file is also loaded into memory, whether or not the assets are used. I suspect Unity performs optimizations such that this is the quickest way to load assets, but it can be quite wasteful in terms of memory.

For our purposes, this is probably not the way to go. If we have a battle where we only need to load animations for 4 unit types, yet it loads the animation pngs for 50 units, we are wasting a great deal of memory. In Flash terms, this is akin to putting assets in the library of a FLA versus loading them dynamically with something like BulkLoader. If you load the SWF/SWC produced from your asset FLA, it's all-or-nothing in memory, but with BulkLoader you can pick or choose.

The WWW Method (Resource Page)

You can use WWW to load resources from outside the project at runtime, so this is the Unity way to do true dynamic loading. Given the class name I suspect it's designed to load assets from remote internet locations. Apparently, it can also be used to load local assets, meaning WWW was a poor choice of class name. It can be used in conjunction with the AssetBundle class, but this is only in Unity Pro so I'll be ignoring this for now.

I will try this method out but I don't want to have to provide unpackaged assets when distributing the game. I'll have to figure out if there's some way to package assets into a binary file and then load them from there. Hopefully, this isn't what AssetBundle is for, because I'm not willing to drop $1200 on a license quiet yet.

The .NET Method


Since Unity uses .NET, you can also just load files using .NET's file-loading infrastructure. Unlike with WWW, I would probably need to write the logic to load the various types of Unity files, though maybe a simple cast would suffice. This may be an alternative to WWW but again I face the problem of distributing unprotected assets. Perhaps there's a way to bundle them into .dll files and load from there.

No comments:

Post a Comment