예제 #1
0
        public override void Update()
        {
            if (IsDone())
            {
                return;
            }

            if (LoadState == EAssetFileLoadState.None)
            {
                LoadState = EAssetFileLoadState.LoadAssetFile;
            }

            // 1. 加载主资源对象
            if (LoadState == EAssetFileLoadState.LoadAssetFile)
            {
                // Load resource folder file
                System.Type systemType = AssetSystem.MakeSystemType(AssetType);
                if (systemType == null)
                {
                    _cacheRequest = Resources.LoadAsync(LoadPath);
                }
                else
                {
                    _cacheRequest = Resources.LoadAsync(LoadPath, systemType);
                }

                LoadState = EAssetFileLoadState.CheckAssetFile;
            }

            // 2. 检测AssetObject加载结果
            if (LoadState == EAssetFileLoadState.CheckAssetFile)
            {
                if (_cacheRequest.isDone == false)
                {
                    return;
                }
                _mainAsset = _cacheRequest.asset;

                // Check scene
                if (AssetType == EAssetType.Scene)
                {
                    LoadState = EAssetFileLoadState.LoadAssetFileOK;
                    LoadCallback?.Invoke(this);
                    return;
                }

                // Check error
                if (_mainAsset == null)
                {
                    LogSystem.Log(ELogType.Warning, $"Failed to load resource file : {LoadPath}");
                    LoadState = EAssetFileLoadState.LoadAssetFileFailed;
                    LoadCallback?.Invoke(this);
                }
                else
                {
                    LoadState = EAssetFileLoadState.LoadAssetFileOK;
                    LoadCallback?.Invoke(this);
                }
            }
        }
예제 #2
0
        public override void Update()
        {
#if UNITY_EDITOR
            if (IsDone())
            {
                return;
            }

            if (LoadState == EAssetFileLoadState.None)
            {
                LoadState = EAssetFileLoadState.LoadAssetFile;
            }

            // 1. 加载主资源对象
            if (LoadState == EAssetFileLoadState.LoadAssetFile)
            {
                // Load from database
                System.Type systemType = AssetSystem.MakeSystemType(AssetType);
                string      assetPath  = GetDatabaseAssetPath(LoadPath);
                _mainAsset = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, systemType);
                LoadState  = EAssetFileLoadState.CheckAssetFile;

                // 注意:为了模拟异步加载效果,这里直接返回
                return;
            }

            // 2. 检测AssetObject加载结果
            if (LoadState == EAssetFileLoadState.CheckAssetFile)
            {
                // Check scene
                if (AssetType == EAssetType.Scene)
                {
                    LoadState = EAssetFileLoadState.LoadAssetFileOK;
                    LoadCallback?.Invoke(this);
                    return;
                }

                // Check error
                if (_mainAsset == null)
                {
                    LogSystem.Log(ELogType.Warning, $"Failed to load database file : {LoadPath}");
                    LoadState = EAssetFileLoadState.LoadAssetFileFailed;
                    LoadCallback?.Invoke(this);
                }
                else
                {
                    LoadState = EAssetFileLoadState.LoadAssetFileOK;
                    LoadCallback?.Invoke(this);
                }
            }
#else
            throw new Exception("AssetDatabaseLoader only support unity editor.");
#endif
        }
예제 #3
0
        public override void LoadMainAsset(System.Type assetType, System.Action <UnityEngine.Object> callback)
        {
            // Check error
            if (LoadState != EAssetFileLoadState.LoadAssetFileOK)
            {
                LogSystem.Log(ELogType.Error, $"Can not load asset object, {nameof(AssetBundleLoader)} is not ok : {LoadPath}");
                callback?.Invoke(null);
                return;
            }

            string assetName = AssetSystem.GetCacheFileName(LoadPath);

            LoadAsset(assetName, assetType, callback);
        }
