Ejemplo n.º 1
0
        void IModule.OnGUI()
        {
            int totalCount = AssetSystem.GetLoaderCount();

            ConsoleGUI.Lable($"[{nameof(ResourceManager)}] Virtual simulation : {AssetSystem.SimulationOnEditor}");
            ConsoleGUI.Lable($"[{nameof(ResourceManager)}] Loader count : {totalCount}");
        }
Ejemplo n.º 2
0
        private AssetOperationHandle LoadInternal(string location, System.Type assetType, IAssetParam param)
        {
            string          assetName   = Path.GetFileName(location);
            AssetLoaderBase cacheLoader = AssetSystem.CreateLoader(location);

            return(cacheLoader.LoadAssetAsync(assetName, assetType, param));
        }
Ejemplo n.º 3
0
 private AssetOperationHandle LoadInternal(string assetName, System.Type assetType, IAssetParam param)
 {
     if (_cacheLoader == null)
     {
         _cacheLoader = AssetSystem.CreateLoader(Location, Variant);
     }
     return(_cacheLoader.LoadAssetAsync(assetName, assetType, param));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 异步加载场景
        /// </summary>
        public AssetOperationHandle LoadSceneAsync(string location, SceneInstanceParam instanceParam)
        {
            string          sceneName   = Path.GetFileName(location);
            AssetLoaderBase cacheLoader = AssetSystem.CreateLoader(location);
            var             handle      = cacheLoader.LoadSceneAsync(sceneName, instanceParam);

            return(handle);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 资源回收
        /// 卸载引用计数为零的资源
        /// </summary>
        public void UnloadUnusedAssets()
        {
            // 轮询更新资源系统
            AssetSystem.UpdatePoll();

            // 主动释放零引用资源
            AssetSystem.UnloadUnusedAssets();
        }
Ejemplo n.º 6
0
        public void OnGUI()
        {
            int totalCount  = AssetSystem.DebugGetFileLoaderCount();
            int failedCount = AssetSystem.DebugGetFileLoaderFailedCount();

            DebugConsole.GUILable($"[{nameof(ResourceManager)}] AssetSystemMode : {AssetSystem.SystemMode}");
            DebugConsole.GUILable($"[{nameof(ResourceManager)}] Asset loader total count : {totalCount}");
            DebugConsole.GUILable($"[{nameof(ResourceManager)}] Asset loader failed count : {failedCount}");
        }
        void IModule.OnUpdate()
        {
            // 轮询更新资源系统
            AssetSystem.UpdatePoll();

            // 自动释放零引用资源
            if (_releaseTimer != null && _releaseTimer.Update(Time.unscaledDeltaTime))
            {
                AssetSystem.Release();
            }
        }
Ejemplo n.º 8
0
        private AssetOperationHandle LoadSubAssetsInternal(string location, System.Type assetType, bool forceSyncLoad)
        {
            string          assetName   = Path.GetFileName(location);
            AssetLoaderBase cacheLoader = AssetSystem.CreateLoader(location);
            var             handle      = cacheLoader.LoadSubAssetsAsync(assetName, assetType);

            if (forceSyncLoad)
            {
                cacheLoader.ForceSyncLoad();
            }
            return(handle);
        }
Ejemplo n.º 9
0
        private AssetOperationHandle LoadSubAssetsInternal(string location, System.Type assetType, bool waitForAsyncComplete)
        {
            string         assetName   = Path.GetFileName(location);
            FileLoaderBase cacheLoader = AssetSystem.CreateLoader(location);
            var            handle      = cacheLoader.LoadSubAssetsAsync(assetName, assetType, waitForAsyncComplete);

            if (waitForAsyncComplete)
            {
                cacheLoader.WaitForAsyncComplete();
            }
            return(handle);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 同步加载接口
        /// 注意:仅支持无依赖关系的资源
        /// </summary>
        public T SyncLoad <T>(string location) where T : UnityEngine.Object
        {
            UnityEngine.Object result = null;

            if (AssetSystem.SystemMode == EAssetSystemMode.EditorMode)
            {
#if UNITY_EDITOR
                string loadPath = AssetSystem.FindDatabaseAssetPath(location);
                result = UnityEditor.AssetDatabase.LoadAssetAtPath <T>(loadPath);
                if (result == null)
                {
                    LogSystem.Log(ELogType.Error, $"Failed to load {loadPath}");
                }
#else
                throw new Exception("AssetDatabaseLoader only support unity editor.");
#endif
            }
            else if (AssetSystem.SystemMode == EAssetSystemMode.ResourcesMode)
            {
                result = Resources.Load <T>(location);
                if (result == null)
                {
                    LogSystem.Log(ELogType.Error, $"Failed to load {location}");
                }
            }
            else if (AssetSystem.SystemMode == EAssetSystemMode.BundleMode)
            {
                string      fileName     = System.IO.Path.GetFileNameWithoutExtension(location);
                string      manifestPath = AssetPathHelper.ConvertLocationToManifestPath(location);
                string      loadPath     = AssetSystem.BundleServices.GetAssetBundleLoadPath(manifestPath);
                AssetBundle bundle       = AssetBundle.LoadFromFile(loadPath);
                if (bundle != null)
                {
                    result = bundle.LoadAsset <T>(fileName);
                }
                if (result == null)
                {
                    LogSystem.Log(ELogType.Error, $"Failed to load {loadPath}");
                }
                if (bundle != null)
                {
                    bundle.Unload(false);
                }
            }
            else
            {
                throw new NotImplementedException($"{AssetSystem.SystemMode}");
            }

            return(result as T);
        }
Ejemplo n.º 11
0
 public AssetBundleLoader(AssetBundleInfo bundleInfo)
     : base(bundleInfo)
 {
     // 准备依赖列表
     string[] dependencies = AssetSystem.BundleServices.GetDirectDependencies(bundleInfo.BundleName);
     if (dependencies != null && dependencies.Length > 0)
     {
         foreach (string dependBundleName in dependencies)
         {
             AssetBundleInfo dependBundleInfo = AssetSystem.BundleServices.GetAssetBundleInfo(dependBundleName);
             AssetLoaderBase dependLoader     = AssetSystem.CreateLoaderInternal(dependBundleInfo);
             _depends.Add(dependLoader);
         }
     }
 }
        void IModule.OnCreate(System.Object param)
        {
            CreateParameters createParam = param as CreateParameters;

            if (createParam == null)
            {
                throw new Exception($"{nameof(ResourceManager)} create param is invalid.");
            }

            // 初始化资源系统
            AssetSystem.Initialize(createParam.LocationRoot, createParam.SimulationOnEditor, createParam.BundleServices, createParam.DecryptServices);

            // 创建间隔计时器
            if (createParam.AutoReleaseInterval > 0)
            {
                _releaseTimer = new RepeatTimer(0, createParam.AutoReleaseInterval);
            }
        }
Ejemplo n.º 13
0
        public override void Update()
        {
#if UNITY_EDITOR
            if (IsDone)
            {
                return;
            }

            if (States == EAssetProviderStates.None)
            {
                States = EAssetProviderStates.Loading;
            }

            // 1. 加载资源对象
            if (States == EAssetProviderStates.Loading)
            {
                string loadPath = _owner.LoadPath;

                // 注意:如果加载路径指向的是文件夹
                if (UnityEditor.AssetDatabase.IsValidFolder(loadPath))
                {
                    string folderPath = loadPath;
                    string fileName   = AssetName;
                    loadPath = AssetSystem.FindDatabaseAssetPath(folderPath, fileName);
                }

                AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(loadPath, AssetType);
                States      = EAssetProviderStates.Checking;
            }

            // 2. 检测加载结果
            if (States == EAssetProviderStates.Checking)
            {
                States = AssetObject == null ? EAssetProviderStates.Failed : EAssetProviderStates.Succeed;
                if (States == EAssetProviderStates.Failed)
                {
                    LogSystem.Log(ELogType.Warning, $"Failed to load asset object : {_owner.LoadPath} : {AssetName}");
                }
                InvokeCompletion();
            }
#endif
        }
Ejemplo n.º 14
0
 /// <summary>
 /// 强制回收所有资源
 /// </summary>
 public void UnloadAllAssets()
 {
     AssetSystem.UnloadAllAssets();
 }
 /// <summary>
 /// 强制回收所有资源
 /// </summary>
 public void ForceReleaseAll()
 {
     AssetSystem.ForceReleaseAll();
 }
Ejemplo n.º 16
0
        public override void Update()
        {
            // 如果资源文件加载完毕
            if (States == EAssetFileLoaderStates.LoadAssetFileOK || States == EAssetFileLoaderStates.LoadAssetFileFailed)
            {
                UpdateAllProvider();
                return;
            }

            if (States == EAssetFileLoaderStates.None)
            {
                States = EAssetFileLoaderStates.LoadDepends;
            }

            // 1. 加载所有依赖项
            if (States == EAssetFileLoaderStates.LoadDepends)
            {
                string[] dependencies = AssetSystem.BundleServices.GetDirectDependencies(_manifestPath);
                if (dependencies.Length > 0)
                {
                    foreach (string dpManifestPath in dependencies)
                    {
                        string          dpLoadPath = AssetSystem.BundleServices.GetAssetBundleLoadPath(dpManifestPath);
                        AssetFileLoader dpLoader   = AssetSystem.CreateFileLoaderInternal(dpLoadPath, dpManifestPath);
                        _depends.Add(dpLoader);
                    }
                }
                States = EAssetFileLoaderStates.CheckDepends;
            }

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

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

                // Load assetBundle file
                _cacheRequest = AssetBundle.LoadFromFileAsync(LoadPath);
                States        = EAssetFileLoaderStates.CheckAssetFile;
            }

            // 4. 检测AssetBundle加载结果
            if (States == EAssetFileLoaderStates.CheckAssetFile)
            {
                if (_cacheRequest.isDone == false)
                {
                    return;
                }
                CacheBundle = _cacheRequest.assetBundle;

                // Check error
                if (CacheBundle == null)
                {
                    LogSystem.Log(ELogType.Warning, $"Failed to load assetBundle file : {LoadPath}");
                    States = EAssetFileLoaderStates.LoadAssetFileFailed;
                }
                else
                {
                    States = EAssetFileLoaderStates.LoadAssetFileOK;
                }
            }
        }
Ejemplo n.º 17
0
        public override void Update()
        {
            // 如果资源文件加载完毕
            if (States == ELoaderStates.Success || States == ELoaderStates.Fail)
            {
                UpdateAllProvider();
                return;
            }

            if (States == ELoaderStates.None)
            {
                // 检测加载地址是否为空
                if (string.IsNullOrEmpty(BundleInfo.LocalPath))
                {
                    States = ELoaderStates.Fail;
                    return;
                }

                if (string.IsNullOrEmpty(BundleInfo.RemoteURL))
                {
                    States = ELoaderStates.LoadDepends;
                }
                else
                {
                    States = ELoaderStates.Download;
                }
            }

            // 1. 从服务器下载
            if (States == ELoaderStates.Download)
            {
                _downloader = new WebFileRequest(BundleInfo.RemoteURL, BundleInfo.LocalPath);
                _downloader.DownLoad();
                States = ELoaderStates.CheckDownload;
            }

            // 2. 检测服务器下载结果
            if (States == ELoaderStates.CheckDownload)
            {
                if (_downloader.IsDone() == false)
                {
                    return;
                }

                if (_downloader.HasError())
                {
                    _downloader.ReportError();
                    States = ELoaderStates.Fail;
                }
                else
                {
                    // 校验文件完整性
                    if (AssetSystem.BundleServices.CheckContentIntegrity(BundleInfo.ManifestPath) == false)
                    {
                        MotionLog.Error($"Check download content integrity is failed : {BundleInfo.ManifestPath}");
                        States = ELoaderStates.Fail;
                    }
                    else
                    {
                        States = ELoaderStates.LoadDepends;
                    }
                }

                // 释放网络资源下载器
                if (_downloader != null)
                {
                    _downloader.Dispose();
                    _downloader = null;
                }
            }

            // 3. 加载所有依赖项
            if (States == ELoaderStates.LoadDepends)
            {
                string[] dependencies = AssetSystem.BundleServices.GetDirectDependencies(BundleInfo.ManifestPath);
                if (dependencies != null && dependencies.Length > 0)
                {
                    foreach (string dpManifestPath in dependencies)
                    {
                        AssetBundleInfo dpBundleInfo = AssetSystem.BundleServices.GetAssetBundleInfo(dpManifestPath);
                        AssetLoaderBase dpLoader     = AssetSystem.CreateLoaderInternal(dpBundleInfo);
                        _depends.Add(dpLoader);
                    }
                }
                States = ELoaderStates.CheckDepends;
            }

            // 4. 检测所有依赖完成状态
            if (States == ELoaderStates.CheckDepends)
            {
                foreach (var dpLoader in _depends)
                {
                    if (dpLoader.IsDone() == false)
                    {
                        return;
                    }
                }
                States = ELoaderStates.LoadFile;
            }

            // 5. 加载AssetBundle
            if (States == ELoaderStates.LoadFile)
            {
#if UNITY_EDITOR
                // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。
                if (System.IO.File.Exists(BundleInfo.LocalPath) == false)
                {
                    MotionLog.Warning($"Not found assetBundle file : {BundleInfo.LocalPath}");
                    States = ELoaderStates.Fail;
                    return;
                }
#endif

                // Load assetBundle file
                if (BundleInfo.IsEncrypted)
                {
                    if (AssetSystem.DecryptServices == null)
                    {
                        throw new Exception($"{nameof(AssetBundleLoader)} need IDecryptServices : {BundleInfo.ManifestPath}");
                    }

                    EDecryptMethod decryptType = AssetSystem.DecryptServices.DecryptType;
                    if (decryptType == EDecryptMethod.GetDecryptOffset)
                    {
                        ulong offset = AssetSystem.DecryptServices.GetDecryptOffset(BundleInfo);
                        _cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath, 0, offset);
                    }
                    else if (decryptType == EDecryptMethod.GetDecryptBinary)
                    {
                        byte[] binary = AssetSystem.DecryptServices.GetDecryptBinary(BundleInfo);
                        _cacheRequest = AssetBundle.LoadFromMemoryAsync(binary);
                    }
                    else
                    {
                        throw new NotImplementedException($"{decryptType}");
                    }
                }
                else
                {
                    _cacheRequest = AssetBundle.LoadFromFileAsync(BundleInfo.LocalPath);
                }
                States = ELoaderStates.CheckFile;
            }

            // 6. 检测AssetBundle加载结果
            if (States == ELoaderStates.CheckFile)
            {
                if (_cacheRequest.isDone == false)
                {
                    return;
                }
                CacheBundle = _cacheRequest.assetBundle;

                // Check error
                if (CacheBundle == null)
                {
                    MotionLog.Warning($"Failed to load assetBundle file : {BundleInfo.ManifestPath}");
                    States = ELoaderStates.Fail;
                }
                else
                {
                    States = ELoaderStates.Success;
                }
            }
        }
Ejemplo n.º 18
0
 public void Update()
 {
     AssetSystem.UpdatePoll();
 }
Ejemplo n.º 19
0
 public void LateUpdate()
 {
     AssetSystem.Release();
 }
Ejemplo n.º 20
0
 /// <summary>
 /// 获取资源的信息
 /// </summary>
 public AssetBundleInfo GetAssetBundleInfo(string location, string variant = PatchDefine.AssetBundleDefaultVariant)
 {
     return(AssetSystem.GetAssetBundleInfo(location, variant));
 }
Ejemplo n.º 21
0
        public override void Update()
        {
            // 如果资源文件加载完毕
            if (States == ELoaderStates.Success || States == ELoaderStates.Fail)
            {
                UpdateAllProvider();
                return;
            }

            if (States == ELoaderStates.None)
            {
                States = ELoaderStates.LoadDepends;
            }

            // 1. 加载所有依赖项
            if (States == ELoaderStates.LoadDepends)
            {
                string[] dependencies = AssetSystem.BundleServices.GetDirectDependencies(_manifestPath);
                if (dependencies.Length > 0)
                {
                    foreach (string dpManifestPath in dependencies)
                    {
                        string          dpLoadPath = AssetSystem.BundleServices.GetAssetBundleLoadPath(dpManifestPath);
                        AssetLoaderBase dpLoader   = AssetSystem.CreateLoaderInternal(dpLoadPath, dpManifestPath);
                        _depends.Add(dpLoader);
                    }
                }
                States = ELoaderStates.CheckDepends;
            }

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

            // 3. 加载AssetBundle
            if (States == ELoaderStates.LoadFile)
            {
#if UNITY_EDITOR
                // 注意:Unity2017.4编辑器模式下,如果AssetBundle文件不存在会导致编辑器崩溃,这里做了预判。
                if (System.IO.File.Exists(LoadPath) == false)
                {
                    MotionLog.Log(ELogLevel.Warning, $"Not found assetBundle file : {LoadPath}");
                    States = ELoaderStates.Fail;
                    return;
                }
#endif

                // Load assetBundle file
                if (AssetSystem.DecryptServices != null)
                {
                    if (AssetSystem.DecryptServices.DecryptType == EDecryptMethod.GetDecryptOffset)
                    {
                        ulong offset = AssetSystem.DecryptServices.GetDecryptOffset(LoadPath);
                        _cacheRequest = AssetBundle.LoadFromFileAsync(LoadPath, 0, offset);
                    }
                    else if (AssetSystem.DecryptServices.DecryptType == EDecryptMethod.GetDecryptBinary)
                    {
                        byte[] binary = AssetSystem.DecryptServices.GetDecryptBinary(LoadPath);
                        _cacheRequest = AssetBundle.LoadFromMemoryAsync(binary);
                    }
                    else
                    {
                        throw new NotImplementedException($"{AssetSystem.DecryptServices.DecryptType}");
                    }
                }
                else
                {
                    _cacheRequest = AssetBundle.LoadFromFileAsync(LoadPath);
                }
                States = ELoaderStates.CheckFile;
            }

            // 4. 检测AssetBundle加载结果
            if (States == ELoaderStates.CheckFile)
            {
                if (_cacheRequest.isDone == false)
                {
                    return;
                }
                CacheBundle = _cacheRequest.assetBundle;

                // Check error
                if (CacheBundle == null)
                {
                    MotionLog.Log(ELogLevel.Warning, $"Failed to load assetBundle file : {LoadPath}");
                    States = ELoaderStates.Fail;
                }
                else
                {
                    States = ELoaderStates.Success;
                }
            }
        }
Ejemplo n.º 22
0
 /// <summary>
 /// 获取当前所有正在使用的Bundle信息
 /// </summary>
 public static List <AssetBundleInfo> GetAllBundleInfos()
 {
     return(AssetSystem.GetAllBundleInfos());
 }
Ejemplo n.º 23
0
 /// <summary>
 /// 强制回收所有资源
 /// </summary>
 public void ForceUnloadAllAssets()
 {
     AssetSystem.ForceUnloadAllAssets();
 }
Ejemplo n.º 24
0
 /// <summary>
 /// 资源回收
 /// 卸载引用计数为零的资源
 /// </summary>
 public static void UnloadUnusedAssets()
 {
     AssetSystem.UnloadUnusedAssets();
 }
 /// <summary>
 /// 资源回收
 /// 卸载引用计数为零的资源
 /// </summary>
 public static void Release()
 {
     AssetSystem.Release();
 }
Ejemplo n.º 26
0
 /// <summary>
 /// 获取资源的信息
 /// </summary>
 public AssetBundleInfo GetAssetBundleInfo(string location)
 {
     return(AssetSystem.GetAssetBundleInfo(location));
 }