Esempio n. 1
0
        /// <summary>
        /// 从Atlas集合中加载Sprite对象
        /// </summary>
        /// <param name="spriteName">精灵图片名称</param>
        /// <param name="atlasName">图集名称:Atlas/common/common-0</param>
        /// <returns></returns>
        public static Sprite LoadSpriteFromAtlasBundle(GameObject owener, string spriteName, string atlasName)
        {
            if (AppConst.AssetBundleMode)
            {
                return(AssetBundleManager.Instance.LoadSprite(owener, spriteName, atlasName));
            }
#if UNITY_EDITOR
            using (zstring.Block())
            {
                if (atlasName.Contains("-"))
                {
                    Debug.LogErrorFormat("atlas name contains \'-\', please check your code: [sprite]{0}, [atlas]{1}", spriteName, atlasName);
                }
                zstring dir = Path.GetDirectoryName(atlasName);

                zstring extension = Path.GetExtension(spriteName);
                if (!zstring.IsNullOrEmpty(extension))
                {
                    spriteName = spriteName.Replace(extension, "");
                }

                UIAtlasCache atlas = null;
                if (!atlasMap.TryGetValue(dir, out atlas))
                {
                    zstring       rootDir      = RuntimeAssetsRoot + dir;
                    string[]      textureGuids = AssetDatabase.FindAssets("t:Texture", new string[] { rootDir });
                    List <Sprite> sprites      = new List <Sprite>();
                    for (int i = 0; i < textureGuids.Length; i++)
                    {
                        zstring relativePath = AssetDatabase.GUIDToAssetPath(textureGuids[i]);
                        zstring childDir     = Path.GetDirectoryName(relativePath);
                        if (!childDir.Equals(rootDir))
                        {
                            continue;                            //只遍历顶层目录
                        }
                        var assets = AssetDatabase.LoadAllAssetsAtPath(relativePath);

                        foreach (var asset in assets)
                        {
                            if (asset is Sprite)
                            {
                                sprites.Add(asset as Sprite);
                            }
                        }
                    }
                    atlas         = new UIAtlasCache(sprites.ToArray());
                    atlasMap[dir] = atlas;
                }

                return(atlas.GetSprite(spriteName));
            }
#endif
            return(null);
        }
Esempio n. 2
0
        public Sprite LoadSprite(GameObject owner, string spriteName, string atlasName)
        {
            using (zstring.Block())
            {
                zstring assetName = atlasName.ToLower();
                zstring ext       = Path.GetExtension(assetName);
                if (!string.IsNullOrEmpty(ext))
                {
                    assetName = assetName.Replace(ext, "");
                }

                string bundleRelativePath = assetName;
                if (!assetbundleMap.TryGetValue(assetName, out bundleRelativePath))
                {
                    Debug.LogWarning(zstring.Format("{0} has no assetbundle resource", assetName));
                    return(null);
                }

                UIAtlasCache atlasCache = null;
                if (!atlasMap.TryGetValue(bundleRelativePath, out atlasCache))
                {
                    AssetBundle bundle = TryGetBundleByFile(assetName);
                    if (bundle == null)
                    {
                        return(null);
                    }

                    var assets = bundle.LoadAllAssets <Sprite>();
                    if (assets == null)
                    {
                        Debug.LogWarning(zstring.Format("Cant find sprite: {0} in bundle {1}", spriteName, bundleRelativePath));
                    }

                    atlasCache = new UIAtlasCache(assets);
                    atlasMap[bundleRelativePath] = atlasCache;
                }

                Sprite         sprite  = atlasCache.GetSprite(spriteName);
                TextureWatcher watcher = owner.transform.GetOrAddComponent <TextureWatcher>();
                watcher.AddBundleName(bundleRelativePath);

                return(sprite);
            }
        }