Пример #1
0
    public static AssetBundleLoadAssetOperation LoadAsset(string assetBundleName, string assetName, Type type, string manifestAssetBundleName = null)
    {
        return(new AssetBundleLoadAssetOperationSimulation(LoadedAssetBundle.Load(assetBundleName, assetName)?.LoadAsset(assetName, type)));
//		return Cache.AssetABM(assetBundleName, assetName, type);
//		if (Cache.AssetABM(assetBundleName, assetName, type, manifestAssetBundleName, out AssetBundleLoadAssetOperation cached))
//			return cached;
//		return _LoadAsset(assetBundleName, assetName, type, manifestAssetBundleName);
    }
Пример #2
0
    public static string[] GetAllAssetName(string assetBundleName, bool _WithExtension = true, string manifestAssetBundleName = null, bool isAllCheck = false)
    {
        var ab = LoadedAssetBundle.Load(assetBundleName);

        if (ab == null)
        {
            return(null);
        }
        return((!_WithExtension) ? ab.GetAllAssetNames().Select(Path.GetFileNameWithoutExtension).ToArray() : ab.GetAllAssetNames().Select(Path.GetFileName).ToArray());
    }
Пример #3
0
    public static string[] FindAllAssetName(string assetBundleName, string _regex, bool _WithExtension = true, RegexOptions _options = RegexOptions.None)
    {
        var ab = LoadedAssetBundle.Load(assetBundleName);

        if (ab == null)
        {
            return(null);
        }
        _regex = _regex.ToLower();
        string[] result = (!_WithExtension) ? (from v in ab.GetAllAssetNames().Select(Path.GetFileNameWithoutExtension)
                                               where CheckRegex(v, _regex, _options)
                                               select v).ToArray() : (from v in ab.GetAllAssetNames().Select(Path.GetFileName)
                                                                      where CheckRegex(v, _regex, _options)
                                                                      select v).ToArray();
        return(result);
    }
Пример #4
0
    public static IEnumerator GetSpriteAsync(string bundle, string name, Action <Sprite> ret)
    {
        var ab = LoadedAssetBundle.Load(bundle);

        if (ab != null)
        {
            yield return(ab.LoadAssetAsync(name, typeof(Texture2D), (tex) =>
            {
                var t = tex as Texture2D;
                if (t)
                {
                    ret(Sprite.Create(t, new Rect(0f, 0f, (float)t.width, (float)t.height), new Vector2(0.5f, 0.5f)));
                }
                else
                {
                    ret(null);
                }
            }));
        }
        ret(null);
    }
