Example #1
0
        public static void Load(string name)
        {
            string path = PathRouter.ScenesFolder + name;

            using (var timer = LoadTimer.Start <SceneLoader>(path))
            {
                if (null == SceneResource.Current || SceneResource.Current.Path != path)
                {
                    SceneResource sceneResource = new SceneResource(path);
                    try
                    {
                        if (Application.isEditor && IsDev)
                        {
                            BuildSettingsCheck(path);
                            SceneManager.LoadScene(path);
                        }
                        else
                        {
                            string bundleName        = path + ".unity" + PathRouter.AssetBundleSuffix;
                            var    assetBundleHandle = AssetBundleLoader.Load(bundleName);
                            SceneManager.LoadScene(path);
                            sceneResource.AddDependency(assetBundleHandle);
                        }

                        if (null != SceneResource.Current)
                        {
                            SceneResource.Current.Dispose();
                            AssetSystem.Instance.GarbageCollect();
                        }

                        SceneResource.Current = sceneResource;
                        AssetSystem.Instance.AddAsset(sceneResource);
                    }
                    catch (Exception e)
                    {
                        sceneResource.Dispose();
                        throw new ApplicationException("Error when loading Scene:" + path, e);
                    }
                }
            }
        }
Example #2
0
        protected override async Task AsyncRun()
        {
            SceneResource sceneResource = new SceneResource(Url);

            try
            {
                if (Application.isEditor && IsDev)
                {
                    BuildSettingsCheck(Url);
                    await LoadSceneAsync();
                }
                else
                {
                    string bundleName        = Url + ".unity" + PathRouter.AssetBundleSuffix;
                    var    assetBundleHandle = await AssetBundleLoader.AsyncLoad(
                        bundleName, v => Progress = v * 0.5f);

                    await LoadSceneAsync(0.5f);

                    sceneResource.AddDependency(assetBundleHandle);
                }

                if (null != SceneResource.Current)
                {
                    SceneResource.Current.Dispose();
                    AssetSystem.Instance.GarbageCollect();
                }

                SceneResource.Current = sceneResource;
                AssetSystem.Instance.AddAsset(sceneResource);
                Finish(sceneResource);
            }
            catch (Exception e)
            {
                Finish(null);
                sceneResource.Dispose();
                throw new ApplicationException("Error when loading Scene:" + Url, e);
            }
        }
Example #3
0
        protected override async Task AsyncRun()
        {
            PrefabResource prefabResource = new PrefabResource(Url);

            try
            {
                if (IsDev)
                {
                    prefabResource = LoadInEditor(Url, prefabResource);
                    Progress       = 1f;
                }
                else
                {
                    string bundleName        = Url + ".prefab" + PathRouter.AssetBundleSuffix;
                    var    assetBundleHandle = await AssetBundleLoader.AsyncLoad(
                        bundleName, v => Progress = v);

                    prefabResource.prefabObject = assetBundleHandle.Bundle.LoadAsset(
                        Path.GetFileNameWithoutExtension(Url));
                    if (null == prefabResource.prefabObject)
                    {
                        throw new ApplicationException(string.Format(
                                                           "AssetBundle.LoadAsset({0}) => null, Bundle:{1}",
                                                           Path.GetFileNameWithoutExtension(Url),
                                                           assetBundleHandle.Path));
                    }
                    prefabResource.AddDependency(assetBundleHandle);
                }
                AssetSystem.Instance.AddAsset(prefabResource);
                Finish(prefabResource);
            }
            catch (Exception e)
            {
                Finish(null);
                prefabResource.Dispose();
                throw new ApplicationException("Error when loading Prefab:" + Url, e);
            }
        }
        private IEnumerator _LoadCoroutine(string path, LoaderMode loaderMode, string package,
                                           LoadSceneMode loadSceneMode)
        {
            string scenePath = ResourceModuleConfig.GameResourcesDir + "/" + path;
            object getAsset  = null;

            if (ResManager.IsEdiotrMode && Application.isEditor)
            {
#if UNITY_EDITOR
                var allScenes = UnityEditor.EditorBuildSettings.scenes;
                UnityEditor.EditorBuildSettingsScene buildScene = null;
                foreach (var scene in allScenes)
                {
                    if (scene.path == scenePath)
                    {
                        buildScene = scene;
                        break;
                    }
                }

                if (buildScene == null || !buildScene.enabled)
                {
                    Debug.LogError("EditorMode must add scene to build settings and active:" + path);
                    OnFinish(null);
                    yield break;
                }

                ////编辑器状态下模拟异步加载延迟等待指定秒数
                //if (loaderMode == LoaderMode.Async)
                //{
                //    yield return new WaitForSecondsRealtime(ResManager.EditorModeLoadDelay);
                //}
#else
                Debug.LogErrorFormat("`IsEditorLoadAsset` is Unity Editor only");
#endif
            }
            else
            {
                DateTime beginTime  = DateTime.Now;
                string   bundlePath = string.IsNullOrEmpty(package) ? path : package;
                _bundleLoader =
                    AssetBundleLoader.Load(bundlePath + ResourceModuleConfig.AssetBundleExt, null, loaderMode);

                while (!_bundleLoader.IsCompleted)
                {
                    if (IsReadyDisposed) // 中途释放
                    {
                        _bundleLoader.Release();
                        OnFinish(null);
                        yield break;
                    }

                    this.Progress = _bundleLoader.Progress / 2f;
                    yield return(null);
                }

                if (!_bundleLoader.IsSuccess || !_bundleLoader.Bundle.isStreamedSceneAssetBundle)
                {
                    Debug.LogErrorFormat("[AssetFileLoader]Load BundleLoader Failed(Error) when Finished: {0}", path);
                    _bundleLoader.Release();
                    OnFinish(null);
                    yield break;
                }

                ResManager.LogLoadTime("AssetFileBridge", loaderMode, path, beginTime);
            }

            this.Progress = 0.5f;
            if (loaderMode == LoaderMode.Async)
            {
                var asyncOp = SceneManager.LoadSceneAsync(scenePath, loadSceneMode);
                while (!asyncOp.isDone)
                {
                    this.Progress = 0.5f + asyncOp.progress / 2f;
                    yield return(null);
                }
            }
            else
            {
                SceneManager.LoadScene(scenePath, loadSceneMode);
            }

            this.Progress = 1f;
            getAsset      = true;

            OnFinish(getAsset);

            //加载完立即释放Loader
            ReleaseImmediate();
        }
