Пример #1
0
        public static void Pack()
        {
            Init();
            AtlasPackerHelper.ReApplyAllSprites();
            CreateAtlasSpriteHolder();
            Packer.RebuildAtlasCacheIfNeeded(EditorUserBuildSettings.activeBuildTarget, true, Packer.Execution.ForceRegroup);

            EditorUtility.ClearProgressBar();
        }
Пример #2
0
        static void Read()
        {
            AtlasPackerHelper.Init();
            if (AtlasPackerHelper.list == null)
            {
                AtlasPackerHelper.list = new AtlasTextureFormatList();
            }
            var dirs = Directory.GetDirectories(PathConfig.localFullPath2);

            for (int i = 0; i < dirs.Length; ++i)
            {
                dirs[i] = dirs[i].Replace('\\', '/');
                dirs[i] = dirs[i].Substring(dirs[i].LastIndexOf('/') + 1);
            }
            if (AtlasPackerHelper.list.list != null)
            {
                foreach (var item in AtlasPackerHelper.list.list)
                {
                    if (dirs.First(t => t == item.atlasName) == null)
                    {
                        AtlasPackerHelper.list.dict.Remove(item.atlasName);
                    }
                }
            }

            Save();
            foreach (var dir in dirs)
            {
                if (AtlasPackerHelper.list.dict.ContainsKey(dir))
                {
                    continue;
                }
                var item = new AtlasTextureFormatItem();
                item.atlasName = dir;
                item.iosFormat = item.androidFormat = item.standaloneFormat = TextureImporterFormat.RGBA32.ToString();
                AtlasPackerHelper.list.dict.Add(dir, item);
            }
        }