예제 #4
0
        /// <summary>
        /// 轮询更新方法
        /// </summary>
        public void Update()
        {
            if (IsDone())
            {
                return;
            }

            if (LoadState == EAssetObjectLoadState.None)
            {
                LoadState = EAssetObjectLoadState.LoadAssetObject;
            }

            // 1. 加载主资源对象
            if (LoadState == EAssetObjectLoadState.LoadAssetObject)
            {
                // Load main asset
                System.Type systemType = AssetSystem.MakeSystemType(AssetType);
                _cacheRequest = _cacheBundle.LoadAssetAsync(AssetName, systemType);
                LoadState     = EAssetObjectLoadState.CheckAssetObject;
            }

            // 2. 检测AssetObject加载结果
            if (LoadState == EAssetObjectLoadState.CheckAssetObject)
            {
                if (_cacheRequest.isDone == false)
                {
                    return;
                }
                AssetObject = _cacheRequest.asset;

                // Check error
                if (AssetObject == null)
                {
                    LoadState = EAssetObjectLoadState.LoadAssetObjectFailed;
                    LoadCallback?.Invoke(AssetObject, false);
                }
                else
                {
                    LoadState = EAssetObjectLoadState.LoadAssetObjectOK;
                    LoadCallback?.Invoke(AssetObject, true);
                }
            }
        }
        public override void LoadMainAsset(EAssetType assetType, OnAssetObjectLoad callback)
        {
            // Check error
            if (LoadState != EAssetFileLoadState.LoadAssetFileOK)
            {
                LogSystem.Log(ELogType.Error, $"Can not load asset object, {nameof(AssetBundleLoader)} is not ok : {LoadPath}");
                callback?.Invoke(null, false);
                return;
            }

            // Check secne
            // 注意:场景文件在获取资源对象的时候直接返回成功
            if (assetType == EAssetType.Scene)
            {
                callback?.Invoke(null, true);
                return;
            }

            // 如果加载器不存在
            if (_mainAssetLoader == null)
            {
                string assetName = AssetSystem.GetCacheFileName(LoadPath);
                _mainAssetLoader = new AssetObjectLoader(_cacheBundle, assetName, assetType);
                _mainAssetLoader.LoadCallback = callback;
                _mainAssetLoader.Update();                 //立刻轮询
            }
            else
            {
                if (_mainAssetLoader.IsDone())
                {
                    callback?.Invoke(_mainAssetLoader.AssetObject, _mainAssetLoader.LoadState == EAssetObjectLoadState.LoadAssetObjectOK);
                }
                else
                {
                    _mainAssetLoader.LoadCallback += callback;
                }
            }
        }
예제 #6
0
        public override void Update()
        {
            // 轮询更新所有对象加载器
            if (LoadState == EAssetFileLoadState.LoadAssetFileOK)
            {
                UpdateAllAssetObjectLoader();
            }

            if (IsDone())
            {
                return;
            }

            if (LoadState == EAssetFileLoadState.None)
            {
                LoadState = EAssetFileLoadState.LoadDepends;
            }

            // 1. 加载所有依赖项
            if (LoadState == EAssetFileLoadState.LoadDepends)
            {
                string[] dependencies = AssetSystem.BundleMethod.GetDirectDependencies(_manifestPath);
                if (dependencies.Length > 0)
                {
                    foreach (string dpManifestPath in dependencies)
                    {
                        string          dpLoadPath = AssetSystem.BundleMethod.GetAssetBundleLoadPath(dpManifestPath);
                        AssetFileLoader dpLoader   = AssetSystem.GetFileLoader(false, dpLoadPath, null, dpManifestPath);
                        _depends.Add(dpLoader);
                    }
                }
                LoadState = EAssetFileLoadState.CheckDepends;
            }

            // 2. 检测所有依赖完成状态
            if (LoadState == EAssetFileLoadState.CheckDepends)
            {
                foreach (var dpLoader in _depends)
                {
                    if (dpLoader.IsDone() == false)
                    {
                        return;
                    }
                }
                LoadState = EAssetFileLoadState.LoadAssetFile;
            }

            // 3. 加载AssetBundle
            if (LoadState == EAssetFileLoadState.LoadAssetFile)
            {
#if UNITY_EDITOR
                // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。
                if (System.IO.File.Exists(LoadPath) == false)
                {
                    LogSystem.Log(ELogType.Warning, $"Not found assetBundle file : {LoadPath}");
                    LoadState = EAssetFileLoadState.LoadAssetFileFailed;
                    LoadCallback?.Invoke(this);
                    return;
                }
#endif

                // Load assetBundle file
                _cacheRequest = AssetBundle.LoadFromFileAsync(LoadPath);
                LoadState     = EAssetFileLoadState.CheckAssetFile;
            }

            // 4. 检测AssetBundle加载结果
            if (LoadState == EAssetFileLoadState.CheckAssetFile)
            {
                if (_cacheRequest.isDone == false)
                {
                    return;
                }
                _cacheBundle = _cacheRequest.assetBundle;

                // Check scene
                if (IsStreamScene)
                {
                    LoadState = EAssetFileLoadState.LoadAssetFileOK;
                    LoadCallback?.Invoke(this);
                    return;
                }

                // Check error
                if (_cacheBundle == null)
                {
                    LogSystem.Log(ELogType.Warning, $"Failed to load assetBundle file : {LoadPath}");
                    LoadState = EAssetFileLoadState.LoadAssetFileFailed;
                    LoadCallback?.Invoke(this);
                }
                else
                {
                    LoadState = EAssetFileLoadState.LoadAssetFileOK;
                    LoadCallback?.Invoke(this);
                }
            }
        }