コード例 #1
0
        /// <summary>
        /// 创建资源文件加载器
        /// </summary>
        public static AssetLoaderBase CreateLoader(string location, string variant)
        {
            if (_isInitialize == false)
            {
                throw new Exception($"{nameof(AssetSystem)} is not initialize.");
            }

            if (SimulationOnEditor)
            {
#if UNITY_EDITOR
                string loadPath = AssetPathHelper.FindDatabaseAssetPath(location);
                return(CreateLoaderInternal(loadPath, null));
#else
                throw new Exception("AssetSystem virtual simulation only support unity editor.");
#endif
            }
            else
            {
                if (BundleServices == null)
                {
                    throw new Exception($"{nameof(AssetSystem.BundleServices)} is null. Use {nameof(AssetSystem.Initialize)}");
                }

                string manifestPath = BundleServices.ConvertLocationToManifestPath(location, variant);
                string loadPath     = BundleServices.GetAssetBundleLoadPath(manifestPath);
                return(CreateLoaderInternal(loadPath, manifestPath));
            }
        }
コード例 #2
0
        /// <summary>
        /// 获取资源信息
        /// </summary>
        public static AssetBundleInfo GetAssetBundleInfo(string location, string variant)
        {
            if (_isInitialize == false)
            {
                throw new Exception($"{nameof(AssetSystem)} is not initialize.");
            }

            if (SimulationOnEditor)
            {
#if UNITY_EDITOR
                string          localPath    = AssetPathHelper.FindDatabaseAssetPath(location);
                string          manifestPath = AssetPathHelper.ConvertLocationToManifestPath(location, variant);
                AssetBundleInfo bundleInfo   = new AssetBundleInfo(manifestPath, localPath);
                return(bundleInfo);
#else
                throw new Exception($"AssetSystem simulation only support unity editor.");
#endif
            }
            else
            {
                if (BundleServices == null)
                {
                    throw new Exception($"{nameof(BundleServices)} is null. Use {nameof(AssetSystem.Initialize)}");
                }

                string manifestPath = AssetPathHelper.ConvertLocationToManifestPath(location, variant);
                return(BundleServices.GetAssetBundleInfo(manifestPath));
            }
        }
コード例 #3
0
        /// <summary>
        /// 获取资源信息
        /// </summary>
        public static AssetBundleInfo GetAssetBundleInfo(string location)
        {
            if (_isInitialize == false)
            {
                throw new Exception($"{nameof(AssetSystem)} is not initialize.");
            }

            if (SimulationOnEditor)
            {
#if UNITY_EDITOR
                string          assetPath  = AssetPathHelper.FindDatabaseAssetPath(location);
                AssetBundleInfo bundleInfo = new AssetBundleInfo(assetPath, assetPath);
                return(bundleInfo);
#else
                throw new Exception($"AssetSystem simulation only support unity editor.");
#endif
            }
            else
            {
                if (BundleServices == null)
                {
                    throw new Exception($"{nameof(BundleServices)} is null. Use {nameof(AssetSystem.Initialize)}");
                }

                string assetPath  = $"{LocationRoot}/{location}".ToLower();
                string bundleName = BundleServices.GetAssetBundleName(assetPath);
                return(BundleServices.GetAssetBundleInfo(bundleName));
            }
        }
コード例 #4
0
        /// <summary>
        /// 获取资源的最终加载路径
        /// </summary>
        public string GetLoadPath(string location, string variant = "unity3d")
        {
            if (AssetSystem.SimulationOnEditor)
            {
#if UNITY_EDITOR
                string loadPath = AssetPathHelper.FindDatabaseAssetPath(location);
                if (string.IsNullOrEmpty(loadPath))
                {
                    MotionLog.Warning($"Not found asset : {location}");
                }
                return(loadPath);
#else
                throw new Exception($"AssetSystem simulation only support unity editor.");
#endif
            }
            else
            {
                if (AssetSystem.BundleServices == null)
                {
                    throw new Exception($"{nameof(AssetSystem.BundleServices)} is null.");
                }

                string manifestPath = AssetSystem.BundleServices.ConvertLocationToManifestPath(location, variant);
                string loadPath     = AssetSystem.BundleServices.GetAssetBundleLoadPath(manifestPath);
                if (string.IsNullOrEmpty(loadPath))
                {
                    MotionLog.Warning($"Not found asset : {location}");
                }
                return(loadPath);
            }
        }
