Exemplo n.º 1
0
    public void doReleaseTexture(string name, bool force)
    {
        if (mReplacement != null)
        {
            mReplacement.doReleaseTexture(name, force);
            return;
        }
        if (isBorrowSprite)
        {
            return;
        }
        if (string.IsNullOrEmpty(name))
        {
            return;
        }

        int i = spriteMap [name] == null ? -1 : (int)spriteMap [name];

        if (i < 0)
        {
            return;
        }

        UISpriteData sp = mSprites.Count > 1 ? mSprites [i] : null;

        if (sp == null)
        {
            return;
        }

        int rc = retainCounter [sp.path] == null ? 0 : (int)retainCounter [sp.path];

        if (rc <= 0)
        {
            if (!force && sp.lastReturnTime - System.DateTime.Now.ToFileTime() > 0)
            {
                if (!releaseTex.Contains(name))
                {
                    releaseTex.Enqueue(name);
                }
                CancelInvoke("ReleaseTexture");
                Invoke("ReleaseTexture", 5);
                return;
            }
            Material mat = materailPool[sp.path] as Material;
            if (mat != null && mat.mainTexture != null)
            {
                try {
                    //=================================================
                                        #if UNITY_EDITOR
                    if (Coolape.CLAssetsManager.self != null &&
                        !string.IsNullOrEmpty(Coolape.CLAssetsManager.self.debugKey) &&
                        sp.path.Contains(Coolape.CLAssetsManager.self.debugKey))
                    {
                        Debug.LogError("doReleaseTexture====" + sp.path);
                    }
                                        #endif
                    //if(!string.IsNullOrEmpty(mat.mainTexture.name)) {
                    //	Resources.UnloadAsset (mat.mainTexture);
                    //}

                    // 其它引用了该图的都要释放
                    for (int j = 0; j < mSprites.Count; j++)
                    {
                        UISpriteData tmpsp = mSprites [j];
                        if (tmpsp.path == sp.path && tmpsp != sp)
                        {
                            tmpsp.material = null;
                        }
                    }
                    sp.material           = null;
                    materailPool[sp.path] = null;

                    mat.mainTexture = null;
                    //DestroyImmediate (mat.mainTexture, true);
                    DestroyImmediate(mat, true);
                    mat = null;
                    AssetBundle ab = assetBundleMap[sp.path] as AssetBundle;
                    if (ab != null)
                    {
                        ab.Unload(true);
                    }
                    assetBundleMap[sp.path] = null;
                    //=================================================
                } catch (System.Exception e) {
                    Debug.LogError(sp.name + " err:" + e);
                }
            }
        }
    }