Example #1
0
        public void AddLoadAssetInfo(string assetName, Type assetType, int priority, LoadAssetCallbacks loadAssetCallbacks, object userData)
        {
            if (loadAssetCallbacks == null)
            {
                Log.Error("Load asset callbacks is invalid.");
                return;
            }

            if (string.IsNullOrEmpty(assetName))
            {
                if (loadAssetCallbacks.AsyncAction != null)
                {
                    loadAssetCallbacks.CreateCallbackFailed(assetName, LoadResourceStatus.NotExist, "Asset name is invalid.", userData);

                    loadAssetCallbacks.AsyncAction.Invoke(loadAssetCallbacks);
                }
                else
                {
                    loadAssetCallbacks.LoadAssetFailureCallback?.Invoke(assetName, LoadResourceStatus.NotExist, "Asset name is invalid.", userData);
                }

                return;
            }

            m_LoadAssetInfos.AddLast(new LoadAssetInfo(assetName, assetType, priority, DateTime.Now, 0, loadAssetCallbacks, userData));
        }
Example #2
0
        public override void OnLoadAssetFailure(LoadResourceAgent agent, LoadResourceStatus status, string errorMessage)
        {
            base.OnLoadAssetFailure(agent, status, errorMessage);

            //加载失败 这边可以直接异步回调
            if (m_LoadAssetCallbacks.AsyncAction != null)
            {
                m_LoadAssetCallbacks.CreateCallbackFailed(AssetName, status, errorMessage, UserData);

                m_LoadAssetCallbacks.AsyncAction.Invoke(m_LoadAssetCallbacks);
            }
            else
            {
                m_LoadAssetCallbacks.LoadAssetFailureCallback?.Invoke(AssetName, status, errorMessage, UserData);
            }
        }
        /// <summary>
        /// 异步加载资源。
        /// </summary>
        /// <param name="assetName">要加载资源的名称。</param>
        /// <param name="assetType">要加载资源的类型。</param>
        /// <param name="priority">加载资源的优先级。</param>
        /// <param name="loadAssetCallbacks">加载资源回调函数集。</param>
        /// <param name="userData">用户自定义数据。</param>
        public void LoadAsset(string assetName, AssetCategory assetCategory, Type assetType, int priority, LoadAssetCallbacks loadAssetCallbacks, object userData)
        {
            ResourceInfo resourceInfo = null;
            Dictionary <string, AssetCategory> dependencyAssetNames = null;

            if (!CheckAsset(assetName, assetCategory, out resourceInfo, out dependencyAssetNames))
            {
                string errorMessage = Utility.Text.Format("Can not load asset '{0}'.", assetName);

                if (loadAssetCallbacks.AsyncAction != null)
                {
                    loadAssetCallbacks.CreateCallbackFailed(assetName, resourceInfo != null && !resourceInfo.Ready ? LoadResourceStatus.NotReady : LoadResourceStatus.NotExist, errorMessage, userData);

                    loadAssetCallbacks.AsyncAction.Invoke(loadAssetCallbacks);
                }
                else
                {
                    loadAssetCallbacks.LoadAssetFailureCallback?.Invoke(assetName, resourceInfo != null && !resourceInfo.Ready ? LoadResourceStatus.NotReady : LoadResourceStatus.NotExist, errorMessage, userData);
                }
                return;
            }

            if (resourceInfo.IsLoadFromBinary)
            {
                string errorMessage = Utility.Text.Format("Can not load asset '{0}' which is a binary asset.", assetName);

                if (loadAssetCallbacks.AsyncAction != null)
                {
                    loadAssetCallbacks.CreateCallbackFailed(assetName, LoadResourceStatus.TypeError, errorMessage, userData);

                    loadAssetCallbacks.AsyncAction.Invoke(loadAssetCallbacks);
                }
                else
                {
                    loadAssetCallbacks.LoadAssetFailureCallback?.Invoke(assetName, LoadResourceStatus.TypeError, errorMessage, userData);
                }
                return;
            }

            LoadAssetTask mainTask = LoadAssetTask.Create(assetName, assetCategory, assetType, priority, resourceInfo, dependencyAssetNames, loadAssetCallbacks, userData);

            foreach (var item in dependencyAssetNames)
            {
                if (!LoadDependencyAsset(item.Key, item.Value, priority, mainTask, userData))
                {
                    string errorMessage = Utility.Text.Format("Can not load dependency asset '{0}' when load asset '{1}'.", item.Key, assetName);

                    if (loadAssetCallbacks.AsyncAction != null)
                    {
                        loadAssetCallbacks.CreateCallbackFailed(assetName, LoadResourceStatus.DependencyError, errorMessage, userData);

                        loadAssetCallbacks.AsyncAction.Invoke(loadAssetCallbacks);
                    }
                    else
                    {
                        loadAssetCallbacks.LoadAssetFailureCallback?.Invoke(assetName, LoadResourceStatus.DependencyError, errorMessage, userData);
                    }
                    return;
                }
            }

            m_TaskPool.AddTask(mainTask);
            if (!resourceInfo.Ready)
            {
                m_ResourceComponent.UpdateResource(resourceInfo.ResourceName);
            }
        }