예제 #1
0
        private void ImportLayers()
        {
            PSDExporter.Export(settings, fileInfo);

#if NGUI
            //打开NGUI图集
            UIAtlasFolderMaker.Instance.OnBuild();
#endif
        }
예제 #2
0
        public void AddComponents(int layerIndex, GameObject imageObject, Sprite sprite, TextureImporterSettings settings)
        {
#if NGUI
            var    uiImg   = imageObject.AddComponent <UISprite>();
            string sprPath = PSDExporter.GetLayerFilename(this.layer);
            if (sprPath == null)
            {
                Debug.LogWarning("Cant find sprite path . layer name :" + this.layer.Name);
            }
            else
            {
                string spriteName = Path.GetFileNameWithoutExtension(sprPath);
                NAtlasHelper.UIAtlasData atlas = NAtlasHelper.FindSprite(spriteName);
                if (atlas != null)
                {
                    uiImg.atlas = atlas.mainAtlas;
                    UISpriteData spriteData = uiImg.atlas.GetSprite(spriteName);
                    if (spriteData.borderLeft != 0 || spriteData.borderRight != 0 ||
                        spriteData.borderBottom != 0 || spriteData.borderTop != 0)
                    {
                        uiImg.type = UIBasicSprite.Type.Sliced;
                    }
                }
                uiImg.spriteName = spriteName;
            }

            uiImg.depth = layerIndex;
            uiImg.SetDimensions((int)layer.Rect.width, (int)layer.Rect.height);
            uiImg.transform.SetAsFirstSibling();


//            Vector2 sprPivot = PsdBuilder.GetPivot(settings);
//            uiImg.pivot = NGUIMath.GetPivot(sprPivot);
#else
            var uiImg = imageObject.AddComponent <Image>();

            uiImg.sprite = sprite;
            uiImg.SetNativeSize();
            uiImg.rectTransform.SetAsFirstSibling();

            Vector2 sprPivot = PsdBuilder.GetPivot(settings);
            uiImg.rectTransform.pivot = sprPivot;
#endif
        }
예제 #3
0
        private static void ExecuteAutoImport()
        {
            // This function only fires once, unhook from update function immediately
            EditorApplication.update -= ExecuteAutoImport;

            for (int i = 0; i < reimports.Count; i++)
            {
                var target         = reimports[i];
                var exportSettings = new PsdExportSettings(target);
                if (exportSettings.AutoReImport)
                {
                    PsdFileInfo psdInfo = new PsdFileInfo(exportSettings.Psd);
                    exportSettings.LoadLayers(psdInfo);
                    PSDExporter.Export(exportSettings, psdInfo);
                }
            }

            reimports.Clear();
        }
