コード例 #1
0
        /// <summary>
        /// Starts a load operation for an asset from the given asset bundle.
        /// </summary>
        static public AssetBundleLoadAssetOperation LoadAssetAsync(string assetBundleName, string assetName, System.Type type)
        {
            Log(LogType.Info, "Loading " + assetName + " from " + assetBundleName + " bundle");

            AssetBundleLoadAssetOperation operation = null;

#if UNITY_EDITOR
            if (SimulateAssetBundleInEditor)
            {
                string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
                if (assetPaths.Length == 0)
                {
                    Log(LogType.Error, "There is no asset with name \"" + assetName + "\" in " + assetBundleName);
                    return(null);
                }

                // @TODO: Now we only get the main object from the first asset. Should consider type also.
                UnityEngine.Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]);
                operation = new AssetBundleLoadAssetOperationSimulation(target);
            }
            else
#endif
            {
                assetBundleName = RemapVariantName(assetBundleName);
                LoadAssetBundle(assetBundleName);
                operation = new AssetBundleLoadAssetOperationFull(assetBundleName, assetName, type);

                m_InProgressOperations.Add(operation);
            }

            return(operation);
        }
コード例 #2
0
        /// <summary>
        /// 下载UI --assetBundle
        /// </summary>
        /// <param name="assetBundleName"></param>
        /// <param name="assetName"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        static public AssetBundleLoadAssetOperation LoadAssetAsync(string assetBundleName, string assetName, System.Type type)
        {
            if (Debug.developerConsoleVisible)
            {
                Log(LogType.Info, "Loading " + assetName + " from " + assetBundleName + " bundle");
            }

            AssetBundleLoadAssetOperation operation = null;

            //#if UNITY_EDITOR
            //            if (SimulateAssetBundleInEditor)
            //            {
            //                string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
            //                if (assetPaths.Length == 0)
            //                {
            //                    Debug.LogError("There is no asset with name \"" + assetName + "\" in " + assetBundleName);
            //                    return null;
            //                }

            //                // @TODO: Now we only get the main object from the first asset. Should consider type also.
            //                Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]);
            //                operation = new AssetBundleLoadAssetOperationSimulation(target);
            //            }
            //            else
            //#endif
            {
                LoadAssetBundle(assetBundleName);
                operation = new AssetBundleLoadAssetOperationFull(assetBundleName, assetName, type);

                m_InProgressOperations.Add(operation);
            }

            return(operation);
        }
コード例 #3
0
        // Load asset from the given assetBundle.
        static public AssetBundleLoadAssetOperation LoadAssetAsync(string assetBundleName, string assetName, System.Type type)
        {
            Log(LogType.Info, "Loading " + assetName + " from " + assetBundleName + " bundle");

            AssetBundleLoadAssetOperation operation = null;

        #if UNITY_EDITOR
            if (SimulateAssetBundleInEditor)
            {
                string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
                if (assetPaths.Length == 0)
                {
                    Debug.LogError("There is no asset with name \"" + assetName + "\" in " + assetBundleName);
                    return(null);
                }

                Object target = AssetDatabase.LoadAssetAtPath(assetPaths[0], type);
                operation = new AssetBundleLoadAssetOperationSimulation(target);
            }
            else
        #endif
            {
                assetBundleName = RemapVariantName(assetBundleName);
                LoadAssetBundle(assetBundleName);
                operation = new AssetBundleLoadAssetOperationFull(assetBundleName, assetName, type);

                m_InProgressOperations.Add(operation);
            }

            return(operation);
        }
        private static IEnumerator FetchAssetBundle <T>(AssetBundleLoadAssetOperation operation,
                                                        IObserver <T> observer, IProgress <float> reportProgress, CancellationToken cancel)
            where T : UnityEngine.Object
        {
            while (!operation.IsDone() && !cancel.IsCancellationRequested)
            {
                if (reportProgress != null)
                {
                    try
                    {
                        reportProgress.Report(operation.GetProgress());
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                        yield break;
                    }
                }

                yield return(null);
            }

            try
            {
                if (cancel.IsCancellationRequested)
                {
                    yield break;
                }

                if (reportProgress != null)
                {
                    try
                    {
                        reportProgress.Report(operation.GetProgress());
                    }
                    catch (Exception ex)
                    {
                        observer.OnError(ex);
                        yield break;
                    }
                }

                if (!string.IsNullOrEmpty(operation.GetError()))
                {
                    observer.OnError(new AssetBundleLoadAssetOperationErrorException(operation));
                }
                else
                {
                    observer.OnNext(operation.GetAsset <T>());
                    observer.OnCompleted();
                }
            }
            catch (Exception ex)
            {
                observer.OnError(ex);
            }
        }