Пример #3
0
        private static void CreateAtlasSpriteHolder()
        {
            string path = Application.dataPath + PathConfig.localPath;

            string[] directories = Directory.GetDirectories(path);
            Array.Sort(directories);
            System.Collections.Generic.List <string> spriteList = new System.Collections.Generic.List <string>();
            List <string> atlasNames = new List <string>();
            Dictionary <string, string> spriteNameSet = new Dictionary <string, string>();

            for (int i = 0; i < directories.Length; i++)
            {
                EditorUtility.DisplayProgressBar("打包图集", "打包中...", 1f * i / directories.Length);

                string directoryPath = directories[i];
                directoryPath = directoryPath.Replace("\\", "/");
                string       directory    = directoryPath.Substring(directoryPath.LastIndexOf('/'));
                string       AtlasName    = Path.GetFileName(directoryPath);
                string[]     files        = Directory.GetFiles(directoryPath, "*.png");
                GameObject   holder       = new GameObject(AtlasName);
                SpriteHolder spriteHolder = holder.AddComponent <SpriteHolder>();
                spriteHolder.allSprites = new Sprite[files.Length];
                var          oldGo     = AssetDatabase.LoadAssetAtPath <GameObject>(PathConfig.atlasPath + AtlasName + ".prefab");
                SpriteHolder oldHolder = null;
                if (oldGo != null)
                {
                    oldHolder = oldGo.GetComponent <SpriteHolder>();
                }
                Array.Sort(files);
                spriteHolder.atlasName = AtlasName;
                atlasNames.Add(AtlasName);
                for (int k = 0; k < files.Length; k++)
                {
                    string filePath = files[k];
                    filePath = filePath.Replace('\\', '/');
                    int    start    = filePath.LastIndexOf(directory + '/') + 1;
                    string fileName = filePath.Substring(start);
                    //AssetDatabase.ImportAsset(PathConfig.localFullPath + fileName);

                    var assetImporter = AssetImporter.GetAtPath(PathConfig.localFullPath + fileName) as TextureImporter;
                    var packingTag    = AtlasPackerHelper.GetPackingTag(AtlasName);

                    if (assetImporter != null && assetImporter.spritePackingTag != packingTag)
                    {
                        assetImporter.spritePackingTag = packingTag;
                        AtlasPackerHelper.ApplyTexture(assetImporter, AtlasName);
                        assetImporter.SaveAndReimport();
                    }

                    UnityEngine.Object[] objects = AssetDatabase.LoadAllAssetsAtPath(PathConfig.localFullPath + fileName);
                    foreach (var obj in objects)
                    {
                        var sprite = obj as Sprite;
                        if (sprite == null)
                        {
                            if (!(obj is Texture2D))
                            {
                                Debug.LogError(fileName + "is null");
                            }
                            continue;
                        }
                        var guid = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(sprite));
                        spriteHolder.allSprites[k] = sprite;
                        start = fileName.LastIndexOf("/") + 1;
                        //fileName = sprite.name;// fileName.Substring(start, fileName.Length - start - ".png".Length);
                        if (spriteNameSet.ContainsKey(sprite.name))
                        {
                            Debug.LogErrorFormat("Sprite Name \"{0}\" Conflict in Atlas \"{1}\" and \"{2}\"", sprite.name, spriteNameSet[sprite.name], AtlasName);
                        }
                        else
                        {
                            spriteNameSet.Add(sprite.name, AtlasName);
                        }
                        spriteList.Add(guid + "." + sprite.name + "." + AtlasName);
                    }
                }
                bool changed = !(oldHolder != null && oldHolder.allSprites != null && oldHolder.allSprites.Length == spriteHolder.allSprites.Length && oldHolder.atlasName == spriteHolder.atlasName);
                if (!changed)
                {
                    for (int j = 0; j < oldHolder.allSprites.Length; ++j)
                    {
                        if (oldHolder.allSprites[j] != spriteHolder.allSprites[j])
                        {
                            changed = true;
                            break;
                        }
                    }
                }
                if (changed)
                {
                    PrefabUtility.CreatePrefab(PathConfig.atlasPath + AtlasName + ".prefab", holder);
                }
                DestroyImmediate(holder);
            }

            EditorUtility.DisplayProgressBar("打包图集", "打包中...", 1);

            GameObject         go            = new GameObject("SpritePath");
            TexturePathsHolder textureHolder = go.AddComponent <TexturePathsHolder>();

            textureHolder.files = new string[spriteList.Count];
            textureHolder.paths = new string[directories.Length];
            for (int i = 0; i < directories.Length; i++)
            {
                var holderName = directories[i].Substring(directories[i].IndexOf("ResourcesUI/") + "ResourcesUI/".Length);
                textureHolder.paths[i] = "Atlas/" + holderName;
            }
            for (int i = 0; i < spriteList.Count; ++i)
            {
                textureHolder.files[i] = spriteList[i];
            }
            var oldPaths = AssetDatabase.LoadAssetAtPath <GameObject>(PathConfig.atlasPath + "SpritePath" + ".prefab");
            TexturePathsHolder oldPathsHolder = null;

            if (oldPaths != null)
            {
                oldPathsHolder = oldPaths.GetComponent <TexturePathsHolder>();
            }
            bool pathChanged = oldPathsHolder == null ||
                               oldPathsHolder.files == null || oldPathsHolder.files.Length != textureHolder.files.Length ||
                               oldPathsHolder.paths == null || oldPathsHolder.paths.Length != textureHolder.paths.Length;

            if (!pathChanged)
            {
                for (int j = 0; j < oldPathsHolder.files.Length; ++j)
                {
                    if (oldPathsHolder.files[j] != textureHolder.files[j])
                    {
                        pathChanged = true;
                        break;
                    }
                }

                for (int i = 0; i < oldPathsHolder.paths.Length; i++)
                {
                    if (oldPathsHolder.paths[i] != textureHolder.paths[i])
                    {
                        pathChanged = true;
                        break;
                    }
                }
            }
            if (pathChanged)
            {
                PrefabUtility.CreatePrefab(PathConfig.atlasPath + "SpritePath" + ".prefab", go);
            }
            DestroyImmediate(go);
            AssetDatabase.SaveAssets();
            // Hotfire.SpriteProxy.instance.InitForEditor();
        }
Пример #4
0
 static void Save()
 {
     AtlasPackerHelper.list.list = AtlasPackerHelper.list.dict.Values.ToListFromPool();
     File.WriteAllText(AtlasPackerHelper.ATLAS_TEXTURE_FORMAT_CONFIG_PATH, JsonUtility.ToJson(AtlasPackerHelper.list));
     AtlasPackerHelper.Init();
 }