/// <summary> /// 异步加载场景 /// </summary> /// <param name="levelName">场景名称</param> /// <param name="isAdditive">是否叠加</param> /// <param name="funcAfter">完成回调</param> /// <returns></returns> private IEnumerator LoadLevelAsync(string levelName, bool isAdditive, Action <ILoadAsset> funcAfter = null) { // LogSystem.Log("Start to load scene " + levelName + " at frame " + Time.frameCount); ILoadAsset _Node = new ILoadAsset(); PrefabPathMap ppm = CheckPrefabBundle(levelName, true); if (ppm.IsHasBundle) { AssetBundleLoadOperation request = AssetBundleManager.LoadLevelAsync(ppm.BundleName, levelName, isAdditive); yield return(StartCoroutine(request)); AddBundleToUnloadList(ppm.BundleName, false); } else { AsyncOperation operation = SceneManager.LoadSceneAsync(levelName, isAdditive ? LoadSceneMode.Additive : LoadSceneMode.Single); yield return(operation); } GC.Collect(); // LogSystem.Log("Finish loading scene " + levelName + " at frame " + Time.frameCount); if (funcAfter != null) { funcAfter(_Node); } }
/// <summary> /// 同步加载Texture2D素材,不缓存,也不需要缓存 /// </summary> /// <param name="assetPath">素材地址</param> /// <param name="type">类型</param> /// <returns></returns> private Object Inner_LoadObjectSync(string assetPath, Type type) { Object uObj; PrefabPathMap ppm = CheckPrefabBundle(assetPath); if (ppm.IsHasBundle) { AssetBundle _Bundle = AssetBundleManager.LoadAssetBundleFromFile(ppm.BundleName); uObj = _Bundle.LoadAsset(ppm.AssetPath, type); AddBundleToUnloadList(ppm.BundleName, false); } else { #if UNITY_EDITOR uObj = AssetDatabase.LoadAssetAtPath(ppm.AssetPath, type); #else uObj = Resources.Load(ppm.ResourcePath, type); #endif } if (type == typeof(RuntimeAnimatorController)) { CacheController(uObj, assetPath); } return(uObj); }
/// <summary> /// 加载GameObect,并且缓存 /// </summary> /// <param name="assetPath">资源路径</param> /// <param name="cachePool">缓存池</param> /// <param name="isCache">是否为缓存</param> /// <returns></returns> private Object Inner_LoadGameObjectSync(string assetPath, SpawnPool cachePool, bool isCache = false) { // LogSystem.Debug("Inner_LoadObjectSync: " + assetPath); Object uObj; PrefabPathMap ppm = CheckPrefabBundle(assetPath); if (ppm.IsHasBundle) { AssetBundle _Bundle = AssetBundleManager.LoadAssetBundleFromFile(ppm.BundleName); uObj = _Bundle.LoadAsset(ppm.AssetPath, typeof(GameObject)); if (null != uObj) { AddBundleToUnloadList(ppm.BundleName, isCache); } } else { #if UNITY_EDITOR uObj = AssetDatabase.LoadAssetAtPath(ppm.AssetPath, typeof(GameObject)); #else uObj = Resources.Load(ppm.ResourcePath, typeof(GameObject)); #endif } if (null != uObj) { CacheGameObject(uObj, assetPath, cachePool); if (!isCache) { uObj = cachePool.Spawn(assetPath).gameObject; } } return(uObj); }
/// <summary> /// 检查prefab是否有更新的bundle /// </summary> /// <param name="prefabName"></param> /// <param name="isScene"></param> private static PrefabPathMap CheckPrefabBundle(string prefabName, bool isScene = false) { if (!m_PrefabPathMap.ContainsKey(prefabName)) { string assetBundleName; PrefabPathMap ppm = new PrefabPathMap(); if (!isScene) { assetBundleName = ("Assets/" + prefabName + ".bytes").ToLower(); } else { assetBundleName = ("Assets/Scene/" + prefabName + ".unity.bytes").ToLower(); } ppm.BundleName = assetBundleName; if (!isScene) { ppm.AssetPath = "Assets/" + prefabName; ppm.ResourcePath = prefabName.Substring(0, prefabName.LastIndexOf(".")); } // 如果外部有,则地址为外部地址. if (File.Exists(ViewDefine.DataPathSync + assetBundleName)) { ppm.IsHasBundle = true; } else { // 外部没有,则地址为StreamingAssets地址 ppm.IsHasBundle = false; } m_PrefabPathMap.Add(prefabName, ppm); return(ppm); } return(m_PrefabPathMap[prefabName]); }
// /// <summary> // /// 加载Controller,并且缓存 // /// </summary> // /// <param name="assetPath">资源路径</param> // /// <returns></returns> // private Object LoadControllerSync(string assetPath) // { // // LogSystem.Debug("LoadControllerSync: " + assetPath); // Object uObj; // PrefabPathMap ppm = CheckPrefabBundle(assetPath); // if (ppm.IsHasBundle) // { // AssetBundle _Bundle = AssetBundleManager.LoadAssetBundleFromFile(ppm.BundleName); // uObj = _Bundle.LoadAsset(ppm.AssetPath, typeof(RuntimeAnimatorController)); // // AddBundleToUnloadList(ppm.BundleName, false); // } // else // { //#if UNITY_EDITOR // uObj = AssetDatabase.LoadAssetAtPath(ppm.AssetPath, typeof(RuntimeAnimatorController)); //#else // uObj = Resources.Load(ppm.ResourcePath, typeof(RuntimeAnimatorController)); //#endif // } // CacheController(uObj, assetPath); // return uObj; // } /// <summary> /// 异步加载GameObject资源 /// </summary> /// <param name="assetPath">资源路径</param> /// <param name="cachePool">缓存池</param> /// <param name="finishCall">完成回调</param> /// <param name="isCache">是否只是缓存</param> /// <returns></returns> private IEnumerator Inner_LoadGameObjectAsync(string assetPath, SpawnPool cachePool, Action <Object> finishCall, bool isCache = false) { // LogSystem.Debug("Inner_LoadObjectAsync: " + assetPath); Object uObj; PrefabPathMap ppm = CheckPrefabBundle(assetPath); if (ppm.IsHasBundle) { AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetAsync(ppm.BundleName, ppm.AssetPath, typeof(GameObject)); yield return(StartCoroutine(request)); uObj = request.GetAsset <GameObject>(); AddBundleToUnloadList(ppm.BundleName, isCache); } else { #if UNITY_EDITOR uObj = AssetDatabase.LoadAssetAtPath(ppm.AssetPath, typeof(GameObject)); yield return(null); #else ResourceRequest request = Resources.LoadAsync(ppm.ResourcePath, typeof(GameObject)); //yield return request; //0415 王国庆 必须要判断isDone,否者可能就变成同步调用了 while (!request.isDone) { yield return(0); } uObj = request.asset; #endif } CacheGameObject(uObj, assetPath, cachePool); if (!isCache) { uObj = cachePool.Spawn(assetPath).gameObject; } if (finishCall != null) { finishCall(uObj); } }
/// <summary> /// 异步加载Texture2D资源 /// </summary> /// <param name="assetPath">资源地址</param> /// <param name="type">类型</param> /// <param name="finishCall">加载完成回调</param> /// <returns></returns> private IEnumerator Inner_LoadObjectAsync(string assetPath, Type type, Action <Object> finishCall) { Object uObj; PrefabPathMap ppm = CheckPrefabBundle(assetPath); if (ppm.IsHasBundle) { AssetBundleLoadAssetOperation request = AssetBundleManager.LoadAssetAsync(ppm.BundleName, ppm.AssetPath, type); yield return(StartCoroutine(request)); uObj = request.GetAsset <Object>(); AddBundleToUnloadList(ppm.BundleName, false); } else { #if UNITY_EDITOR uObj = AssetDatabase.LoadAssetAtPath(ppm.AssetPath, type); yield return(null); #else ResourceRequest request = Resources.LoadAsync(ppm.ResourcePath, type); //yield return request; //0415 王国庆 必须要判断isDone,否者可能就变成同步调用了 while (!request.isDone) { yield return(0); } uObj = request.asset; #endif } if (type == typeof(RuntimeAnimatorController)) { CacheController(uObj, assetPath); } if (finishCall != null) { finishCall(uObj); } }