Example #5
0
        private IEnumerator _LoadCoroutine(string path, LoaderMode loaderMode, string package, Type type, bool loadAll)
        {
            if (type == null)
            {
                type = typeof(UnityEngine.Object);
            }
            object getAsset = null;

            if (ResManager.IsEdiotrMode && Application.isEditor)
            {
#if UNITY_EDITOR
                string projPath = ResourceModuleConfig.BundleResoucesDir + "/" + path;
                if (!string.IsNullOrEmpty(package))
                {
                    string bundleName = UnityEditor.AssetDatabase.GetImplicitAssetBundleName(projPath);
                    if (bundleName != package + ResourceModuleConfig.AssetBundleExt)
                    {
                        Debug.LogErrorFormat("Asset is BundleName set error: {0} {1}\n{2}", package, bundleName, projPath);
                    }
                }

                if (loadAll)
                {
                    getAsset = UnityEditor.AssetDatabase.LoadAllAssetRepresentationsAtPath(projPath);
                }
                else
                {
                    getAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(projPath, type);
                }

                if (getAsset == null)
                {
                    Debug.LogErrorFormat("Asset is NULL(from {0} Folder): {1}", ResourceModuleConfig.BundleResoucesDir, path);
                }

                //编辑器状态下模拟异步加载延迟等待指定秒数
                if (loaderMode == LoaderMode.Async)
                {
                    yield return(new WaitForSecondsRealtime(ResManager.EditorModeLoadDelay));
                }
#else
                Debug.LogErrorFormat("`IsEditorLoadAsset` is Unity Editor only");
#endif
            }
            else
            {
                DateTime beginTime  = DateTime.Now;
                string   bundlePath = string.IsNullOrEmpty(package) ? path : package;
                _bundleLoader = AssetBundleLoader.Load(bundlePath + ResourceModuleConfig.AssetBundleExt, null, loaderMode);

                while (!_bundleLoader.IsCompleted)
                {
                    if (IsReadyDisposed) // 中途释放
                    {
                        _bundleLoader.Release();
                        OnFinish(null);
                        yield break;
                    }

                    this.Progress = _bundleLoader.Progress / 2f;
                    yield return(null);
                }

                if (!_bundleLoader.IsSuccess)
                {
                    Debug.LogErrorFormat("[AssetFileLoader]Load BundleLoader Failed(Error) when Finished: {0}", path);
                    _bundleLoader.Release();
                    OnFinish(null);
                    yield break;
                }

                this.Progress = 0.5f;
                var assetBundle = _bundleLoader.Bundle;
                if (loadAll)
                {
                    if (loaderMode == LoaderMode.Sync)
                    {
                        getAsset = assetBundle.LoadAllAssets(type);
                    }
                    else
                    {
                        var request = assetBundle.LoadAllAssetsAsync(type);
                        while (!request.isDone)
                        {
                            this.Progress = 0.5f + request.progress / 2f;
                            yield return(null);
                        }
                        getAsset = request.allAssets;
                    }
                }
                else
                {
                    var assetPath = ResourceModuleConfig.BundleResoucesDir + "/" + path;
                    if (loaderMode == LoaderMode.Sync)
                    {
                        getAsset = assetBundle.LoadAsset(assetPath, type);
                    }
                    else
                    {
                        var request = assetBundle.LoadAssetAsync(assetPath, type);
                        while (!request.isDone)
                        {
                            this.Progress = 0.5f + request.progress / 2f;
                            yield return(null);
                        }
                        getAsset = request.asset;
                    }
                }

                ResManager.LogLoadTime("AssetFileBridge", loaderMode, path, beginTime);
                if (getAsset == null)
                {
                    Debug.LogErrorFormat("Asset is NULL: {0}", path);
                }
            }

#if UNITY_EDITOR
            if (getAsset != null)
            {
                KResoourceLoadedAssetDebugger.Create(getAsset.GetType().Name, Url, getAsset);
            }
#endif
            OnFinish(getAsset);
        }