Skip to content

Instantly share code, notes, and snippets.

@kimsama
Forked from asus4/AssetBundleManager.cs
Created April 12, 2013 01:18
Show Gist options
  • Save kimsama/5368529 to your computer and use it in GitHub Desktop.
Save kimsama/5368529 to your computer and use it in GitHub Desktop.
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