コード例 #5
0
        // Load asset from the given assetBundle.
        public static AssetBundleLoadAssetOperation LoadAssetAsync(string assetBundleName, string assetName, System.Type type)
        {
            Log(LogType.Info, "Loading " + assetName + " from " + assetBundleName + " bundle");

            AssetBundleLoadAssetOperation operation = null;

            assetBundleName = RemapVariantName(assetBundleName);
            LoadAssetBundle(assetBundleName);
            operation = new AssetBundleLoadAssetOperationFull(assetBundleName, assetName, type);
            m_InProgressOperations.Add(operation);
            return(operation);
        }
コード例 #6
0
        IEnumerator LoadAssetAsyncRoutine <TAsset>(
            string assetBundleName,
            string assetName,
            Action <TAsset> onLoad)
            where TAsset : UnityEngine.Object
        {
            AssetBundleLoadAssetOperation loadAsset = LoadAssetAsync <TAsset>(
                assetBundleName, assetName);

            yield return(loadAsset);

            onLoad?.Invoke(loadAsset.GetAsset <TAsset>());
        }
コード例 #7
0
        //unicon7 : Load asset from the given assetBundle.
        static public AssetBundleLoadAssetOperation LoadAssetFromCachedAssetBundle(string assetBundleName, string assetName, System.Type type)
        {
            Log(LogType.Info, "Loading " + assetName + " from " + assetBundleName + " bundle");

            AssetBundleLoadAssetOperation operation = null;

#if UNITY_EDITOR
            if (SimulateAssetBundleInEditor)
            {
                string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
                if (assetPaths.Length == 0)
                {
                    Debug.LogWarning("There is no asset with name \"" + assetName + "\" in " + assetBundleName);
                    return(null);
                }

                // @TODO: Now we only get the main object from the first asset. Should consider type also.
                Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]);
                operation = new AssetBundleLoadAssetOperationSimulation(target);
            }
            else
#endif
            {
                assetBundleName = RemapVariantName(assetBundleName);
                string            error             = string.Empty;
                LoadedAssetBundle loadedAssetBundle = GetLoadedAssetBundle(assetBundleName, out error);
                if (string.IsNullOrEmpty(error) == false)
                {
                    Debug.LogError("LoadAssetFromCachedAssetBundle:" + error);
                    return(null);
                }

                Object target = loadedAssetBundle.m_AssetBundle.LoadAsset(assetName);
                if (target == null)
                {
                    Debug.LogError("LoadAssetFromCachedAssetBundle: " + assetName + " not found in " + assetBundleName);
                    return(null);
                }

                operation = new AssetBundleLoadAssetOperationSimulation(target);

                //m_InProgressOperations.Add (operation);//unicon7 : hmm~
            }

            return(operation);
        }
