示例#1
0
    private void OnSpriteSheetLoaded()
    {
        string path = AssetDatabase.GetAssetPath(SpriteSheet.Texture);

        Sprite[] foundSprites = AssetDatabase.LoadAllAssetsAtPath(path).OfType <Sprite>().ToArray();
        sprites                 = foundSprites.ToList();
        selectedSprite          = 0;
        selectedAttachmentIndex = 0;
        gridSpriteNames.Clear();

        foreach (Sprite sprite in sprites)
        {
            bool present = false;
            foreach (SerializedSpriteAttachmentSprite attSprite in SpriteSheet.AttachmentSprites)
            {
                if (sprite.name == attSprite.Name)
                {
                    present = true;
                    break;
                }
            }

            if (!present)
            {
                SerializedSpriteAttachmentSprite ssaSprite = new SerializedSpriteAttachmentSprite();
                ssaSprite.Name = sprite.name;
                SpriteSheet.AttachmentSprites.Add(ssaSprite);
            }
        }

        foreach (Sprite sprite in sprites)
        {
            foreach (SerializedSpriteAttachmentSprite attSprite in SpriteSheet.AttachmentSprites)
            {
                if (sprite.name == attSprite.Name)
                {
                    attSprite.pivot       = sprite.pivot;
                    attSprite.textureRect = sprite.rect;
                }
            }
        }

        foreach (Sprite sprite in sprites)
        {
            SerializedSpriteAttachmentSprite ssaSprite = new SerializedSpriteAttachmentSprite();
            ssaSprite.Name = sprite.name;
            gridSpriteNames.Add(ssaSprite.Name);
        }

        if (sprites.Count > selectedSprite)
        {
            CalculateUniScale(sprites[selectedSprite]);
        }
        Repaint();
    }
示例#2
0
    protected override void OnGUILeftView()
    {
        EditorGUIUtility.labelWidth = 80;
        EditorGUILayout.LabelField("");

        if (SpriteSheet == null)
        {
            gridSpriteNames.Clear();
            selectedSprite = 0;
        }

        if (SpriteSheet != null)
        {
            GUILayout.Space(10);
            showSprites = EditorGUILayout.Foldout(showSprites, "Sprites");

            if (showSprites)
            {
                selectedSprite = GUILayout.SelectionGrid(selectedSprite, gridSpriteNames.ToArray(), 1);
            }

            showAttachments = EditorGUILayout.Foldout(showAttachments, "Attachments");

            if (showAttachments)
            {
                EditorGUILayout.BeginHorizontal();
                GUI.backgroundColor = Color.green;

                if (popup == null)
                {
                    popupActive = false;
                }

                if (GUILayout.Button("Create") && !popupActive)
                {
                    popupActive         = true;
                    popup               = GetWindow <EditorPopupEnterString>();
                    popup.m_DemandInput = true;
                    popup.Focus();
                    popup.OnConfirm += (string name) =>
                    {
                        foreach (SerializedSpriteAttachmentSprite sprite in SpriteSheet.AttachmentSprites)
                        {
                            if (sprite.Attachments.SingleOrDefault(x => x.Name == name) != null)
                            {
                                Debug.LogError(string.Format("[SAEditor] Sprite Attachment with name '{0}' already exists.", name));
                                break;
                            }
                            SerializedSpriteAttachment attachment = new SerializedSpriteAttachment();
                            attachment.Name = name;
                            Undo.RecordObject(SpriteSheet, "Add Attachment");
                            sprite.Attachments.Add(attachment);
                            dirty = true;
                        }
                        Repaint();
                        popupActive = false;
                    };
                }


                GUI.backgroundColor = Color.red;

                if (GUILayout.Button("Delete"))
                {
                    foreach (SerializedSpriteAttachmentSprite sprite in SpriteSheet.AttachmentSprites)
                    {
                        SerializedSpriteAttachment toDelete = null;
                        foreach (SerializedSpriteAttachment attachment in sprite.Attachments)
                        {
                            if (sprite.Attachments[selectedAttachmentIndex].Name == attachment.Name)
                            {
                                toDelete = attachment;
                                break;
                            }
                        }

                        if (toDelete != null)
                        {
                            Undo.RecordObject(SpriteSheet, "Delete Attachment");
                            sprite.Attachments.Remove(toDelete);
                            dirty = true;
                        }
                    }
                    selectedAttachmentIndex = 0;
                    Repaint();
                }

                GUI.backgroundColor = Color.white;
                EditorGUILayout.EndHorizontal();

                if (SpriteSheet.AttachmentSprites.Count > 0)
                {
                    string spriteName = sprites[selectedSprite].name;
                    SerializedSpriteAttachmentSprite sprite = null;
                    foreach (SerializedSpriteAttachmentSprite s in SpriteSheet.AttachmentSprites)
                    {
                        if (s.Name == spriteName)
                        {
                            sprite = s;
                            break;
                        }
                    }

                    if (sprite.Attachments.Count > 0)
                    {
                        List <string> attachmentNames = new List <string>();
                        sprite.Attachments.ForEach(x => attachmentNames.Add(x.Name));
                        selectedAttachmentIndex = GUILayout.SelectionGrid(selectedAttachmentIndex, attachmentNames.ToArray(), 1);
                    }
                }
            }
        }
        else
        {
            GUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            GUILayout.BeginVertical();
            GUILayout.FlexibleSpace();
            GUILayout.Label("Select Sprite Attachment Sheet Asset");
            GUILayout.FlexibleSpace();
            GUILayout.EndVertical();
            GUILayout.FlexibleSpace();
            GUILayout.EndHorizontal();
        }
    }