예제 #4
0
        public void AddComponents(int layerIndex, GameObject imageObject, Sprite sprite, TextureImporterSettings settings)
        {
            string sprPath = PSDExporter.GetLayerFilename(this.layer);

            if (sprPath == null)
            {
                Debug.LogWarning("Cant find sprite path . layer name :" + this.layer.Name);
                return;
            }
            string spriteName = Path.GetFileNameWithoutExtension(sprPath);

#if NGUI
            var uiImg = imageObject.AddComponent <UISprite>();
            NAtlasHelper.UIAtlasData atlas = NAtlasHelper.FindSprite(spriteName);
            if (atlas != null)
            {
                uiImg.atlas = atlas.mainAtlas;
                UISpriteData spriteData = uiImg.atlas.GetSprite(spriteName);
                if (spriteData.borderLeft != 0 || spriteData.borderRight != 0 ||
                    spriteData.borderBottom != 0 || spriteData.borderTop != 0)
                {
                    uiImg.type = UIBasicSprite.Type.Sliced;
                }
            }
            uiImg.spriteName = spriteName;

            uiImg.depth = layerIndex;
            uiImg.SetDimensions((int)layer.Rect.width, (int)layer.Rect.height);
            uiImg.transform.SetAsFirstSibling();


//            Vector2 sprPivot = PsdBuilder.GetPivot(settings);
//            uiImg.pivot = NGUIMath.GetPivot(sprPivot);
#elif ART_PROJ
            var      uiImg       = imageObject.AddComponent <Image>();
            string[] all_sprites = Directory.GetFiles("Assets/Textures", "*.png", SearchOption.AllDirectories);
            foreach (var file in all_sprites)
            {
                string tmpName = Path.GetFileNameWithoutExtension(file);
                var    tmp     = AssetDatabase.LoadAssetAtPath <Sprite>(file);
                if (tmp.name == spriteName)
                {
                    sprite = tmp as Sprite;
                    break;
                }
            }
            uiImg.sprite = sprite;
            if (sprite != null && sprite.border != Vector4.zero)
            {
                uiImg.type = Image.Type.Sliced;
            }
            //uiImg.SetNativeSize();
            uiImg.rectTransform.sizeDelta = new Vector2((int)layer.Rect.width, (int)layer.Rect.height);
            uiImg.rectTransform.SetAsFirstSibling();

            //Vector2 sprPivot = PsdBuilder.GetPivot(settings);
            //uiImg.rectTransform.pivot = sprPivot;
#else
            var      uiImg       = imageObject.AddComponent <Image>();
            string[] all_sprites = Directory.GetFiles("Assets/Res/Atlas", "*.png", SearchOption.AllDirectories);
            foreach (var file in all_sprites)
            {
                string tmpName = Path.GetFileNameWithoutExtension(file);
                var    sprites = AssetDatabase.LoadAllAssetsAtPath(file);

                bool created = false;
                foreach (var tmp in sprites)
                {
                    if (tmp.name == spriteName)
                    {
                        sprite  = tmp as Sprite;
                        created = true;
                        break;
                    }
                }
                if (created)
                {
                    break;
                }
            }
            uiImg.sprite = sprite;
            if (sprite != null && sprite.border != Vector4.zero)
            {
                uiImg.type = Image.Type.Sliced;
            }
            //uiImg.SetNativeSize();
            uiImg.rectTransform.sizeDelta = new Vector2((int)layer.Rect.width, (int)layer.Rect.height);
            uiImg.rectTransform.SetAsFirstSibling();

            //Vector2 sprPivot = PsdBuilder.GetPivot(settings);
            //uiImg.rectTransform.pivot = sprPivot;
#endif
        }
