Ejemplo n.º 1
0
        /// <summary>
        /// 加载资源
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="completeCallback"></param>
        /// <param name="cacheType"></param>
        /// <param name="cacheTimeout"></param>
        public void LoadAsset(string assetName, Action <string, UnityEngine.Object> completeCallback = null, BundleCacheType cacheType = BundleCacheType.TimeoutCache, float cacheTimeout = 5)
        {
            if (!CheckInited())
            {
                completeCallback?.Invoke("", null);
                return;
            }

            var requestBundleName = m_assetNameToBundleNameMap[assetName];

            if (string.IsNullOrEmpty(requestBundleName))
            {
                completeCallback?.Invoke("", null);
                Debug.LogErrorFormat("Asset Info {0} Not Found", assetName);
                return;
            }

            IncreaseRef(requestBundleName);
            if (m_assetLoading.ContainsKey(assetName))            //说明之前有同一个资源的请求
            {
                m_assetLoading[assetName].assetCompleteCallbackList.Add(completeCallback);
                return;
            }

            WeakReference weakPtr = null;

            if (m_assetNameToWeakPtr.TryGetValue(assetName, out weakPtr) && weakPtr.IsAlive)
            {
                completeCallback?.Invoke(assetName, (UnityEngine.Object)weakPtr.Target);
                return;
            }

            var assetLoader = new KTAssetLoader()
            {
                loaderType                = KTAssetLoader.LoaderType.Asset,
                requestAssetName          = assetName,
                requestBundleName         = requestBundleName,
                cacheType                 = cacheType,
                cacheTimeout              = cacheTimeout,
                dependentBundleNames      = m_manifest.GetAllDependencies(requestBundleName).ToList(),
                assetRequest              = null,
                assetCompleteCallbackList = new List <Action <string, UnityEngine.Object> >()
                {
                    completeCallback
                },
                assetCallbackForManager = AssetCompleteCallback
            };

            m_assetLoading.Add(assetName, assetLoader);

            for (int i = 0, j = assetLoader.dependentBundleNames.Count; i < j; ++i)
            {
                var bundleName = assetLoader.dependentBundleNames[i];
                if (m_bundleLoaded.ContainsKey(bundleName))
                {
                    continue;
                }

                if (m_bundleLoading.ContainsKey(bundleName))
                {
                    m_bundleLoading[bundleName].Subscribe(assetLoader);
                }
                else
                {
                    var bundleLoader = assetLoader.CreateBundleLoader(bundleName, BundleCompleteCallback);
                    m_bundleLoading.Add(bundleName, bundleLoader);
                    bundleLoader.BeginLoad();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 加载场景
        /// </summary>
        /// <param name="assetName"></param>
        /// <param name="completeCallback"></param>
        /// <param name="loadMode"></param>
        /// <param name="cacheType">可以选三种模式</param>
        /// <param name="cacheTimeout"></param>
        public void LoadScene(string assetName, Action <string> completeCallback = null, LoadSceneMode loadMode = LoadSceneMode.Additive, BundleCacheType cacheType = BundleCacheType.None, float cacheTimeout = 5)
        {
            if (!CheckInited())
            {
                completeCallback?.Invoke("");
                return;
            }

            var requestBundleName = m_assetNameToBundleNameMap[assetName];

            if (string.IsNullOrEmpty(requestBundleName))
            {
                completeCallback?.Invoke("");
                Debug.LogErrorFormat("Asset Info {0} Not Found", assetName);
                return;
            }

            if (m_assetLoading.ContainsKey(assetName))            //场景不允许请求重复
            {
                completeCallback?.Invoke("");
                Debug.LogErrorFormat("Request Scene {0} Repeated", assetName);
                return;
            }

            IncreaseRef(requestBundleName);

            var assetLoader = new KTAssetLoader()
            {
                loaderType              = KTAssetLoader.LoaderType.Scene,
                requestAssetName        = assetName,
                requestBundleName       = requestBundleName,
                cacheType               = cacheType,
                cacheTimeout            = cacheTimeout,
                dependentBundleNames    = m_manifest.GetAllDependencies(requestBundleName).ToList(),
                sceneRequest            = null,
                loadMode                = loadMode,
                sceneCompleteCallback   = completeCallback,
                assetCallbackForManager = AssetCompleteCallback
            };

            m_assetLoading.Add(assetName, assetLoader);

            for (int i = 0, j = assetLoader.dependentBundleNames.Count; i < j; ++i)
            {
                var bundleName = assetLoader.dependentBundleNames[i];
                if (m_bundleLoaded.ContainsKey(bundleName))
                {
                    continue;
                }

                if (m_bundleLoading.ContainsKey(bundleName))
                {
                    m_bundleLoading[bundleName].Subscribe(assetLoader);
                }
                else
                {
                    var bundleLoader = assetLoader.CreateBundleLoader(bundleName, BundleCompleteCallback);
                    m_bundleLoading.Add(bundleName, bundleLoader);
                    bundleLoader.BeginLoad();
                }
            }
        }