Created
May 14, 2012 04:24
-
-
Save asus4/2691788 to your computer and use it in GitHub Desktop.
Manage Assetbundles for unity3d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
public class AssetBundleManager { | |
class AssetReference : IDisposable { | |
int count; | |
public AssetBundle bundle; | |
public AssetReference(AssetBundle bundle) { | |
this.bundle = bundle; | |
this.count = 0; | |
} | |
public int Increase() { | |
return ++count; | |
} | |
public int Decrease() { | |
--count; | |
if(count <= 0) { | |
this.Dispose(); | |
} | |
return count; | |
} | |
public void Dispose() { | |
if(bundle!=null) { | |
bundle.Unload(false); | |
bundle = null; | |
} | |
} | |
} | |
static Dictionary<string,AssetReference> bundles = new Dictionary<string,AssetReference>(); | |
static public void AddAssetBundle(string url, AssetBundle bundle){ | |
if(bundles.ContainsKey(url)) { | |
bundles[url].Increase(); | |
} | |
else { | |
bundles.Add(url, new AssetReference(bundle)); | |
} | |
} | |
static public void AddAssetBundle(WWW www) { | |
AddAssetBundle(www.url, www.assetBundle); | |
} | |
static public void DeleteAssetBundle(string url) { | |
if(bundles.ContainsKey(url)) { | |
if(bundles[url].Decrease() <= 0) { | |
bundles.Remove(url); | |
} | |
} | |
} | |
static public void DeleteAssetBundle(WWW www) { | |
DeleteAssetBundle(www.url); | |
} | |
static public AssetBundle GetAssetBundle(string url) { | |
if(bundles.ContainsKey(url)) { | |
return bundles[url].bundle; | |
} | |
else { | |
return null; | |
} | |
} | |
static public bool HasAssetBundle(string url) { | |
if(bundles.ContainsKey(url)) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How To Use