예제 #5
0
        public static void BuildPsd(GameObject root, PSDLayerGroupInfo group,
                                    PsdExportSettings settings, PsdFileInfo fileInfo,
                                    SpriteAlignment align, IPsdConstructor initConstructor)
        {
            // Run the export on non exported layers
//			PSDExporter.Export(settings, fileInfo, false);

            // Find all the layers being exported
            var exportLayers = PSDExporter.GetExportLayers(settings, fileInfo);

            // Stores the root object for each encountered group
            Dictionary <PSDLayerGroupInfo, GameObject> groupHeaders = new Dictionary <PSDLayerGroupInfo, GameObject>();

            // Store the last parent, for traversal
            GameObject lastParent = root;
            GameObject rootBase   = root;

            int groupVisibleMask = 1;
            int groupDepth       = 0;

            // Loop through all the layers of the PSD file
            // backwards so they appear in the expected order
            // Going through all the layers, and not just the exported layers
            // so that the groups can be setup
            for (int i = group.end; i >= group.start; i--)
            {
                IPsdConstructor constructor = initConstructor;

                // Skip if layer is hidden
                if (fileInfo.LayerVisibility[i] == false)
                {
                    continue;
                }

                var  groupInfo = fileInfo.GetGroupByLayerIndex(i);
                bool inGroup   = groupInfo != null;

                // Skip if layer belongs to a hidden group
                if (inGroup && groupInfo.visible == false)
                {
                    continue;
                }


                // When inside a group...
                if (inGroup)
                {
                    // Inverted because starting backwards
                    bool startGroup = groupInfo.end == i;
                    bool closeGroup = groupInfo.start == i;

                    // Go up or down group depths
                    if (startGroup)
                    {
                        groupDepth++;
                        groupVisibleMask |= ((groupInfo.visible ? 1 : 0) << groupDepth);
                    }
                    if (closeGroup)
                    {
                        // Reset group visible flag when closing group
                        groupVisibleMask &= ~(1 << groupDepth);
                        groupDepth--;
                    }

                    // First, check if parents of this group is visible in the first place
                    bool parentVisible = true;
                    for (int parentMask = groupDepth - 1; parentMask > 0; parentMask--)
                    {
                        bool isVisible = (groupVisibleMask & (1 << parentMask)) > 0;
                        parentVisible &= isVisible;
                    }
                    // Parents not visible, continue to next layer
                    if (!parentVisible)
                    {
                        continue;
                    }

                    // Finally, check if layer being processed is start/end of group
                    if (startGroup || closeGroup)
                    {
                        // If start or end of the group, call HandleGroupObject
                        // which creates the group layer object and assignment of lastParent
                        HandleGroupObject(groupInfo, groupHeaders,
                                          startGroup, constructor, ref lastParent);

                        // A bunch of book keeping needs to be done at the start of a group
                        if (startGroup)
                        {
                            // If this is the start of the group being constructed
                            // store as the rootBase
                            if (i == group.end)
                            {
                                rootBase = lastParent;
                            }
                        }

                        // Start or end group doesn't have visible sprite object, skip to next layer
                        continue;
                    }
                }                 // End processing of group start/end

                // If got to here, processing a visual layer

                // Skip if the export layers list doesn't contain this index
                if (exportLayers.Contains(i) == false)
                {
                    continue;
                }

                // If got here and root base hasn't been set, that's a problem
                if (rootBase == null)
                {
                    throw new Exception("Trying to create image layer before root base has been set");
                }

                // Get layer info
                Layer layer = settings.Psd.Layers[i];
                if (layer.IsText)
                {
                    constructor = new TextConstructor();
                }
                constructor.layer = layer;

                // Create the game object for the sprite
                string     layerName    = Utility.PinYinHelper.Convert(layer.Name);
                GameObject spriteObject = constructor.CreateGameObject(layerName, lastParent);
                // Reparent created object to last parent
                if (lastParent != null)
                {
                    spriteObject.transform.SetParent(lastParent.transform, false);
                }

                Vector2 spritePivot = GetPivot(SpriteAlignment.Center);
                if (layer.IsText)
                {
                    constructor.AddComponents(i, spriteObject, null, null);
                }
                else
                {
#if NGUI
                    // Add components to the sprite object for the visuals
                    constructor.AddComponents(i, spriteObject, null, null);
#else
                    // Retrieve sprite from asset database
                    string sprPath = PSDExporter.GetLayerFilename(settings, i);

                    Sprite sprite = AssetDatabase.LoadAssetAtPath <Sprite>(sprPath);

                    // Get the pivot settings for the sprite
                    TextureImporter         sprImporter = (TextureImporter)AssetImporter.GetAtPath(sprPath);
                    TextureImporterSettings sprSettings = new TextureImporterSettings();
                    sprImporter.ReadTextureSettings(sprSettings);
                    sprImporter = null;

                    // Add components to the sprite object for the visuals
                    constructor.AddComponents(i, spriteObject, sprite, sprSettings);

                    // Reposition the sprite object according to PSD position
                    spritePivot = GetPivot(sprSettings);
#endif
                }

                Vector3 layerPos = constructor.GetLayerPosition(layer.Rect, spritePivot, settings.PixelsToUnitSize);
                // reverse y axis
                layerPos.y = fileInfo.height - layerPos.y;
                layerPos  -= new Vector3(fileInfo.width, fileInfo.height) * 0.5f;

                // Scaling factor, if sprites were scaled down
                float posScale = 1f;
                switch (settings.ScaleBy)
                {
                case 1:
                    posScale = 0.5f;
                    break;

                case 2:
                    posScale = 0.25f;
                    break;
                }
                layerPos *= posScale;

                // Sprite position is based on root object position initially
                Transform spriteT = spriteObject.transform;
#if NGUI
                spriteT.localPosition = layerPos;
#else
                RectTransform rectTrans = spriteObject.GetComponent <RectTransform>();
                rectTrans.anchoredPosition3D = layerPos;
#endif
            }             // End layer loop

            parseComponent(root);
        }         // End BuildPsd()
