コード例 #1
0
ファイル: ABLoader.cs プロジェクト: jeason0813/knight
        private IEnumerator LoadAsset_Async(AssetLoaderRequest rRequest)
        {
            ABLoadEntry rAssetLoadEntry = null;

            if (!ABLoaderVersion.Instance.TryGetValue(rRequest.Path, out rAssetLoadEntry))
            {
                Debug.LogErrorFormat("Can not find assetbundle: -- {0}", rRequest.Path);
                rRequest.Asset = null;
                rRequest.SetResult(rRequest);
                yield break;
            }

            //引用计数加1
            rAssetLoadEntry.RefCount++;

            // 确认未加载完成并且正在被加载、一直等待其加载完成
            while (rAssetLoadEntry.IsLoading && !rAssetLoadEntry.IsLoadCompleted)
            {
                yield return(new WaitForEndOfFrame());
            }

            // 如果该资源加载完成了
            if (!rAssetLoadEntry.IsLoading && rAssetLoadEntry.IsLoadCompleted)
            {
                // 从缓存的Assetbundle里面加载资源
                yield return(LoadAssetObject(rRequest, rAssetLoadEntry, false));

                rRequest.SetResult(rRequest);
                yield break;
            }

            // 开始加载资源依赖项
            if (rAssetLoadEntry.ABDependNames != null && !rRequest.IsSimulate)
            {
                for (int i = rAssetLoadEntry.ABDependNames.Length - 1; i >= 0; i--)
                {
                    string rDependABPath = rAssetLoadEntry.ABDependNames[i];
                    string rDependABName = rDependABPath;

                    var rDependAssetRequest = new AssetLoaderRequest(rDependABName, "", false, rRequest.IsSimulate, false);
                    yield return(LoadAsset_Async(rDependAssetRequest));
                }
            }

            //开始加载当前的资源包
            rAssetLoadEntry.IsLoading       = true;
            rAssetLoadEntry.IsLoadCompleted = false;

            // 真正的从AB包里面加载资源
            yield return(LoadAssetObject(rRequest, rAssetLoadEntry, true));

            rRequest.SetResult(rRequest);

            rAssetLoadEntry.IsLoading       = false;
            rAssetLoadEntry.IsLoadCompleted = true;
        }
コード例 #2
0
ファイル: ABLoader.cs プロジェクト: LittleDragonMan/knight
        private IEnumerator LoadAssetAsync(AssetLoaderRequest rRequest)
        {
            this.mIsLoadingRefCount++;

            ABLoadEntry rAssetLoadEntry = null;

            if (!rRequest.IsSimulate)
            {
                if (!ABLoaderVersion.Instance.TryGetValue(rRequest.Path, out rAssetLoadEntry))
                {
                    Debug.LogErrorFormat("---Can not find assetbundle: -- {0}", rRequest.Path);
                    rRequest.Asset = null;
                    this.mIsLoadingRefCount--;
                    rRequest.SetResult(rRequest);
                    yield break;
                }
            }
            else
            {
                rAssetLoadEntry = new ABLoadEntry()
                {
                    ABName        = rRequest.Path,
                    ABPath        = ABLoaderVersion.Instance.GetABPath_With_Space(LoaderSpace.Streaming, rRequest.Path),
                    ABDependNames = new string[0],
                };
            }

            // 得到该资源的所有依赖项
            var rABAllDependenceEntries = this.GetABEntryAllDependencies(rAssetLoadEntry);

            this.mTempEntries.Clear();
            for (int i = 0; i < rABAllDependenceEntries.Count; i++)
            {
                rABAllDependenceEntries[i].RefCount++;
                if (!this.mTempEntries.Contains(rABAllDependenceEntries[i]))
                {
                    this.mTempEntries.Add(rABAllDependenceEntries[i]);
                }
            }
            for (int i = 0; i < this.mTempEntries.Count; i++)
            {
                // 构建依赖项的Request
                var rItem = this.mTempEntries[i];
                var rDependenceLoaderRequest = new AssetLoaderRequest(
                    rItem.ABPath, string.Empty, true, rRequest.IsSimulate, rRequest.IsLoadAllAssets);
                yield return(this.LoadAssetAsync_OneEntry(rDependenceLoaderRequest, rItem));
            }
            yield return(this.LoadAssetAsync_OneEntry(rRequest, rABAllDependenceEntries[rABAllDependenceEntries.Count - 1]));

            this.mIsLoadingRefCount--;
            rRequest.SetResult(rRequest);
        }