// 异步加载资源 public bool loadAssetAsync <T>(string fileName, AssetLoadDoneCallback doneCallback, object userData = null) where T : UnityEngine.Object { if (!mInited) { logError("AssetBundleLoader is not inited!"); return(false); } LIST(out List <string> fileNameList); ResourceManager.adjustResourceName <T>(fileName, fileNameList); // 只加载第一个找到的资源,所以不允许有重名的同类资源 int loadedCount = 0; int count = fileNameList.Count; for (int i = 0; i < count; ++i) { string fileNameWithSuffix = fileNameList[i]; if (mAssetToBundleInfo.TryGetValue(fileNameWithSuffix, out AssetInfo asset)) { if (asset.getAssetBundle().loadAssetAsync(fileNameWithSuffix, doneCallback, fileName, userData)) { ++loadedCount; break; } } } UN_LIST(fileNameList); return(loadedCount != 0); }
// 异步加载资源,name为Resources下的相对路径,不带后缀 public bool loadResourcesAsync <T>(string name, AssetLoadDoneCallback doneCallback, object userData) where T : UnityEngine.Object { string path = getFilePath(name); // 如果文件夹还未加载,则先加载文件夹 if (!mLoadedPath.ContainsKey(path)) { mLoadedPath.Add(path, new Dictionary <string, UnityEngine.Object>()); } // 已经加载,则返回true if (mLoadedPath[path].ContainsKey(name)) { // 如果已经加载完毕,则返回,否则继续等待 if (mLoadedPath[path][name] != null) { doneCallback(mLoadedPath[path][name], null, null); } } else { mLoadedPath[path].Add(name, null); mGameFramework.StartCoroutine(loadResourceCoroutine <T>(name, doneCallback, userData)); } return(true); }
public GameObject loadPrefab(string fileWithPath, bool async = false, AssetLoadDoneCallback doneCallback = null) { if (mPrefabList.ContainsKey(fileWithPath)) { return(mPrefabList[fileWithPath].mPrefab); } PrefabInfo info = new PrefabInfo(); info.mFileWithPath = fileWithPath; mPrefabList.Add(fileWithPath, info); if (async) { info.mPrefab = null; info.mState = LOAD_STATE.LS_LOADING; info.mDoneCallback = doneCallback; mResourceManager.loadResourceAsync <GameObject>(fileWithPath, onPrefabLoaded, fileWithPath, false); } else { GameObject prefab = mResourceManager.loadResource <GameObject>(fileWithPath, false); if (prefab == null) { logInfo("can not load prefab : " + fileWithPath); return(null); } info.mPrefab = prefab; info.mState = LOAD_STATE.LS_LOADED; return(prefab); } return(null); }
// 异步加载资源,文件名称,不带后缀,Resources下的相对路径 public bool loadSubAssetAsync <T>(string fileName, AssetLoadDoneCallback callback, object[] userData) where T : UnityEngine.Object { if (!mInited) { logError("AssetBundleLoader is not inited!"); return(false); } List <string> fileNameList = mListPool.newList(out fileNameList); ResourceManager.adjustResourceName <T>(fileName, fileNameList); // 只加载第一个找到的资源,所以不允许有重名的同类资源 int loadedCount = 0; int count = fileNameList.Count; for (int i = 0; i < count; ++i) { string fileNameWithSuffix = fileNameList[i]; if (mAssetToBundleInfo.ContainsKey(fileNameWithSuffix)) { AssetBundleInfo bundleInfo = mAssetToBundleInfo[fileNameWithSuffix].getAssetBundle(); if (bundleInfo.loadSubAssetAsync(ref fileNameWithSuffix, callback, userData, fileName)) { ++loadedCount; break; } } } mListPool.destroyList(fileNameList); return(loadedCount != 0); }
public TPSpriteManager() { mSpriteNameList = new Dictionary <string, Dictionary <string, Sprite> >(); mSpriteList = new Dictionary <UGUIAtlas, Dictionary <string, Sprite> >(); mAtlasNameList = new Dictionary <string, UGUIAtlas>(); mAtlasList = new Dictionary <UGUIAtlas, string>(); mAtlasCallback = onAtlasLoaded; }
public void addCallback(AssetLoadDoneCallback callback, object[] userData, string loadPath) { if (callback != null) { mCallback.Add(callback); mUserData.Add(userData); mLoadPath.Add(loadPath); } }
protected AssetLoadDoneCallback mPrefabCallback; // 预先保存下函数的委托,避免传参时产生GC public ObjectPool() { mInstanceFileList = new Dictionary <string, Dictionary <GameObject, ObjectInfo> >(); mInstanceList = new Dictionary <GameObject, ObjectInfo>(); mAsyncLoadGroup = new List <AsyncLoadGroup>(); mCreateObject = true; mPrefabGroupCallback = onPrefabGroupLoaded; mPrefabCallback = onPrefabLoaded; }
public AssetBundleLoader() { mAssetBundleInfoList = new Dictionary <string, AssetBundleInfo>(); mAssetToBundleInfo = new Dictionary <string, AssetInfo>(); mRequestBundleList = new List <AssetBundleInfo>(); mRequestAssetList = new Dictionary <string, AssetBundleInfo>(); mAssetToAssetBundleInfo = new Dictionary <UnityEngine.Object, AssetBundleInfo>(); mWaitForEndOfFrame = new WaitForEndOfFrame(); mConfigLoadCallback = onAssetConfigDownloaded; }
//--------------------------------------------------------------------------------------------------------------------------------------- protected IEnumerator loadResourceCoroutine <T>(string resName, AssetLoadDoneCallback doneCallback, object userData) where T : UnityEngine.Object { ResourceRequest request = Resources.LoadAsync <T>(resName); yield return(request); string path = getFilePath(resName); mLoadedPath[path][resName] = request.asset; doneCallback(request.asset, null, userData); }
//--------------------------------------------------------------------------------------------------------------------------------------- protected IEnumerator loadResourceCoroutine <T>(string resName, AssetLoadDoneCallback doneCallback) where T : UnityEngine.Object { UnityUtility.logInfo(resName + " start load!", LOG_LEVEL.LL_NORMAL); ResourceRequest request = Resources.LoadAsync <T>(resName); yield return(request); string path = StringUtility.getFilePath(resName); mLoadedPath[path][resName] = request.asset; doneCallback(request.asset, null); UnityUtility.logInfo(resName + " load done!", LOG_LEVEL.LL_NORMAL); }
protected bool mUseAnchor; // 是否启用锚点来自动调节窗口的大小和位置 public LayoutManager() { mUseAnchor = true; mScriptMappingList = new Dictionary <Type, List <int> >(); mScriptRegisteList = new Dictionary <int, Type>(); mLayoutTypeToPath = new Dictionary <int, string>(); mLayoutPathToType = new Dictionary <string, int>(); mLayoutList = new SafeDictionary <int, GameLayout>(); mLayoutAsyncList = new Dictionary <string, LayoutInfo>(); mBackBlurLayoutList = new List <GameLayout>(); mLayoutLoadCallback = onLayoutPrefabLoaded; // 在构造中获取UI根节点,确保其他组件能在任意时刻正常访问 mUGUIRoot = LayoutScript.newUIObject <myUGUICanvas>(null, null, getGameObject(FrameDefine.UGUI_ROOT, true)); }
// 异步加载资源 public bool loadAssetAsync <T>(string fileName, AssetLoadDoneCallback doneCallback) where T : UnityEngine.Object { string fileNameWithSuffix = fileName; adjustResourceName <T>(ref fileNameWithSuffix); // 找不到资源则直接返回 if (!mAssetToBundleInfo.ContainsKey(fileNameWithSuffix)) { return(false); } AssetBundleInfo bundleInfo = mAssetToBundleInfo[fileNameWithSuffix].mParentAssetBundle; bool ret = bundleInfo.loadAssetAsync(ref fileNameWithSuffix, doneCallback); return(ret); }
//-------------------------------------------------------------------------------------------------------------------------------------------- protected IEnumerator loadAssetsUrl(string url, Type assetsType, AssetLoadDoneCallback callback, object[] userData) { logInfo("开始下载: " + url, LOG_LEVEL.LL_HIGH); if (assetsType == typeof(AudioClip)) { yield return loadAudioClipWithURL(url, callback, userData); } else if (assetsType == typeof(Texture2D) || assetsType == typeof(Texture)) { yield return loadTextureWithURL(url, callback, userData); } else if (assetsType == typeof(AssetBundle)) { yield return loadAssetBundleWithURL(url, callback, userData); } }
//-------------------------------------------------------------------------------------------------------------------------------------------- protected IEnumerator loadAssetsUrl(string url, Type assetsType, AssetLoadDoneCallback callback, object userData = null) { log("开始下载: " + url, LOG_LEVEL.HIGH); if (assetsType == Typeof <AudioClip>()) { yield return(loadAudioClipWithURL(url, callback, userData)); } else if (assetsType == Typeof <Texture2D>() || assetsType == Typeof <Texture>()) { yield return(loadTextureWithURL(url, callback, userData)); } else if (assetsType == Typeof <AssetBundle>()) { yield return(loadAssetBundleWithURL(url, callback, userData)); } }
// name是Resources下的相对路径,errorIfNull表示当找不到资源时是否报错提示 public bool loadResourceAsync<T>(string name, AssetLoadDoneCallback doneCallback, object[] userData, bool errorIfNull) where T : UnityEngine.Object { bool ret = false; if (mLoadSource == LOAD_SOURCE.LS_RESOURCES) { ret = mResourceLoader.loadResourcesAsync<T>(name, doneCallback, userData); } else if (mLoadSource == LOAD_SOURCE.LS_ASSET_BUNDLE) { ret = mAssetBundleLoader.loadAssetAsync<T>(name, doneCallback, userData); } if (!ret && errorIfNull) { logError("can not find resource : " + name); } return ret; }
//------------------------------------------------------------------------------------------------------------------------------------- protected IEnumerator loadAssetsFromUrl(string url, Type assetsType, AssetLoadDoneCallback callback, object userData) { WWW www = new WWW(url); yield return(www); if (www.error != null) { // 下载失败 UnityUtility.logInfo("下载失败 : " + url, LOG_LEVEL.LL_FORCE); callback(null, userData); } else { UnityEngine.Object obj = null; if (assetsType == typeof(AudioClip)) { #if UNITY_5_3_5 obj = www.audioClip; #else obj = WWWAudioExtensions.GetAudioClip(www); #endif } else if (assetsType == typeof(Texture2D) || assetsType == typeof(Texture)) { obj = www.texture; } else if (assetsType == typeof(MovieTexture)) { #if UNITY_5_3_5 obj = www.movie; #else obj = WWWAudioExtensions.GetMovieTexture(www); #endif } else if (assetsType == typeof(AssetBundle)) { obj = www.assetBundle; } obj.name = url; callback(obj, userData); } www.Dispose(); www = null; }
// name是Resources下的相对路径,errorIfNull表示当找不到资源时是否报错提示 public bool loadResourceAsync <T>(string name, AssetLoadDoneCallback doneCallback, bool errorIfNull) where T : UnityEngine.Object { bool ret = false; if (mLoadSource == 0) { ret = mResourceLoader.loadResourcesAsync <T>(name, doneCallback); } else if (mLoadSource == 1) { ret = mAssetBundleLoader.loadAssetAsync <T>(name, doneCallback); } if (!ret && errorIfNull) { UnityUtility.logError("can not find resource : " + name); } return(ret); }
protected IEnumerator loadAudioClipWithURL(string url, AssetLoadDoneCallback callback, object[] userData) { UnityWebRequest www = new UnityWebRequest(url); DownloadHandlerAudioClip downloadHandler = new DownloadHandlerAudioClip(url, 0); www.downloadHandler = downloadHandler; yield return www; if (www.error != null) { logInfo("下载失败 : " + url + ", info : " + www.error, LOG_LEVEL.LL_HIGH); callback?.Invoke(null, null, null, userData, url); } else { logInfo("下载成功:" + url, LOG_LEVEL.LL_HIGH); downloadHandler.audioClip.name = url; callback?.Invoke(downloadHandler.audioClip, null, www.downloadHandler.data, userData, url); } www.Dispose(); }
protected IEnumerator loadTextureWithURL(string url, AssetLoadDoneCallback callback, object[] userData) { UnityWebRequest www = new UnityWebRequest(url); www.downloadHandler = new DownloadHandlerTexture(); yield return www; if (www.error != null) { logInfo("下载失败 : " + url + ", info : " + www.error, LOG_LEVEL.LL_HIGH); callback?.Invoke(null, null, null, userData, url); } else { logInfo("下载成功:" + url, LOG_LEVEL.LL_HIGH); Texture2D tex = DownloadHandlerTexture.GetContent(www); tex.name = url; callback?.Invoke(tex, null, www.downloadHandler.data, userData, url); } www.Dispose(); }
protected IEnumerator loadAssetsUrl(string url, Type assetsType, AssetLoadDoneCallback callback, object userData) { WWW www = new WWW(url); yield return(www); if (www.error != null) { // 下载失败 logInfo("下载失败 : " + url + ", info : " + www.error, LOG_LEVEL.LL_FORCE); callback(null, null, userData); } else { UnityEngine.Object obj = null; if (assetsType == typeof(AudioClip)) { #if UNITY_5_3_5 obj = www.audioClip; #elif UNITY_2018_2 || UNITY_2018_1 obj = www.GetAudioClip(); #else obj = WWW.GetAudioClip(www); #endif } else if (assetsType == typeof(Texture2D) || assetsType == typeof(Texture)) { obj = www.texture; } else if (assetsType == typeof(AssetBundle)) { obj = www.assetBundle; } if (obj != null) { obj.name = url; } callback(obj, www.bytes, userData); } www.Dispose(); www = null; }
//异步加载资源的子集 public bool loadSubAssetAsync(ref string fileNameWithSuffix, AssetLoadDoneCallback callback, object[] userData, string loadPath) { // 记录下需要异步加载的资源名 if (!mLoadAsyncList.Contains(mAssetList[fileNameWithSuffix])) { mLoadAsyncList.Add(mAssetList[fileNameWithSuffix]); } mAssetList[fileNameWithSuffix].addCallback(callback, userData, loadPath); // 如果当前资源包还未加载完毕,则需要等待资源包加载完以后才能加载资源 if (mLoadState == LOAD_STATE.LS_UNLOAD) { loadAssetBundleAsync(null, null); } // 如果资源包已经加载,则可以直接异步加载资源 else if (mLoadState == LOAD_STATE.LS_LOADED) { mAssetList[fileNameWithSuffix].loadSubAssetsAsync(); } return(true); }
// 异步加载资源,name为Resources下的相对路径,不带后缀 public bool loadResourcesAsync <T>(string name, AssetLoadDoneCallback doneCallback) where T : UnityEngine.Object { string path = StringUtility.getFilePath(name); // 如果文件夹还未加载,则先加载文件夹 if (!mLoadedPath.ContainsKey(path)) { mLoadedPath.Add(path, new Dictionary <string, UnityEngine.Object>()); } // 已经加载,则返回true if (mLoadedPath[path].ContainsKey(name)) { doneCallback(mLoadedPath[path][name]); } else { StartCoroutine(loadResourceCoroutine <T>(name, doneCallback)); } return(true); }
protected IEnumerator loadAssetBundleWithURL(string url, AssetLoadDoneCallback callback, object userData = null) { UnityWebRequest www = new UnityWebRequest(url); DownloadHandlerAssetBundle downloadHandler = new DownloadHandlerAssetBundle(url, 0); www.downloadHandler = downloadHandler; yield return(www); if (www.error != null) { log("下载失败 : " + url + ", info : " + www.error, LOG_LEVEL.HIGH); callback?.Invoke(null, null, null, userData, url); } else { log("下载成功:" + url, LOG_LEVEL.HIGH); downloadHandler.assetBundle.name = url; callback?.Invoke(downloadHandler.assetBundle, null, www.downloadHandler.data, userData, url); } www.Dispose(); }
//异步加载资源的子集 public bool loadSubAssetAsync(ref string fileNameWithSuffix, AssetLoadDoneCallback callback, string loadPath, object userData = null) { // 记录下需要异步加载的资源名 AssetInfo info = mAssetList[fileNameWithSuffix]; if (!mLoadAsyncList.Contains(info)) { mLoadAsyncList.Add(info); } info.addCallback(callback, userData, loadPath); // 如果当前资源包还未加载完毕,则需要等待资源包加载完以后才能加载资源 if (mLoadState == LOAD_STATE.UNLOAD) { loadAssetBundleAsync(null); } // 如果资源包已经加载,则可以直接异步加载资源 else if (mLoadState == LOAD_STATE.LOADED) { info.loadSubAssetsAsync(); } return(true); }
// 异步加载资源,name为Resources下的相对路径,不带后缀 public bool loadResourcesAsync <T>(string name, AssetLoadDoneCallback doneCallback, object[] userData) where T : Object { string path = getFilePath(name); // 如果文件夹还未加载,则添加文件夹 if (!mLoadedPath.ContainsKey(path)) { mLoadedPath.Add(path, new Dictionary <string, ResoucesLoadInfo>()); } // 已经加载,则返回true if (mLoadedPath[path].ContainsKey(name)) { ResoucesLoadInfo info = mLoadedPath[path][name]; // 资源正在下载,将回调添加到回调列表中,等待下载完毕 if (info.mState == LOAD_STATE.LS_LOADING) { info.addCallback(doneCallback, userData, name); } // 资源已经下载完毕,直接调用回调 else if (info.mState == LOAD_STATE.LS_LOADED) { doneCallback(mLoadedPath[path][name].mObject, mLoadedPath[path][name].mSubObjects, null, userData, name); } } // 还没有加载则开始异步加载 else { ResoucesLoadInfo info; mClassPool.newClass(out info); info.mPath = path; info.mResouceName = name; info.mState = LOAD_STATE.LS_LOADING; info.addCallback(doneCallback, userData, name); mLoadedPath[path].Add(name, info); mGameFramework.StartCoroutine(loadResourceCoroutine <T>(info)); } return(true); }
// 异步加载资源 public bool loadAssetAsync <T>(string fileName, AssetLoadDoneCallback doneCallback) where T : UnityEngine.Object { List <string> fileNameList = adjustResourceName <T>(fileName); // 只加载第一个找到的资源,所以不允许有重名的同类资源 int loadedCount = 0; int count = fileNameList.Count; for (int i = 0; i < count; ++i) { string fileNameWithSuffix = fileNameList[i]; if (mAssetToBundleInfo.ContainsKey(fileNameWithSuffix)) { AssetBundleInfo bundleInfo = mAssetToBundleInfo[fileNameWithSuffix].mParentAssetBundle; bool ret = bundleInfo.loadAssetAsync(ref fileNameWithSuffix, doneCallback); if (ret) { ++loadedCount; break; } } } return(loadedCount != 0); }
//-------------------------------------------------------------------------------------------------------------------------------------------- protected IEnumerator loadAssetsUrl(string url, Type assetsType, AssetLoadDoneCallback callback, object[] userData) { logInfo("开始下载: " + url, LOG_LEVEL.LL_HIGH); WWW www = new WWW(url); yield return(www); if (www.error != null) { // 下载失败 logInfo("下载失败 : " + url + ", info : " + www.error, LOG_LEVEL.LL_HIGH); callback(null, null, null, userData, url); } else { logInfo("下载成功:" + url, LOG_LEVEL.LL_HIGH); UnityEngine.Object obj = null; if (assetsType == typeof(AudioClip)) { obj = www.GetAudioClip(); } else if (assetsType == typeof(Texture2D) || assetsType == typeof(Texture)) { obj = www.texture; } else if (assetsType == typeof(AssetBundle)) { obj = www.assetBundle; } if (obj != null) { obj.name = url; } callback(obj, null, www.bytes, userData, url); } www.Dispose(); }
// 异步加载资源,name为Resources下的相对路径,不带后缀 public bool loadResourcesAsync <T>(string name, AssetLoadDoneCallback doneCallback, object userData = null) where T : Object { string path = getFilePath(name); // 如果文件夹还未加载,则添加文件夹 if (!mLoadedPath.TryGetValue(path, out Dictionary <string, ResourceLoadInfo> resList)) { resList = new Dictionary <string, ResourceLoadInfo>(); mLoadedPath.Add(path, resList); } // 已经加载,则返回true if (resList.TryGetValue(name, out ResourceLoadInfo info)) { // 资源正在下载,将回调添加到回调列表中,等待下载完毕 if (info.mState == LOAD_STATE.LOADING) { info.addCallback(doneCallback, userData, name); } // 资源已经下载完毕,直接调用回调 else if (info.mState == LOAD_STATE.LOADED) { doneCallback(mLoadedPath[path][name].mObject, mLoadedPath[path][name].mSubObjects, null, userData, name); } } // 还没有加载则开始异步加载 else { CLASS(out info); info.mPath = path; info.mResouceName = name; info.mState = LOAD_STATE.LOADING; info.addCallback(doneCallback, userData, name); resList.Add(name, info); mGameFramework.StartCoroutine(loadResourceCoroutine <T>(info)); } return(true); }
public void requestLoadAssetsFromUrl(string url, Type assetsType, AssetLoadDoneCallback callback, object userData) { StartCoroutine(loadAssetsFromUrl(url, assetsType, callback, userData)); }
public void loadAssetsFromUrl(string url, AssetLoadDoneCallback callback, object userData = null) { object[] tempUserData = userData != null ? new object[] { userData } : null; mGameFramework.StartCoroutine(loadAssetsUrl(url, null, callback, tempUserData)); }