コード例 #1
0
ファイル: AssetUtils.cs プロジェクト: yqxflong/mh_3drpg
 public static void DoAction <T>(UniqueAssetActionWrapper <T> action, string assetname, T asset, bool success) where T : UnityEngine.Object
 {
     if (action?.Execute(assetname, asset, success) <= 0)
     {
         EB.Debug.LogError("Action[3] is Null! Asset is {0}, success is {1}", assetname, success);
     }
 }
コード例 #2
0
ファイル: TextureManager.cs プロジェクト: yqxflong/mh_3drpg
        IEnumerator DownloadTexture2DCoroutine(string texName, System.Action <string, Texture2D, bool> action, GameObject target)
        {
            string bundlename = AssetManager.GetBundleNameContainsAsset(texName);

            if (string.IsNullOrEmpty(bundlename))
            {
                EB.Debug.LogWarning("DownloadTexture2DCoroutine: bundle not found for texture {0}", texName);
                AssetUtils.DoAction <Texture2D>(action, texName, null, false, target);
                yield break;
            }

            if (AssetManager.IsLocalBundleOutdated(bundlename))
            {
                yield return(AssetManager.DownloadAssetBundle(bundlename));

                if (target == null)
                {
                    EB.Debug.LogWarning("DownloadTexture2DCoroutine: target is null after download assetbundle {0} {1}, stop callback", texName, bundlename);
                    yield break;
                }
            }

            // ensure add bundle reference by texName
            if (mLoadingTextures.ContainsKey(texName))
            {
                //EB.Debug.Log("DownloadTexture2DCoroutine: texture is loading {0}, push callback action", texName);
                mLoadingTextures[texName].Push(action, target);
                yield break;
            }

            Texture2D _tex = null;

            if (!mCachedTexture2DDict.TryGetValue(texName, out _tex) || _tex == null)
            {
                UniqueAssetActionWrapper <Texture2D> wrapper = new UniqueAssetActionWrapper <Texture2D>(action, target);
                mLoadingTextures.Add(texName, wrapper);

                GM.BundleLoader loader = new GM.BundleLoader(bundlename, gameObject);
                if (loader.keepWaiting)
                {
                    yield return(loader);
                }

                if (!loader.Success)
                {
                    EB.Debug.LogError("DownloadTexture2DCoroutine: load bundle {0} error", bundlename);
                    mLoadingTextures.Remove(texName);
                    AssetUtils.DoAction <Texture2D>(mLoadingTextures[texName], texName, null, false);
                    yield break;
                }

                AssetBundleRequest waitasset = loader.Bundle.LoadAssetAsync(texName);
                if (!waitasset.isDone)
                {
                    yield return(waitasset);
                }

                _tex = waitasset.asset as Texture2D;
                if (_tex != null)
                {
                    EB.Debug.Log("DownloadTexture2DCoroutine: loaded texture {0}", texName);
                    if (!mCachedTexture2DDict.ContainsKey(texName))
                    {
                        mCachedTexture2DDict.Add(texName, _tex);
                    }
                    else if (mCachedTexture2DDict[texName] != _tex)
                    {
                        EB.Debug.LogError("DownloadTexture2DCoroutine: texture instance is different for {0}", texName);
                        ReleaseTextureImpl(texName);
                        mCachedTexture2DDict.Add(texName, _tex);
                    }
                }

                if (!mLoadingTextures.ContainsKey(texName))
                {                // already unload, stop callback
                    EB.Debug.LogWarning("DownloadTexture2DCoroutine: texture released before loaded {0}, stop callback", texName);
                    if (_tex != null)
                    {
                        ReleaseTextureImpl(texName);
                    }
                    yield break;
                }

                if (mLoadingTextures[texName] != wrapper)
                {                // async load > release > async load problem
                    EB.Debug.Log("DownloadTexture2DCoroutine: ABA problem {0}, stop callback", texName);
                    if (_tex != null)
                    {
                        ReleaseTextureImpl(texName);
                    }
                    yield break;
                }

                int refCount = mLoadingTextures[texName].Simulate();
                if (refCount <= 0)
                {                // nothing to execute
                    EB.Debug.LogWarning("DownloadTexture2DCoroutine: nothing to callback {0}", texName);
                    if (_tex != null)
                    {
                        ReleaseTextureImpl(texName);
                    }
                    mLoadingTextures.Remove(texName);
                    yield break;
                }

                if (_tex != null)
                {
                    AssetUtils.DoAction <Texture2D>(mLoadingTextures[texName], texName, _tex, true);
                }
                else
                {
                    EB.Debug.LogWarning("DownloadTexture2DCoroutine: Get none texture {0} from bundle {1}", texName, bundlename);
                    AssetUtils.DoAction <Texture2D>(mLoadingTextures[texName], texName, null, false);
                }

                mLoadingTextures.Remove(texName);
            }
            else
            {
                AssetUtils.DoAction <Texture2D>(action, texName, _tex, true, target);
            }
        }