public void CustomInspector()
        {
            EditorLayoutTools.SetLabelWidth(80f);

            var sprite = (atlas != null) ? atlas.GetSpriteData(EditorAtlasPrefs.selectedSprite) : null;

            EditorGUILayout.Separator();

            if (atlas.Texture != null)
            {
                if (sprite == null && atlas.Sprites.Count > 0)
                {
                    string spriteName = EditorAtlasPrefs.selectedSprite;

                    if (!string.IsNullOrEmpty(spriteName))
                    {
                        sprite = atlas.GetSpriteData(spriteName);
                    }

                    if (sprite == null)
                    {
                        sprite = atlas.Sprites[0];
                    }
                }

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

                    if (tex != null)
                    {
                        EditorGUILayout.Separator();

                        DrawAdvancedSpriteField(atlas, sprite.name);

                        EditorGUILayout.Separator();

                        EditorLayoutTools.DrawContentTitle("Sprite Size");

                        using (new ContentsScope())
                        {
                            EditorLayoutTools.IntRangeField(null, "Width", "Height", sprite.width, sprite.height, false);
                        }

                        EditorLayoutTools.DrawContentTitle("Sprite Border");

                        using (new ContentsScope())
                        {
                            GUI.changed = false;

                            var borderA = EditorLayoutTools.DelayedIntRangeField(null, "Left", "Right", sprite.borderLeft, sprite.borderRight);
                            var borderB = EditorLayoutTools.DelayedIntRangeField(null, "Bottom", "Top", sprite.borderBottom, sprite.borderTop);

                            if (GUI.changed)
                            {
                                UnityEditorUtility.RegisterUndo("Atlas Change", atlas);

                                sprite.borderLeft   = borderA.x;
                                sprite.borderRight  = borderA.y;
                                sprite.borderBottom = borderB.x;
                                sprite.borderTop    = borderB.y;

                                atlas.CacheClear();
                            }
                        }

                        GUILayout.Space(2f);

                        using (new EditorGUILayout.HorizontalScope())
                        {
                            GUILayout.FlexibleSpace();

                            if (GUILayout.Button("Update"))
                            {
                                UnityEditorUtility.SaveAsset(atlas);
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        private void ApplyAtlas(AtlasAction action, List <Texture> textures)
        {
            if (action == AtlasAction.None)
            {
                return;
            }

            StartTextureEdit(selectAtlas, textures);

            if (action.HasFlag(AtlasAction.Create))
            {
                var path = string.Empty;

                if (string.IsNullOrEmpty(Prefs.exportPath) || !Directory.Exists(path))
                {
                    path = UnityPathUtility.AssetsFolder;
                }
                else
                {
                    path = Prefs.exportPath;
                }

                path = EditorUtility.SaveFilePanelInProject("Save As", "New Atlas.asset", "asset", "Save atlas as...", path);

                if (!string.IsNullOrEmpty(path))
                {
                    Prefs.exportPath = Path.GetDirectoryName(path) + PathUtility.PathSeparator;

                    // Create ScriptableObject for atlas.
                    var atlasTexture = ScriptableObjectGenerator.Generate <AtlasTexture>(path, false);

                    if (atlasTexture != null)
                    {
                        Selection.activeObject = atlasTexture;

                        OnSelectAtlas(atlasTexture);
                    }
                }
                else
                {
                    action = AtlasAction.None;
                }
            }
            else if (action.HasFlag(AtlasAction.Delete))
            {
                var sprites = new List <SpriteEntry>();
                ExtractSprites(selectAtlas, sprites);

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

                    if (deleteNames.Contains(ent.name))
                    {
                        sprites.RemoveAt(i);
                    }
                }

                UpdateAtlas(selectAtlas, sprites);

                deleteNames.Clear();
            }

            if (action.HasFlag(AtlasAction.Update))
            {
                UpdateAtlas(selectAtlas, textures, true);
            }
            else if (action.HasFlag(AtlasAction.Replace))
            {
                UpdateAtlas(selectAtlas, textures, false);
            }

            selectAtlas.CacheClear();

            FinishTextureEdit();

            UnityEditorUtility.SaveAsset(selectAtlas);
        }