Skip to content

Instantly share code, notes, and snippets.

@asus4
Created May 14, 2012 04:24
Show Gist options
  • Save asus4/2691788 to your computer and use it in GitHub Desktop.
Save asus4/2691788 to your computer and use it in GitHub Desktop.
Manage Assetbundles for unity3d
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;
}
}
}
@asus4
Copy link
Author

asus4 commented May 22, 2012

How To Use

// マネージャーに参照を追加
AssetBundleManager.AddAssetBundle(www);

// マネージャーから参照を削除
AssetBundleManager.DeleteAssetBundle(url);
// マネージャーないで、同アセットバンドルへの参照数が0になったら、アンロードします。

// アセットバンドルを取得
AssetBundleManager.GetAssetBundle(url);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment