/// <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);
        }
Exemple #2
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
        }