Пример #5
0
    public static bool GetSprite(string bundle, string name, out Sprite ret)
    {
        ret = null;
        var lab = LoadedAssetBundle.Load(bundle);

        if (lab == null)
        {
            return(false);
        }
        if (!caching)
        {
            var t = LoadedAssetBundle.Load(bundle)?.LoadAsset(name, typeof(Texture2D)) as Texture2D;
            if (t == null)
            {
                return(false);
            }
            ret = Sprite.Create(t, new Rect(0f, 0f, (float)t.width, (float)t.height), new Vector2(0.5f, 0.5f));
            return(true);
        }
        Debug.Log("Trying to get sprite for ", bundle, name);
        if (lab.cachedSprites.TryGetValue(name, out int idx))
        {
            /*if (scache.TryGetValue(idx, out Sprite sc))
             * {
             *      if (sc)
             *              sc = Sprite.Instantiate(sc);
             *      return sc;
             * }*/
            Debug.Log("potential cache hit at ", idx, atlas.Length);
            int atlasno = (idx / spriteCount);
            int off     = idx % spriteCount;
            // load atlases up to that count
            for (int i = atlas.Length; i <= atlasno; i++)
            {
                var atn = Dir.cache + "abinfo" + i;
                if (File.Exists(atn))
                {
#if USE_BC7
                    var fmt  = TextureFormat.BC7;
                    var dbuf = new byte[atlasDim * atlasDim];
#elif USE_DXT
                    var fmt  = TextureFormat.DXT5;
                    var dbuf = new byte[atlasDim * atlasDim];
#else
                    var fmt  = TextureFormat.RGBA32;
                    var dbuf = new byte[4 * atlasDim * atlasDim];
#endif
                    Texture2D at = new Texture2D(atlasDim, atlasDim, fmt, false);
                    at.LoadRawTextureData(Ext.LZ4Decompress(File.ReadAllBytes(atn), dbuf));
                    at.Apply(updateMipmaps: false, makeNoLongerReadable: true);
                    if (atlas.Length <= i)
                    {
                        Array.Resize(ref atlas, i + 1);
                    }
                    Texture2D.DontDestroyOnLoad(at);
                    atlas[i] = at;
                }
                else
                {
                    Debug.Log("This cache index ", idx, " doesn't exist but should! (atlasno ", i, ")", atn);
                    break;
                }
            }
            if (atlasno < atlas.Length)
            {
                Debug.Log("Sprite within atlas range");
                int x  = (off % perDim) * 128;
                int y  = (off / perDim) * 128;
                var st = Sprite.Create(atlas[atlasno], new Rect(x, y, 128, 128), new Vector2(0.5f, 0.5f));
                //Sprite.DontDestroyOnLoad(st);
                //scache[idx] = st;
                ret = st;                // Sprite.Instantiate(st);
                return(true);
            }
            if (atlasno == atlas.Length)
            {
                if (off < spriteLen)
                {
                    Debug.Log("Found the sprite in pending set");
                    return(spritePending[off] ? Sprite.Instantiate(spritePending[off]) : null);
                }
            }
            Debug.Error("Corrupted sprite cache. This shouldn't happen.", atlasno, atlas.Length, off, spriteLen);
        }
        Debug.Log("cache miss. we need to cache the sprite afresh.");
        var tex      = LoadedAssetBundle.Load(bundle)?.LoadAsset(name, typeof(Texture2D)) as Texture2D;
        int localidx = spriteLen;
        lab.cachedSprites[name] = localidx + atlas.Length * spriteCount;
        spriteLen = localidx + 1;
        if (tex != null)
        {
        }

        /*scache[idx] = tex ? Sprite.Create(tex, new Rect(0f, 0f, (float)tex.width, (float)tex.height), new Vector2(0.5f, 0.5f)) : null;
         * if (tex != null)
         *      Sprite.DontDestroyOnLoad(scache[idx]);
         * spritePending[localidx] = scache[idx];
         * if (tex != null)
         *      ret = Sprite.Instantiate(scache[idx]);
         */
        ret = spritePending[localidx] = tex ? Sprite.Create(tex, new Rect(0f, 0f, (float)tex.width, (float)tex.height), new Vector2(0.5f, 0.5f)) : null;
        if (ret != null)
        {
            Sprite.DontDestroyOnLoad(ret);
        }
        // swap out the pending ones into a new texture if full
        if (spriteLen == spriteCount)
        {
            Debug.Log("sprite atlas full, flushing");
            Texture2D at = new Texture2D(atlasDim, atlasDim, TextureFormat.RGBA32, false);

            RenderTexture tmp = RenderTexture.GetTemporary(128, 128);
            tmp.filterMode       = FilterMode.Point;
            RenderTexture.active = tmp;
            for (int off = 0; off < spriteCount; off++)
            {
                if (spritePending[off] == null)
                {
                    continue;
                }
                int x = (off % perDim) * 128;
                int y = (off / perDim) * 128;
                Graphics.Blit(spritePending[off].texture, tmp);                 // blit and stretch the original to our 128x128 render target
                at.ReadPixels(new Rect(0, 0, 128, 128), x, y);
                spritePending[off] = null;
            }
            at.Apply();
            RenderTexture.active = null;
            int atno = atlas.Length;
            Array.Resize(ref atlas, atno + 1);
            atlas[atno] = at;
#if GAME_DEBUG
            File.WriteAllBytes(Dir.cache + "abinfo" + atno + ".png", at.EncodeToPNG());
#endif

#if USE_BC7
            var rawbuf = at.GetRawTextureData();
            var bc7buf = new byte[rawbuf.Length / 4];
            unsafe
            {
                fixed(byte *pbc7buf = bc7buf)
                {
                    fixed(byte *prawbuf = rawbuf)
                    bc7_compress(new IntPtr(pbc7buf), new IntPtr(prawbuf), atlasDim, atlasDim);
                }
            }
            rawbuf = bc7buf;
#elif USE_DXT
            at.Compress(false);
            var rawbuf = at.GetRawTextureData();
#else
            var rawbuf = at.GetRawTextureData();
#endif
            File.WriteAllBytes(Dir.cache + "abinfo" + atno, Ext.LZ4Compress(rawbuf));

            spriteLen = 0;
            Save();
        }
        return(ret != null);
    }
Пример #6
0
 public static LoadedAssetBundle LoadAssetBundle(string assetBundleName, bool isAsync, string manifestAssetBundleName, string forasset)
 {
     return(LoadedAssetBundle.Load(assetBundleName, forasset));
 }