/// <summary> /// 资源加载(编辑器非AB模式AssetDatabase同步加载,否则AB异步加载) /// </summary> /// <returns>asyncAsset</returns> public static AsyncAsset Load(this AssetManager self, string path, Action <bool, Object> complete, Action <bool, AsyncAsset> action = null, bool async = true) { AsyncAsset asyncAsset = null; #if UNITY_EDITOR && !AB_MODE if (complete != null) { complete(true, UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/" + path, typeof(Object))); } #else if (async) { asyncAsset = self.AssetBundleAsyncLoad(self.url + path, (bResult, asset) => { if (null != complete && bResult && null != asset) { complete(bResult, asset.mainAsset); } }, action); } else { asyncAsset = self.AssetBundleLoad(self.url + path); if (null != complete && null != asyncAsset) { complete(string.IsNullOrEmpty(asyncAsset.error), asyncAsset.mainAsset); } } #endif return(asyncAsset); }
/// <summary> /// 卸载指定资源 /// </summary> /// <param name="asset"></param> /// <param name="unloadAllLoadedObjects"></param> public void UnloadAssets(AsyncAsset asset, bool unloadAllLoadedObjects) { if (null == asset) { return; } if (m_complete.ContainsKey(asset.url) && asset == m_complete[asset.url]) { asset.Unload(unloadAllLoadedObjects); m_complete.Remove(asset.url); } else if (m_loading.ContainsKey(asset.url) && asset == m_loading[asset.url]) { asset.Unload(unloadAllLoadedObjects); m_loading.Remove(asset.url); } for (int i = 0; i < m_queue.Count; ++i) { if (m_queue[i].asyncAsset == asset) { m_queue.RemoveAt(i); break; } } }
/// <summary> /// AssetBundle加载 /// </summary> /// <param name="url"></param> /// <returns></returns> public AsyncAsset AssetBundleLoad(string path) { // 依赖加载 string assetBundleName = "data/" + GetAssetBundleName(path); var data = m_getDependentAsset(assetBundleName); AssetsBundle[] dependent = new AssetsBundle[data.Count]; for (int i = 0; i < data.Count; ++i) { dependent[i] = (AssetsBundle)AssetBundleLoad(path.Replace(assetBundleName, data[i])); } AsyncAsset async = null; if (m_complete.ContainsKey(path)) { async = m_complete[path]; } else { if (m_loading.ContainsKey(path)) { async = m_loading[path]; } else { async = new AssetsBundle(path); async.AsyncLoad(); } while (!async.isDone) { } async.Complete(); m_complete.Add(async.url, async); // 计算依赖关系 if (dependent != null) { for (int i = 0; i < dependent.Length; ++i) { dependent[i].AddDependentSelf((AssetsBundle)async); ((AssetsBundle)async).AddSelfDependent(dependent[i]); } } } return(async); }
/// <summary> /// Lua加载器 /// </summary> /// <param name="fileName"></param> /// <returns></returns> byte[] Loader(ref string fileName) { byte[] result = new byte[0]; if (awake) { #if UNITY_EDITOR && !AB_MODE string path = System.IO.Directory.GetCurrentDirectory() + "/Assets/data/lua/" + fileName.Replace(".", "/").ToLower() + ".txt"; result = System.IO.File.ReadAllBytes(path); #else string path = UnityAsset.AssetManager.instance.url + "data/lua/" + fileName.Replace(".", "/").ToLower() + ".txt"; UnityAsset.AsyncAsset asyncAsset = UnityAsset.AssetManager.instance.AssetBundleLoad(path); result = System.Text.Encoding.Default.GetBytes(asyncAsset.mainAsset.ToString()); UnityAsset.AssetManager.instance.UnloadAssets(asyncAsset, true); #endif } else { #if UNITY_EDITOR && !AB_MODE string path = System.IO.Directory.GetCurrentDirectory() + "/Assets/data/lua/" + fileName.Replace(".", "/").ToLower() + ".txt"; result = System.IO.File.ReadAllBytes(path); #else if (scheduleEvent == null) { string path = UnityAsset.AssetManager.instance.url + "data/lua/" + fileName.Replace(".", "/").ToLower() + ".txt"; UnityAsset.AsyncAsset asyncAsset = UnityAsset.AssetManager.instance.AssetBundleLoad(path); result = System.Text.Encoding.Default.GetBytes(asyncAsset.mainAsset.ToString()); UnityAsset.AssetManager.instance.UnloadAssets(asyncAsset, true); } else { assetPath = UnityAsset.AssetManager.instance.url + "data/lua/" + fileName.Replace(".", "/").ToLower() + ".txt"; while (true) { System.Threading.Thread.Sleep(30); if (bytes != null) { break; } } result = bytes; bytes = null; } #endif } return(result); }
/// <summary> /// AssetBundle资源加载 /// </summary> /// <param name="path"></param> /// <param name="action"></param> /// <returns></returns> private AsyncAsset AssetBundleAsyncLoadWithoutDependent(string path, Action <bool, AsyncAsset> action) { AsyncAsset async = null; if (m_complete.ContainsKey(path)) { async = m_complete[path]; } else { if (m_loading.ContainsKey(path)) { async = m_loading[path]; } else { async = new AssetsBundle(path); m_loading.Add(async.url, async); } } m_queue.Add(new AsyncAssetBundle(async, action)); return(async); }
/// <summary> /// Resource异步加载 /// </summary> /// <param name="path"></param> /// <param name="action"></param> /// <returns></returns> public AsyncAsset ResourceAsyncLoad(string path, Action <bool, AsyncAsset> complete) { AsyncAsset async = null; if (m_complete.ContainsKey(path)) { async = m_complete[path]; } else { if (m_loading.ContainsKey(path)) { async = m_loading[path]; } else { async = new Resource(path); m_loading.Add(async.url, async); } } m_queue.Add(new AsyncAssetBundle(async, complete)); return(async); }
/// <summary> /// 构造 /// </summary> /// <param name="asyncAsset"></param> /// <param name="action"></param> public AsyncAssetBundle(AsyncAsset asyncAsset, Action <bool, AsyncAsset> action) { m_asyncAsset = asyncAsset; m_action = action; }