Exemple #1
0
        /// <summary>
        /// 异步加载场景
        /// </summary>
        /// <param name="sceneAssetName">要加载场景资源的名称</param>
        /// <param name="priority">加载场景资源的优先级</param>
        /// <param name="loadSceneCallbacks">加载场景回调函数集</param>
        /// <param name="userData">用户自定义数据</param>
        internal void LoadScene(string sceneAssetName, int priority, LoadAssetCallbacks loadSceneCallbacks, object userData)
        {
            string assetBundleName = null;

            if (!m_AssetInfos.TryGetValue(sceneAssetName, out assetBundleName))
            {
                string errorMessage = TextUtil.Format("Can not load scene '{0}'.", sceneAssetName);
                if (loadSceneCallbacks.LoadAssetFailureCallback != null)
                {
                    loadSceneCallbacks.LoadAssetFailureCallback(sceneAssetName, errorMessage, userData);
                    return;
                }

                throw new Exception(errorMessage);
            }

            //依赖项名称
            string[] dependencyAssetNames = m_AssetBundleManifest.GetAllDependencies(assetBundleName);

            if (!CheckAsset(assetBundleName))
            {
                string errorMessage = string.Format("Can not load scene '{0}'.", sceneAssetName);
                if (loadSceneCallbacks.LoadAssetFailureCallback != null)
                {
                    loadSceneCallbacks.LoadAssetFailureCallback(sceneAssetName, errorMessage, userData);
                    return;
                }

                throw new Exception(errorMessage);
            }

            ResourceLoaderTask mainTask = new ResourceLoaderTask(sceneAssetName, assetBundleName, null, priority, dependencyAssetNames, true, null, loadSceneCallbacks, userData);

            foreach (string dependencyAssetName in dependencyAssetNames)
            {
                if (!LoadDependencyAsset(assetBundleName, dependencyAssetName, priority, mainTask, loadSceneCallbacks, userData))
                {
                    string errorMessage = string.Format("Can not load dependency asset '{0}' when load scene '{1}'.", dependencyAssetName, sceneAssetName);
                    if (loadSceneCallbacks.LoadAssetFailureCallback != null)
                    {
                        loadSceneCallbacks.LoadAssetFailureCallback(sceneAssetName, errorMessage, userData);
                        return;
                    }

                    throw new Exception(errorMessage);
                }
            }

            m_TaskPool.AddTask(mainTask);
        }
Exemple #2
0
 public ResourceLoaderTask(string assetName, string assetBundleName, Type assetType, int priority, string[] dependencyAssetNames, bool isScene, ResourceLoaderTask mainTask, LoadAssetCallbacks loadAssetCallbacks, object userData)
 {
     m_SerialId                  = s_Serial++;
     m_Priority                  = priority;
     m_Done                      = false;
     m_AssetBundleName           = assetBundleName;
     m_AssetType                 = assetType;
     m_DependencyAssetNames      = dependencyAssetNames;
     m_UserData                  = userData;
     m_StartTime                 = default(DateTime);
     m_LoadAssetCallbacks        = loadAssetCallbacks;
     m_TotalDependencyAssetCount = 0;
     m_MainTask                  = mainTask;
     m_DependencyAssets          = new List <AssetBundle>();
     m_IsScene                   = isScene;
     m_AssetName                 = assetName;
 }
Exemple #3
0
        /// <summary>
        /// 加载依赖资源
        /// </summary>
        /// <param name="assetName">资源名称</param>
        /// <param name="priority">加载优先级</param>
        /// <param name="mainTask">主资源任务</param>
        /// <param name="userData">用户数据</param>
        /// <returns></returns>
        private bool LoadDependencyAsset(string assetName, string dependencyassetname, int priority, ResourceLoaderTask mainTask, LoadAssetCallbacks loadAssetCallbacks, object userData)
        {
            if (!CheckAsset(dependencyassetname))
            {
                string errorMessage = string.Format("Can not load DependencyAsset '{0}' when load '{1}'.", dependencyassetname, assetName);
                if (loadAssetCallbacks.LoadAssetFailureCallback != null)
                {
                    loadAssetCallbacks.LoadAssetFailureCallback(dependencyassetname, errorMessage, userData);
                    return(false);
                }
                Debug.LogError(errorMessage);
                return(false);
            }

            ResourceLoaderTask dependencyTask = new ResourceLoaderTask(null, dependencyassetname, null, priority, null, false, mainTask, loadAssetCallbacks, userData);

            m_TaskPool.AddTask(dependencyTask);
            return(true);
        }
Exemple #4
0
        /// <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, Type assetType, int priority, LoadAssetCallbacks loadAssetCallbacks, object userData)
        {
            //依赖项名称
            if (m_AssetBundleManifest == null)
            {
                throw new Exception("Please set manifest first!");
            }

            if (!HasAsset(assetName))
            {
                string errorMessage = string.Format("Can not load asset '{0}'.", assetName);
                if (loadAssetCallbacks.LoadAssetFailureCallback != null)
                {
                    loadAssetCallbacks.LoadAssetFailureCallback(assetName, errorMessage, userData);
                    return;
                }

                throw new Exception(errorMessage);
            }

            string assetBundleName = null;

            if (!m_AssetInfos.TryGetValue(assetName, out assetBundleName))
            {
                string errorMessage = TextUtil.Format("Can not load asset '{0}'.", assetName);
                if (loadAssetCallbacks.LoadAssetFailureCallback != null)
                {
                    loadAssetCallbacks.LoadAssetFailureCallback(assetName, errorMessage, userData);
                    return;
                }

                throw new Exception(errorMessage);
            }


            if (!CheckAsset(assetBundleName))
            {
                string errorMessage = string.Format("Can not load assetbundle '{0}'.", assetBundleName);
                if (loadAssetCallbacks.LoadAssetFailureCallback != null)
                {
                    loadAssetCallbacks.LoadAssetFailureCallback(assetBundleName, errorMessage, userData);
                    return;
                }

                throw new Exception(errorMessage);
            }

            string[] dependencyAssetNames = m_AssetBundleManifest.GetAllDependencies(assetBundleName);

            ResourceLoaderTask mainTask = new ResourceLoaderTask(assetName, assetBundleName, assetType, priority, dependencyAssetNames, false, null, loadAssetCallbacks, userData);

            foreach (string dependencyAssetName in dependencyAssetNames)
            {
                if (!LoadDependencyAsset(assetBundleName, dependencyAssetName, priority, mainTask, loadAssetCallbacks, userData))
                {
                    string errorMessage = string.Format("Can not load dependency asset '{0}' when load asset '{1}'.", dependencyAssetName, assetBundleName);
                    if (loadAssetCallbacks.LoadAssetFailureCallback != null)
                    {
                        loadAssetCallbacks.LoadAssetFailureCallback(assetBundleName, errorMessage, userData);
                        return;
                    }

                    throw new Exception(errorMessage);
                }
            }

            m_TaskPool.AddTask(mainTask);
        }