コード例 #5
0
        /// <summary>
        /// 创建资源文件加载器
        /// </summary>
        public static AssetFileLoader CreateFileLoader(string location)
        {
            AssetFileLoader loader;

            if (SystemMode == EAssetSystemMode.EditorMode)
            {
#if UNITY_EDITOR
                string loadPath = FindDatabaseAssetPath(location);
                loader = CreateFileLoaderInternal(loadPath, null);
#else
                throw new Exception("EAssetSystemMode.EditorMode only support unity editor.");
#endif
            }
            else if (SystemMode == EAssetSystemMode.ResourcesMode)
            {
                string loadPath = location;
                loader = CreateFileLoaderInternal(loadPath, null);
            }
            else if (SystemMode == EAssetSystemMode.BundleMode)
            {
                if (BundleServices == null)
                {
                    throw new Exception($"{nameof(IBundleServices)} is null.");
                }

                string manifestPath = AssetPathHelper.ConvertLocationToManifestPath(location);
                string loadPath     = BundleServices.GetAssetBundleLoadPath(manifestPath);
                loader = CreateFileLoaderInternal(loadPath, manifestPath);
            }
            else
            {
                throw new NotImplementedException($"{SystemMode}");
            }
            return(loader);
        }
コード例 #6
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);
        }
コード例 #7
0
 /// <summary>
 /// 初始化资源系统
 /// 注意:在使用AssetSystem之前需要初始化
 /// </summary>
 public static void Initialize(string locationRoot, bool simulationOnEditor, IBundleServices bundleServices, IDecryptServices decryptServices)
 {
     if (_isInitialize == false)
     {
         _isInitialize      = true;
         LocationRoot       = AssetPathHelper.GetRegularPath(locationRoot);
         SimulationOnEditor = simulationOnEditor;
         BundleServices     = bundleServices;
         DecryptServices    = decryptServices;
     }
     else
     {
         throw new Exception($"{nameof(AssetSystem)} is already initialized");
     }
 }
コード例 #8
0
        /// <summary>
        /// 同步加载接口
        /// 注意:仅支持无依赖关系的资源
        /// </summary>
        public T SyncLoad <T>(string location, string variant) where T : UnityEngine.Object
        {
            UnityEngine.Object result = null;

            if (AssetSystem.SimulationOnEditor)
            {
#if UNITY_EDITOR
                string loadPath = AssetPathHelper.FindDatabaseAssetPath(location);
                result = UnityEditor.AssetDatabase.LoadAssetAtPath <T>(loadPath);
                if (result == null)
                {
                    MotionLog.Log(ELogLevel.Error, $"Failed to load {loadPath}");
                }
#else
                throw new Exception($"AssetSystem virtual simulation only support unity editor.");
#endif
            }
            else
            {
                if (AssetSystem.BundleServices == null)
                {
                    throw new Exception($"{nameof(AssetSystem.BundleServices)} is null.");
                }

                string      manifestPath = AssetSystem.BundleServices.ConvertLocationToManifestPath(location, variant);
                string      loadPath     = AssetSystem.BundleServices.GetAssetBundleLoadPath(manifestPath);
                AssetBundle bundle       = AssetBundle.LoadFromFile(loadPath);
                if (bundle != null)
                {
                    string fileName = System.IO.Path.GetFileName(location);
                    result = bundle.LoadAsset <T>(fileName);
                }

                if (result == null)
                {
                    MotionLog.Log(ELogLevel.Error, $"Failed to load {loadPath}");
                }

                if (bundle != null)
                {
                    bundle.Unload(false);
                }
            }

            return(result as T);
        }
コード例 #9
0
        public override void Update()
        {
#if UNITY_EDITOR
            if (IsDone)
            {
                return;
            }

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

            // 1. 加载资源对象
            if (States == EAssetStates.Loading)
            {
                string assetPath = _owner.BundleInfo.LocalPath;

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

                AssetObject = UnityEditor.AssetDatabase.LoadAssetAtPath(assetPath, AssetType);
                States      = EAssetStates.Checking;
            }

            // 2. 检测加载结果
            if (States == EAssetStates.Checking)
            {
                States = AssetObject == null ? EAssetStates.Fail : EAssetStates.Success;
                if (States == EAssetStates.Fail)
                {
                    MotionLog.Warning($"Failed to load asset object : {_owner.BundleInfo.LocalPath} : {AssetName}");
                }
                InvokeCompletion();
            }
#endif
        }
コード例 #10
0
        /// <summary>
        /// 初始化资源系统
        /// 注意:在使用AssetSystem之前需要初始化
        /// </summary>
        public static void Initialize(string locationRoot, bool simulationOnEditor, int runtimeMaxLoadingCount, IBundleServices bundleServices, IDecryptServices decryptServices)
        {
            if (_isInitialize == false)
            {
                _isInitialize = true;

                if (runtimeMaxLoadingCount < 3)
                {
                    runtimeMaxLoadingCount = 3;
                    MotionLog.Warning("AssetSystem RuntimeMaxLoadingCount minimum is 3");
                }

                LocationRoot           = AssetPathHelper.GetRegularPath(locationRoot);
                SimulationOnEditor     = simulationOnEditor;
                RuntimeMaxLoadingCount = runtimeMaxLoadingCount;
                BundleServices         = bundleServices;
                DecryptServices        = decryptServices;
            }
            else
            {
                throw new Exception($"{nameof(AssetSystem)} is already initialized");
            }
        }