Exemplo n.º 1
0
        /// <summary>
        /// Create the sprite layers for each layer in the document.
        /// </summary>
        void CreateSpriteLayers()
        {
            // safety first
            if (document == null ||
                document.allLayers == null
                )
            {
                return;
            }

            // with each photoshop layer
            foreach (Layer layer in document.allLayers)
            {
                // create a sprite layer
                UnitySpriteLayer spriteLayer = ScriptableObject.CreateInstance <UnitySpriteLayer>();

                // that holds these details
                spriteLayer.spriteLayerStyle = SpriteLayerStyle.None;
                spriteLayer.layer            = layer;
                spriteLayer.name             = layer.photoshopLayerName;
                spriteLayer.isVisible        = layer.isVisible;

                // add to our list
                spriteLayers.Add(spriteLayer);
            }

            // put them in the correct order
            spriteLayers.Sort(delegate(UnitySpriteLayer a, UnitySpriteLayer b)
            {
                return(a.layer.order.CompareTo(b.layer.order));
            });
        }
Exemplo n.º 2
0
        /// <summary>
        /// Assign the sprites from a spritesheet.
        /// </summary>
        public static void AssignSpritesFromSpritesheet(Layout layout)
        {
            layout.ResetSpriteLayers();
            string spritesheetPath = AssetDatabase.GetAssetPath(layout.spritesheetTexture);

            Object[] allSprites = AssetDatabase.LoadAllAssetRepresentationsAtPath(spritesheetPath);

            // with each layer in the document
            foreach (Layer layer in layout.document.allLayers)
            {
                UnitySpriteLayer spriteLayer = layout.FindSpriteLayer(layer);
                spriteLayer.ResetSprites();

                // with each sprite inside the spritesheet
                foreach (Sprite sprite in allSprites)
                {
                    // with each guess of the layer's name
                    foreach (string guess in layer.GetGuessesForSpriteName())
                    {
                        // did we match the sprite in the spritesheet?
                        if (sprite.name == guess)
                        {
                            spriteLayer.sprite           = sprite;
                            spriteLayer.name             = sprite.name;
                            spriteLayer.bounds           = sprite.bounds;
                            spriteLayer.spriteLayerStyle = SpriteLayerStyle.UnitySprite;
                            break;
                        }
                    }
                }
            }
            layout.ResetHierarchyLabels();
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the best name for this layer.  It's either the name of the layer,
        /// or the name of the sprite if we've got one assigned.
        /// </summary>
        /// <param name="layer">The layer.</param>
        /// <returns>The best name.</returns>
        public string GetBestName(Layer layer)
        {
            // lookup the sprite layer
            UnitySpriteLayer spriteLayer = FindSpriteLayer(layer);

            // set the best name
            string bestName = (spriteLayer != null && spriteLayer.hasSprite) ? spriteLayer.name : layer.photoshopLayerName;

            // replace our POWER .. ower .. ower .. TOKEN .. token .. token
            bestName = bestName.Replace("#ps2d", "");

            return(bestName.Trim());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Assign the sprites from the folder to existing sprite layers.
        /// </summary>
        public static void AssignSpritesFromFolder(Layout layout)
        {
            layout.ResetSpriteLayers();
            string folder = AssetDatabase.GetAssetPath(layout.imageSourceAssetFolder);

            List <string> assetPaths = new List <string>(AssetDatabase.GetAllAssetPaths());

            assetPaths.RemoveAll(each => !each.StartsWith(folder));

            // with each document layer
            foreach (Layer layer in layout.document.allLayers)
            {
                UnitySpriteLayer spriteLayer = layout.FindSpriteLayer(layer);
                spriteLayer.ResetSprites();

                // with each matching asset path
                foreach (string assetPath in assetPaths)
                {
                    // extra the filename
                    string onlyFilename = Path.GetFileNameWithoutExtension(assetPath);

                    // and go through each guess at the layer's sprite name
                    foreach (string guess in layer.GetGuessesForSpriteName())
                    {
                        // match?
                        if (onlyFilename == guess)
                        {
                            spriteLayer.sprite           = (Sprite)AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite));
                            spriteLayer.name             = onlyFilename;
                            spriteLayer.bounds           = spriteLayer.sprite.bounds;
                            spriteLayer.spriteLayerStyle = SpriteLayerStyle.UnitySprite;
                            break;
                        }
                    }
                }
            }

            // reset the labels
            layout.ResetHierarchyLabels();
        }
