Example #1
0
        private void ReplaceSprites(AtlasTexture atlas, List <SpriteEntry> sprites)
        {
            var spriteList = atlas.Sprites;
            var kept       = new List <SpriteData>();

            for (var i = 0; i < sprites.Count; ++i)
            {
                var se     = sprites[i];
                var sprite = AddSprite(spriteList, se);
                kept.Add(sprite);
            }

            for (int i = spriteList.Count; i > 0;)
            {
                var sp = spriteList[--i];

                if (!kept.Contains(sp))
                {
                    spriteList.RemoveAt(i);
                }
            }

            atlas.SortAlphabetically();

            EditorUtility.SetDirty(atlas);
        }
        //----- property -----

        //----- method -----

        private void StartTextureEdit(AtlasTexture atlas, List <Texture> textures)
        {
            AssetDatabase.StartAssetEditing();

            if (editableTextures == null)
            {
                editableTextures = new Dictionary <string, EditableTexture>();
            }
            else
            {
                foreach (var target in editableTextures.Values)
                {
                    target.Restore();
                }

                editableTextures.Clear();
            }

            if (atlas != null)
            {
                var texture = atlas.Texture as Texture2D;

                RegisterEditableTexture(texture, true);
            }

            foreach (var texture in textures)
            {
                RegisterEditableTexture(texture, false);
            }

            AssetDatabase.StopAssetEditing();
            AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
        }
        private void UpdateAtlas(AtlasTexture atlas, List <Texture> textures, bool keepSprites)
        {
            if (atlas == null)
            {
                return;
            }

            Texture2D texture = null;

            texture = atlas.Texture as Texture2D;

            var sprites = CreateSprites(textures);

            if (sprites.Count > 0)
            {
                if (keepSprites)
                {
                    ExtractSprites(atlas, sprites);
                }

                UpdateAtlas(atlas, sprites);
            }
            else if (!keepSprites)
            {
                UpdateAtlas(atlas, sprites);
            }

            texture = atlas.Texture as Texture2D;

            if (texture != null)
            {
                SaveTextureMetaData(texture, sprites);
            }
        }
        private static void DrawAdvancedSpriteField(AtlasTexture atlas, string spriteName)
        {
            if (atlas == null)
            {
                return;
            }

            if (atlas.Sprites.Count == 0)
            {
                EditorGUILayout.HelpBox("No sprites found", MessageType.Warning);
                return;
            }

            GUILayout.BeginHorizontal();
            {
                if (EditorLayoutTools.DrawPrefixButton("Sprite"))
                {
                    EditorAtlasPrefs.atlas          = atlas;
                    EditorAtlasPrefs.selectedSprite = spriteName;
                    AtlasSpriteSelector.Show(SelectSprite);
                }

                EditorGUILayout.SelectableLabel(spriteName, new GUIStyle("TextArea"), GUILayout.Height(18f));
            }
            GUILayout.EndHorizontal();
        }
Example #5
0
        private List <Texture> GetSelectedTextures(AtlasTexture atlas)
        {
            var textures = new List <Texture>();

            if (Selection.objects != null && Selection.objects.Length > 0)
            {
                foreach (var o in Selection.objects)
                {
                    var tex = o as Texture;

                    if (tex == null)
                    {
                        continue;
                    }

                    if (tex.name == "Font Texture")
                    {
                        continue;
                    }

                    if (atlas != null && atlas.Texture != null)
                    {
                        if (tex.GetInstanceID() == atlas.Texture.GetInstanceID())
                        {
                            continue;
                        }
                    }

                    textures.Add(tex);
                }
            }

            return(textures);
        }
Example #6
0
        private void OnSelectAtlas(UnityEngine.Object obj)
        {
            if (EditorAtlasPrefs.atlas != obj || obj == null)
            {
                EditorAtlasPrefs.atlas = obj as AtlasTexture;

                if (EditorAtlasPrefs.atlas != null)
                {
                    selectAtlas   = EditorAtlasPrefs.atlas;
                    padding       = selectAtlas.Padding;
                    pixelsPerUnit = selectAtlas.PixelsPerUnit;
                    filterMode    = selectAtlas.FilterMode;
                    unityPacking  = selectAtlas.UnityPacking;
                    forceSquare   = selectAtlas.ForceSquare;
                }
                else
                {
                    selectAtlas  = null;
                    padding      = 0;
                    filterMode   = FilterMode.Bilinear;
                    unityPacking = false;
                    forceSquare  = true;
                }

                Repaint();
            }
        }