コード例 #8
0
        // Load asset from the given assetBundle.
        /// <summary>
        /// 异步加载资源
        /// </summary>
        /// <param name="assetBundleName">资源包名</param>
        /// <param name="assetName">资源名</param>
        /// <param name="type">资源类型</param>
        /// <returns></returns>
        static public AssetBundleLoadAssetOperation LoadAssetAsync(string assetBundleName, string assetName,
                                                                   System.Type type)
        {
            Log(LogType.Info, "Loading " + assetName + " from " + assetBundleName + " bundle");

            AssetBundleLoadAssetOperation operation = null;

#if UNITY_EDITOR
            // 是否是编辑模式,且为模式
            if (SimulateAssetBundleInEditor)
            {
                string[] assetPaths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(assetBundleName, assetName);
                if (assetPaths.Length == 0)
                {
                    Debug.LogError("There is no asset with name \"" + assetName + "\" in " + assetBundleName);
                    return(null);
                }

                // @TODO: Now we only get the main object from the first asset. Should consider type also.
                // 本地获得资源依赖总览
                Object target = AssetDatabase.LoadMainAssetAtPath(assetPaths[0]);
                // 创建模拟请求
                operation = new AssetBundleLoadAssetOperationSimulation(target);
            }
            else
#endif
            {
                // 切换Variant
                assetBundleName = RemapVariantName(assetBundleName);
                // 下载资源,以及其依赖项
                LoadAssetBundle(assetBundleName);
                // 创建全资源请求
                operation = new AssetBundleLoadAssetOperationFull(assetBundleName, assetName, type);

                // 添加到等待处理
                m_InProgressOperations.Add(operation);
            }

            return(operation);
        }
コード例 #9
0
ファイル: AssetBundleManager.cs プロジェクト: K07H/The-Forest
 public static bool LoadAssetAsyncCallback <T>(string assetBundleName, string assetName, Func <T, bool> onAssetLoaded) where T : UnityEngine.Object
 {
     AssetBundleManager.Initialize();
     if (AssetBundleManager.m_Instance)
     {
         if (string.IsNullOrEmpty(assetBundleName) || string.IsNullOrEmpty(assetName))
         {
             Debug.LogError("## Empty bundle or asset name: assetBundleName=" + assetBundleName + ", assetName=" + assetName);
             return(false);
         }
         AssetBundleLoadAssetOperation assetBundleLoadAssetOperation = AssetBundleManager.LoadAssetAsync(assetBundleName, assetName, typeof(T));
         if (assetBundleLoadAssetOperation != null)
         {
             if (!AssetBundleManager.m_PendingRoutines.ContainsKey(onAssetLoaded))
             {
                 Coroutine value = AssetBundleManager.m_Instance.StartCoroutine(AssetBundleManager.LoadAssetAsyncCallbackRoutine <T>(assetBundleName, assetBundleLoadAssetOperation, onAssetLoaded));
                 AssetBundleManager.m_PendingRoutines.Add(onAssetLoaded, value);
             }
             return(true);
         }
     }
     return(false);
 }
コード例 #10
0
ファイル: AssetBundleManager.cs プロジェクト: K07H/The-Forest
        private static IEnumerator LoadAssetAsyncCallbackRoutine <T>(string assetBundleName, AssetBundleLoadAssetOperation request, Func <T, bool> onAssetLoaded) where T : UnityEngine.Object
        {
            yield return(AssetBundleManager.m_Instance.StartCoroutine(request));

            if (AssetBundleManager.m_LoadedAssetBundles.ContainsKey(assetBundleName))
            {
                T asset = request.GetAsset <T>();
                if (onAssetLoaded == null || !onAssetLoaded(asset))
                {
                    AssetBundleManager.UnloadAssetBundle(assetBundleName);
                }
            }
            else
            {
                onAssetLoaded((T)((object)null));
            }
            if (AssetBundleManager.m_PendingRoutines.ContainsKey(onAssetLoaded))
            {
                AssetBundleManager.m_PendingRoutines.Remove(onAssetLoaded);
            }
            yield break;
        }
 public AssetBundleLoadAssetOperationErrorException(AssetBundleLoadAssetOperation operation)
 {
     Operation       = operation;
     RawErrorMessage = operation.GetError();
 }