UObj RefAssetBundle(string filename) { UObj obj = lstRefAsset.Get(filename); if (obj == null) { //TODO : 后期合成真实的本地路劲 string path = ResourceSetting.ConvertToABPath(filename); /// /// WWW.LoadFromCacheOrDownload 这个方法建议大家以后不要再用了 /// 因为是异步方法,而且还占用内存。 /// AssetBundle asset = AssetBundle.CreateFromFile(path); obj = asset.Load(filename); //释放Compressed资源 asset.Unload(false); //加入引用列表的同时,释放资源 releaseRes(lstRefAsset.Add(filename, obj)); } return(obj); }
IEnumerator AsyncAb(string filename, Action <UObj, string> finished) { yield return(null); bool cached = false; UObj obj = lstRefAsset.Get(filename); cached = obj != null; if (!cached) { //TODO : 后期合成真实的本地路劲 string path = ResourceSetting.ConvertToABPath(filename); AssetBundle asset = AssetBundle.CreateFromFile(path); AssetBundleRequest request = asset.LoadAsync(filename, typeof(UObj)); yield return(request); //加入引用列表的同时,释放资源 releaseRes(lstRefAsset.Add(filename, request.asset)); obj = request.asset; } if (finished != null) { finished(obj, filename); } }
//异步加载Assetbundle上的东西 IEnumerator AsyncAb(string filename, Action <UObj, string> finished) { yield return(null); bool cached = false; AssetBundle asset = lstRefAsset.Get(filename); cached = asset != null; if (!cached) { //TODO : 后期合成真实的本地路劲 string path = ResourceSetting.ConvertToABPath(filename); asset = AssetBundle.CreateFromFile(path); /// add Reference if it does't exist KeyValuePair <string, AssetBundle>[] toBeRm = lstRefAsset.Add(filename, asset); if (toBeRm != null && toBeRm.Length > 0) { foreach (KeyValuePair <string, AssetBundle> rm in toBeRm) { bool useless = Usless.ContainsKey(rm.Key); if (useless) { Usless.Remove(rm.Key); rm.Value.Unload(true); } else { rm.Value.Unload(false); } } } toBeRm = null; AssetBundleRequest request = asset.LoadAsync(filename, typeof(UObj)); yield return(request); if (finished != null) { finished(request.asset, filename); } } }
/// <summary> /// 添加引用, 如果已经有引用的时候,则会放到最近使用的位置,所以最不可能被删除 /// </summary> /// <param name="name">Name.</param> protected override AssetBundle RefAsset(string filename) { bool cached = false; AssetBundle asset = lstRefAsset.Get(filename); cached = asset != null; if (!cached) { //TODO : 后期合成真实的本地路劲 string path = ResourceSetting.ConvertToABPath(filename); /// /// WWW.LoadFromCacheOrDownload 这个方法建议大家以后不要再用了 /// 因为是异步方法,而且还占用内存。 /// asset = AssetBundle.CreateFromFile(path); /// add Reference if it does't exist KeyValuePair <string, AssetBundle>[] toBeRm = lstRefAsset.Add(filename, asset); if (toBeRm != null && toBeRm.Length > 0) { foreach (KeyValuePair <string, AssetBundle> rm in toBeRm) { bool useless = Usless.ContainsKey(rm.Key); if (useless) { Usless.Remove(rm.Key); rm.Value.Unload(true); } else { rm.Value.Unload(false); } } } toBeRm = null; } return(asset); }