Example #7
0
        //----- property -----

        //----- method -----

        private void ExtractSprites(AtlasTexture atlas, List <SpriteEntry> finalSprites)
        {
            var tex = atlas.Texture as Texture2D;

            if (tex != null)
            {
                Color32[] pixels = null;

                var width   = tex.width;
                var height  = tex.height;
                var sprites = atlas.Sprites;
                var count   = sprites.Count;
                var index   = 0;

                foreach (var es in sprites)
                {
                    var progress = (float)index++ / count;

                    EditorUtility.DisplayProgressBar("progress", "Updating the atlas...", progress);

                    var found = false;

                    foreach (var fs in finalSprites)
                    {
                        if (!string.IsNullOrEmpty(es.guid) && es.guid == fs.guid)
                        {
                            found = true;
                        }
                        else if (es.name == fs.name)
                        {
                            found = true;
                        }

                        if (found)
                        {
                            fs.CopyBorderFrom(es);
                            break;
                        }
                    }

                    if (!found)
                    {
                        if (pixels == null)
                        {
                            pixels = tex.GetPixels32();
                        }

                        var sprite = ExtractSprite(es, pixels, width, height);

                        if (sprite != null)
                        {
                            finalSprites.Add(sprite);
                        }
                    }
                }
            }

            EditorUtility.ClearProgressBar();
        }
Example #8
0
        private bool UpdateTexture(AtlasTexture atlas, List <SpriteEntry> sprites)
        {
            var texture = atlas.Texture as Texture2D;

            var oldPath = (texture != null) ? AssetDatabase.GetAssetPath(texture.GetInstanceID()) : string.Empty;
            var newPath = GetSaveableTexturePath(atlas);

            if (System.IO.File.Exists(newPath))
            {
                System.IO.FileAttributes newPathAttrs = System.IO.File.GetAttributes(newPath);
                newPathAttrs &= ~System.IO.FileAttributes.ReadOnly;
                System.IO.File.SetAttributes(newPath, newPathAttrs);
            }

            var newTexture = (texture == null || oldPath != newPath);

            if (newTexture)
            {
                texture = TextureUtility.CreateEmptyTexture(1, 1, TextureFormat.ARGB32);
            }
            else
            {
                RegisterEditableTexture(texture, true);
            }

            if (PackTextures(texture, sprites, padding, unityPacking, forceSquare))
            {
                var bytes = texture.EncodeToPNG();

                System.IO.File.WriteAllBytes(newPath, bytes);
                bytes = null;

                AssetDatabase.ImportAsset(newPath);

                texture = AssetDatabase.LoadMainAssetAtPath(newPath) as Texture2D;

                if (newTexture)
                {
                    Reflection.SetPrivateField <AtlasTexture, Texture>(atlas, "texture", texture);

                    ReleaseSprites(sprites);
                }
            }
            else
            {
                EditorUtility.DisplayDialog("Operation Canceled", "The selected sprites can't fit into the atlas.", "OK");

                return(false);
            }

            return(true);
        }
        private SpriteEntry ExtractSprite(AtlasTexture atlas, string identifier)
        {
            if (atlas.Texture == null)
            {
                return(null);
            }

            var sd = atlas.GetSpriteData(identifier);

            if (sd == null)
            {
                return(null);
            }

            var se = ExtractSprite(sd, atlas.Texture as Texture2D);

            return(se);
        }