예제 #6
0
        private Dictionary <PSDLayerGroupInfo, Rect> DisplayLayers()
        {
            Dictionary <PSDLayerGroupInfo, Rect> groupRects = new Dictionary <PSDLayerGroupInfo, Rect>();

            int groupDepth       = 0;
            int groupVisibleMask = 1;
            int groupOpenMask    = 1;

            PsdFile psd = settings.Psd;

            // Loop backwards through the layers to display them in the expected order
            for (int i = psd.Layers.Count - 1; i >= 0; i--)
            {
                Layer layer = psd.Layers[i];

                //var instanceInfo = fileInfo.GetInstancedLayer(i);
                //if (instanceInfo != null && doDebug)
                //	Debug.LogFormat("Layer {0}, index {1}, is instance of {2}", layer.Name, i, instanceInfo.instanceLayer);

                // Layer set seems to appear in the photoshop layers
                // no idea what it does but doesn't seem to be relevant
                if (layer.Name.Equals("</Layer set>"))
                {
                    continue;
                }

                // Try to get the group of this layer
                var  groupInfo = fileInfo.GetGroupByLayerIndex(i);
                bool inGroup   = groupInfo != null;

                bool startGroup = false;
                bool closeGroup = false;

                if (inGroup)
                {
                    closeGroup = groupInfo.start == i;
                    startGroup = groupInfo.end == i;
                }

                // If start of group, indent
                if (startGroup)
                {
                    groupDepth++;

                    // Save the mask info
                    groupVisibleMask |= ((groupInfo.visible ? 1 : 0) << groupDepth);
                    groupOpenMask    |= ((groupInfo.opened ? 1 : 0) << groupDepth);
                }
                // If exiting a layer group, unindent and continue to next layer
                if (closeGroup)
                {
                    // Reset mask info when closing group
                    groupVisibleMask &= ~(1 << groupDepth);
                    groupOpenMask    &= ~(1 << groupDepth);

                    groupDepth--;
                    continue;
                }

                bool parentVisible = true;

                // If layer group content...
                if (inGroup)
                {
                    bool parentOpen = true;
                    parentVisible = true;

                    for (int parentMask = groupDepth - 1; parentMask > 0; parentMask--)
                    {
                        bool isOpen    = (groupOpenMask & (1 << parentMask)) > 0;
                        bool isVisible = (groupVisibleMask & (1 << parentMask)) > 0;

                        parentOpen    &= isOpen;
                        parentVisible &= isVisible;
                    }

                    bool skipLayer    = !groupInfo.opened || !parentOpen;
                    bool disableLayer = !groupInfo.visible || !parentVisible;

                    if (startGroup)
                    {
                        skipLayer    = !parentOpen;
                        disableLayer = !parentVisible;
                    }

                    // Skip contents if group folder closed
                    if (skipLayer)
                    {
                        SetHiddenLayer(i, groupInfo.visible && parentVisible);
                        continue;
                    }

                    // If not visible, disable the row
                    if (disableLayer)
                    {
                        GUI.enabled = false;
                    }

                    // Set parentVisible for settings override in DrawLayerEntry
                    parentVisible = !disableLayer;
                }

                if (startGroup)
                {
                    var rect = DrawLayerGroupStart(groupInfo, i, groupDepth - 1);
                    groupInfo.depth = groupDepth;
                    groupRects.Add(groupInfo, rect);
                }
                else
                {
                    DrawLayerEntry(layer, i, groupDepth, parentVisible);
                }

                GUI.enabled = true;
            }             // End layer loop

            importCount = PSDExporter.GetExportCount(settings, fileInfo);
            return(groupRects);
        }