Exemplo n.º 5
0
        /// <summary>
        /// Create the nested layers.
        /// </summary>
        /// <param name="layer">The layer to start from.</param>
        /// <param name="parentTransform">Where to attach.</param>
        /// <returns>The game object</returns>
        public GameObject CreateGameObjectHierarchyForLayer(Layer layer, Transform parentTransform)
        {
            UnitySpriteLayer spriteLayer = layout.FindSpriteLayer(layer);

            // sanity
            if (spriteLayer == null)
            {
                return(null);
            }

            // don't worry about this layer ... not a sprite, no kids.
            if (!spriteLayer.hasSprite && !spriteLayer.hasChildren)
            {
                return(null);
            }

            GameObject layerGameObject;

            if (spriteLayer.hasSprite)
            {
                layerGameObject = CreateGameObjectForSpriteLayer(spriteLayer, parentTransform);
            }
            else
            {
                string layerName = layout.GetBestName(layer);
                layerGameObject = new GameObject(layerName);
                layerGameObject.transform.parent = parentTransform;
            }

            if (layer.layers != null)
            {
                foreach (Layer childLayer in layer.layers)
                {
                    CreateGameObjectHierarchyForLayer(childLayer, layerGameObject.transform);
                }
            }

            return(layerGameObject);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Assign the sprites from a 2D Toolkit Collection.
        /// </summary>
        public static void AssignSpritesFromTk2dCollection(Layout layout, tk2dSpriteCollectionData spriteCollection)
        {
            layout.ResetSpriteLayers();

            // did we select anything?
            if (spriteCollection != null)
            {
                List <tk2dSpriteDefinition> spriteDefinitions = new List <tk2dSpriteDefinition>(spriteCollection.spriteDefinitions);
                // go through our layers
                foreach (Layer layer in layout.document.allLayers)
                {
                    UnitySpriteLayer spriteLayer = layout.FindSpriteLayer(layer);
                    spriteLayer.ResetSprites();

                    // go through the guesses for the current layer
                    foreach (string guess in layer.GetGuessesForSpriteName())
                    {
                        // find the sprite definition with this name...
                        // HEADS UP:  Use the sprite definitions list, because the GetSpriteDefinition() call
                        // uses a cache that's stale when you click Commit from the editor.
                        tk2dSpriteDefinition spriteDef = spriteDefinitions.Find(each => each.name == guess);

                        // did we get it? (null && empty names... gah, this hung me up for a few hours)
                        if (spriteDef != null && spriteDef.name != "")
                        {
                            // hook it up
                            spriteLayer.tk2dSpriteName   = guess;
                            spriteLayer.spriteLayerStyle = SpriteLayerStyle.Tk2dSprite;
                            spriteLayer.bounds           = spriteDef.GetUntrimmedBounds();
                            spriteLayer.name             = guess;
                            break;
                        }
                    }
                }
            }

            layout.ResetHierarchyLabels();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Create a node for this layer.
        /// </summary>
        /// <param name="layer">The layer.</param>
        private void MakeAssembleFromLayerNode(Layer layer)
        {
            const int pixelsPerIndent = 15;

            // sanity
            if (layer == null)
            {
                return;
            }

            // grab the sprite layer.
            UnitySpriteLayer spriteLayer = layout.FindSpriteLayer(layer);

            if (spriteLayer == null)
            {
                return;
            }

            // don't even bother if we aren't a sprite and don't have kids
            bool deadLeaf = !spriteLayer.hasSprite && !spriteLayer.hasChildren;

            GUILayout.BeginVertical();

            GUIStyle horizontalStyle = new GUIStyle();

            horizontalStyle.padding = new RectOffset(20, 0, 0, 0);
            GUILayout.BeginHorizontal(horizontalStyle);

            GUIStyle iconStyle = new GUIStyle();

            iconStyle.padding      = new RectOffset(0, 0, 2, 2);
            iconStyle.margin       = new RectOffset(0, 0, 0, 0);
            iconStyle.border       = new RectOffset(0, 0, 0, 0);
            iconStyle.stretchWidth = false;


            // indent column
            GUILayout.Space(System.Math.Max(0, layer.indentLevel) * pixelsPerIndent);


            // type column
            GUIContent typeContent = new GUIContent();

            if (spriteLayer.hasSprite)
            {
                if (layer.isVisible)
                {
                    typeContent.image   = layout.editorGraphics.spriteLayerSprite;
                    typeContent.tooltip = "This is a sprite!";
                }
                else
                {
                    typeContent.image   = layout.editorGraphics.spriteLayerInvisible;
                    typeContent.tooltip = "A sprite with a disabled renderer.";
                }
            }
            else
            {
                if (deadLeaf)
                {
                    typeContent.image   = layout.editorGraphics.spriteLayerDeadLeaf;
                    typeContent.tooltip = "This dead-end layer will not be created.";
                }
                else
                {
                    typeContent.image   = layout.editorGraphics.spriteLayerFolder;
                    typeContent.tooltip = "This is a group.";
                }
            }
            GUILayout.Label(typeContent, iconStyle);

            // name column
            GUIContent nameContent = new GUIContent();

            nameContent.text = layout.GetBestName(layer);
            GUIStyle nameStyle = new GUIStyle(EditorStyles.label);

            nameStyle.stretchWidth = true;
            GUILayout.Label(nameContent, nameStyle);

            // assemble column
            if (!deadLeaf)
            {
                GUIContent assembleButtonContent = new GUIContent();
                assembleButtonContent.image = layout.editorGraphics.usain;
                if (spriteLayer.hasChildren)
                {
                    assembleButtonContent.tooltip = "Assemble this layer and children.";
                }
                else
                {
                    assembleButtonContent.tooltip = "Assemble this single sprite.";
                }


                GUIStyle assembleButtonStyle = new GUIStyle(EditorStyles.miniButton);
                assembleButtonStyle.stretchWidth = false;
                if (GUILayout.Button(assembleButtonContent, assembleButtonStyle))
                {
                    layout.startingLayer = layer;
#if PS2D_TK2D
                    SpriteCreator.CreateSprites(layout, layer, _spriteCollectionData);
#else
                    SpriteCreator.CreateSprites(layout, layer);
#endif
                }
            }

            GUILayout.EndVertical();
            GUILayout.EndHorizontal();
        }
Exemplo n.º 8
0
        /// <summary>
        /// Create a GameObject for this sprite layer, attached to this parent.
        /// </summary>
        /// <param name="parentTransform">The parent to attach to</param>
        /// <param name="spriteLayer">The sprite layer.</param>
        public GameObject CreateGameObjectForSpriteLayer(UnitySpriteLayer spriteLayer, Transform parentTransform)
        {
            // spawn up the game object
            GameObject goSprite;

            if (layout.replicator != null)
            {
                goSprite = GameObject.Instantiate(layout.replicator, Vector3.zero, Quaternion.identity) as GameObject;
            }
            else
            {
                goSprite = new GameObject();
            }
            goSprite.name = spriteLayer.name;

            // attach the appropriate sprite
            // do we have a sprite to set?
            if (spriteLayer.hasSprite)
            {
#if PS2D_TK2D
                if (spriteLayer.sprite != null)
                {
                    spriteLayer.AttachUnitySprite(goSprite);
                }
                else if (spriteLayer.tk2dSpriteName != null)
                {
                    spriteLayer.AttachTk2dSprite(goSprite, spriteCollectionData);
                }
#else
                spriteLayer.AttachUnitySprite(goSprite);
#endif
            }


            // where should this sprite be?
            PixelBounds bounds = layout.trimToSprites ? layout.trimmedSpriteBounds : layout.document.bounds;

            Vector3 position = spriteLayer.GetPixelPosition(layout.coordinatesScale, bounds);

            float x = position.x;
            float y = position.y;
            float z = position.z;

            // layout the sprite based on the anchor
            Vector2 offset = new Vector2(bounds.width, bounds.height);

            switch (layout.anchor)
            {
            case TextAnchor.LowerCenter:
                offset.x = offset.x * 0.5f;
                offset.y = offset.y * 0f;
                break;

            case TextAnchor.LowerLeft:
                offset.x = offset.x * 0f;
                offset.y = offset.y * 0f;
                break;

            case TextAnchor.LowerRight:
                offset.x = offset.x * 1f;
                offset.y = offset.y * 0f;
                break;

            case TextAnchor.MiddleCenter:
                offset.x = offset.x * 0.5f;
                offset.y = offset.y * 0.5f;
                break;

            case TextAnchor.MiddleLeft:
                offset.x = offset.x * 0f;
                offset.y = offset.y * 0.5f;
                break;

            case TextAnchor.MiddleRight:
                offset.x = offset.x * 1f;
                offset.y = offset.y * 0.5f;
                break;

            case TextAnchor.UpperCenter:
                offset.x = offset.x * 0.5f;
                offset.y = offset.y * 1f;
                break;

            case TextAnchor.UpperLeft:
                offset.x = offset.x * 0f;
                offset.y = offset.y * 1f;
                break;

            case TextAnchor.UpperRight:
                offset.x = offset.x * 1f;
                offset.y = offset.y * 1f;
                break;

            default:
                break;
            }

            // let's move it!
            x -= offset.x * layout.coordinatesScale;
            y -= offset.y * layout.coordinatesScale;

            // the pixels to units ratio
            x *= 1f / layout.pixelsToUnits;
            y *= 1f / layout.pixelsToUnits;

            // should we create an extra parent for the sprite?
            if (layout.addExtraParentToSprite)
            {
                GameObject goSpriteParent = new GameObject(goSprite.name);
                goSpriteParent.transform.position = new Vector3(x, y, z);
                goSpriteParent.transform.parent   = parentTransform;
                goSprite.transform.parent         = goSpriteParent.transform;
                goSprite.transform.localPosition  = Vector3.zero;
            }
            else
            {
                goSprite.transform.position = new Vector3(x, y, z);
                goSprite.transform.parent   = parentTransform;
            }

            return(goSprite);
        }