Example #10
0
        private void SaveTextureMetaData(Texture2D texture, List <SpriteEntry> sprites)
        {
            var spritesheet = new List <SpriteMetaData>();
            var path        = AssetDatabase.GetAssetPath(texture.GetInstanceID());

            // TextureImporter.
            var textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;

            if (textureImporter != null)
            {
                textureImporter.textureType         = TextureImporterType.Sprite;
                textureImporter.spriteImportMode    = SpriteImportMode.Multiple;
                textureImporter.filterMode          = filterMode;
                textureImporter.spritePixelsPerUnit = pixelsPerUnit;

                for (var i = 0; i < sprites.Count; ++i)
                {
                    var sprite = sprites[i];

                    var metaData = new SpriteMetaData();

                    var rect = new Rect(new Vector2(sprite.x, sprite.y), new Vector2(sprite.width, sprite.height));

                    metaData.name = sprite.name;
                    metaData.rect = AtlasTexture.ConvertToSpriteSheetPixels(rect, texture.width, texture.height);

                    spritesheet.Add(metaData);
                }

                textureImporter.spritesheet = spritesheet.ToArray();

                // TextureImporterSettings.
                var textureImporterSettings = new TextureImporterSettings();
                textureImporter.ReadTextureSettings(textureImporterSettings);

                textureImporterSettings.spriteMeshType = SpriteMeshType.FullRect;
                textureImporterSettings.readable       = false;
                textureImporterSettings.spriteGenerateFallbackPhysicsShape = false;

                textureImporter.SetTextureSettings(textureImporterSettings);

                AssetDatabase.ImportAsset(path);
            }
        }
Example #11
0
        private static void UpdateAtlasTextureImage(GameObject[] gameObjects, AtlasTexture atlas)
        {
            var atlasAssetPath = AssetDatabase.GetAssetPath(atlas);

            foreach (var gameObject in gameObjects)
            {
                var atlasTextureImage = UnityUtility.GetComponent <AtlasTextureImage>(gameObject);

                if (atlasTextureImage != null && atlasTextureImage.Atlas != null)
                {
                    var path = AssetDatabase.GetAssetPath(atlasTextureImage.Atlas);

                    if (path == atlasAssetPath)
                    {
                        atlasTextureImage.Apply();
                    }
                }
            }
        }
Example #12
0
        private void UpdateAtlas(AtlasTexture atlas, List <SpriteEntry> sprites)
        {
            if (atlas == null)
            {
                return;
            }

            if (sprites.Count > 0)
            {
                if (UpdateTexture(atlas, sprites))
                {
                    ReplaceSprites(atlas, sprites);
                }

                ReleaseSprites(sprites);

                atlas.Padding       = padding;
                atlas.PixelsPerUnit = pixelsPerUnit;
                atlas.FilterMode    = filterMode;
                atlas.UnityPacking  = unityPacking;
                atlas.ForceSquare   = forceSquare;
            }
            else
            {
                atlas.Sprites.Clear();

                var path = GetSaveableTexturePath(atlas);

                atlas.Padding       = 0;
                atlas.PixelsPerUnit = 100f;
                atlas.FilterMode    = FilterMode.Bilinear;
                atlas.UnityPacking  = unityPacking;
                atlas.ForceSquare   = forceSquare;

                if (!string.IsNullOrEmpty(path))
                {
                    AssetDatabase.DeleteAsset(path);
                }
            }

            EditorUtility.ClearProgressBar();
        }
Example #13
0
        private List <SpriteEntry> ExtractAllSprite(AtlasTexture atlas)
        {
            var sprites = new List <SpriteEntry>();

            if (atlas.Texture == null)
            {
                return(null);
            }

            foreach (var sd in atlas.Sprites)
            {
                var identifier = string.IsNullOrEmpty(sd.guid) ? sd.name : sd.guid;

                var se = ExtractSprite(atlas, identifier);

                if (se != null)
                {
                    sprites.Add(se);
                }
            }

            return(sprites);
        }
Example #14
0
        public string GetSaveableTexturePath(AtlasTexture atlas)
        {
            const string TextureExtension = ".png";

            string path = "";

            if (atlas.Texture != null)
            {
                path = AssetDatabase.GetAssetPath(atlas.Texture.GetInstanceID());

                if (!string.IsNullOrEmpty(path))
                {
                    var dot = path.LastIndexOf('.');

                    return(path.Substring(0, dot) + TextureExtension);
                }
            }

            path = AssetDatabase.GetAssetPath(atlas.GetInstanceID());
            path = string.IsNullOrEmpty(path) ? "Assets/" + atlas.name + TextureExtension : path.Replace(".asset", TextureExtension);

            return(path);
        }
        public override void OnInspectorGUI()
        {
            atlas = target as AtlasTexture;

            CustomInspector();
        }