Ejemplo n.º 1
0
        public void DrawLayer(Layer layer, GameObject parent)
        {
            UnityEngine.UI.InputField temp       = AssetDatabase.LoadAssetAtPath(PSDImporterConst.ASSET_PATH_INPUTFIELD, typeof(UnityEngine.UI.InputField)) as UnityEngine.UI.InputField;
            UnityEngine.UI.InputField inputfield = GameObject.Instantiate(temp) as UnityEngine.UI.InputField;
            inputfield.transform.SetParent(parent.transform, false);//.parent = parent.transform;
            inputfield.name = layer.name;
            inputfield.placeholder?.gameObject.SetActive(false);
            //if (layer.image != null)
            if (layer.layers != null)
            {
                for (int imageIndex = 0; imageIndex < layer.layers.Length; imageIndex++)
                {
                    PSImage image = layer.layers[imageIndex].image;
                    //PSImage image = layer.image;

                    if (image.imageType == ImageType.Label)
                    {
                        if (image.name.ToLower().Contains("text"))
                        {
                            UnityEngine.UI.Text text = (UnityEngine.UI.Text)inputfield.textComponent;//inputfield.transform.Find("Text").GetComponent<UnityEngine.UI.Text>();
                            Color color;
                            if (UnityEngine.ColorUtility.TryParseHtmlString(("#" + image.arguments[0]), out color))
                            {
                                text.color = color;
                            }
                            int   size;
                            float sizeFloat;
                            if (float.TryParse(image.arguments[2], out sizeFloat))
                            {
                                size          = Mathf.RoundToInt(sizeFloat);
                                text.fontSize = size;
                            }
                            text.text = image.arguments[3];

                            //设置字体,注意unity中的字体名需要和导出的xml中的一致
                            string fontFolder;

                            if (image.arguments[1].ToLower().Contains("static"))
                            {
                                fontFolder = PSDImporterConst.FONT_STATIC_FOLDER;
                            }
                            else
                            {
                                fontFolder = fontFolder = PSDImporterConst.FONT_FOLDER;
                            }
                            string fontFullName = fontFolder + image.arguments[1] + PSDImporterConst.FONT_SUFIX;
                            Debug.Log("font name ; " + fontFullName);
                            var font = AssetDatabase.LoadAssetAtPath(fontFullName, typeof(Font)) as Font;
                            if (font == null)
                            {
                                Debug.LogWarning("Load font failed : " + fontFullName);
                            }
                            else
                            {
                                text.font = font;
                            }
                            //ps的size在unity里面太小,文本会显示不出来,暂时选择溢出
                            text.verticalOverflow   = VerticalWrapMode.Overflow;
                            text.horizontalOverflow = HorizontalWrapMode.Overflow;
                            //设置对齐
                            if (image.arguments.Length >= 5)
                            {
                                text.alignment = ParseAlignmentPS2UGUI(image.arguments[4]);
                            }
                            else
                            {
                                text.alignment = TextAnchor.MiddleLeft;
                            }
                            TextImport.SetOutline(image, text);
                        }
                        else if (image.name.ToLower().Contains("holder"))
                        {
                            /*
                             * UnityEngine.UI.Text text = (UnityEngine.UI.Text)inputfield.placeholder;//.transform.Find("Placeholder").GetComponent<UnityEngine.UI.Text>();
                             * Color color;
                             * if (UnityEngine.ColorUtility.TryParseHtmlString(("#" + image.arguments[0]), out color))
                             * {
                             *  text.color = color;
                             * }
                             *
                             * int size;
                             * float sizeFloat;
                             * if (float.TryParse(image.arguments[2], out sizeFloat))
                             * {
                             *  size = Mathf.RoundToInt(sizeFloat);
                             *  text.fontSize = size;
                             * }
                             *
                             * text.text = image.arguments[3];
                             *
                             * //设置字体,注意unity中的字体名需要和导出的xml中的一致
                             * string fontFolder;
                             *
                             * if (image.arguments[1].ToLower().Contains("static"))
                             * {
                             *  fontFolder = PSDImporterConst.FONT_STATIC_FOLDER;
                             * }
                             * else
                             * {
                             *  fontFolder = fontFolder = PSDImporterConst.FONT_FOLDER;
                             * }
                             * string fontFullName = fontFolder + image.arguments[1] + PSDImporterConst.FONT_SUFIX;
                             * Debug.Log("font name ; " + fontFullName);
                             * var font = AssetDatabase.LoadAssetAtPath(fontFullName, typeof(Font)) as Font;
                             * if (font == null)
                             * {
                             *  Debug.LogWarning("Load font failed : " + fontFullName);
                             * }
                             * else
                             * {
                             *  text.font = font;
                             * }
                             * //ps的size在unity里面太小,文本会显示不出来,暂时选择溢出
                             * text.verticalOverflow = VerticalWrapMode.Overflow;
                             * text.horizontalOverflow = HorizontalWrapMode.Overflow;
                             * //设置对齐
                             * if (image.arguments.Length >= 5)
                             *  text.alignment = ParseAlignmentPS2UGUI(image.arguments[4]);
                             * else
                             * {
                             *  text.alignment = TextAnchor.MiddleLeft;
                             * }*/
                        }
                    }
                    else
                    {
                        if (image.name.ToLower().Contains("background"))
                        {
                            if (image.imageSource == ImageSource.Common || image.imageSource == ImageSource.Custom)
                            {
                                string assetPath = PSDImportUtility.baseDirectory + image.name + PSDImporterConst.PNG_SUFFIX;
                                Sprite sprite    = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite;
                                inputfield.image.sprite = sprite;

                                RectTransform rectTransform = inputfield.GetComponent <RectTransform>();
                                rectTransform.sizeDelta        = new Vector2(image.size.width, image.size.height);
                                rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y);
                            }
                            else if (image.imageSource == ImageSource.Global)
                            {
                                SpriteImport.SetGlobalImage(inputfield.image, image);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void DrawLayer(Layer layer, GameObject parent)
        {
            UnityEngine.UI.Button button = PSDImportUtility.LoadAndInstant <UnityEngine.UI.Button>(PSDImporterConst.ASSET_PATH_BUTTON, layer.name, parent);

            if (layer.layers != null)
            {
                for (int imageIndex = 0; imageIndex < layer.layers.Length; imageIndex++)
                {
                    PSImage image     = layer.layers[imageIndex].image;
                    string  lowerName = image.name.ToLower();
                    if (image.imageType != ImageType.Label && image.imageType != ImageType.Texture)
                    {
                        if (image.imageSource == ImageSource.Custom || image.imageSource == ImageSource.Common)
                        {
                            string assetPath = PSDImportUtility.baseDirectory + image.name + PSDImporterConst.PNG_SUFFIX;
                            Sprite sprite    = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite;

                            if (image.name.ToLower().Contains("normal"))
                            {
                                button.image.sprite = sprite;
                                RectTransform rectTransform = button.GetComponent <RectTransform>();
                                rectTransform.sizeDelta        = new Vector2(image.size.width, image.size.height);
                                rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y);

                                adjustButtonBG(image.imageType, button);
                            }
                            else if (image.name.ToLower().Contains("pressed"))
                            {
                                button.transition = UnityEngine.UI.Selectable.Transition.SpriteSwap;
                                UnityEngine.UI.SpriteState state = button.spriteState;
                                state.pressedSprite = sprite;
                                button.spriteState  = state;
                            }
                            else if (image.name.ToLower().Contains("disabled"))
                            {
                                button.transition = UnityEngine.UI.Selectable.Transition.SpriteSwap;
                                UnityEngine.UI.SpriteState state = button.spriteState;
                                state.disabledSprite = sprite;
                                button.spriteState   = state;
                            }
                            else if (image.name.ToLower().Contains("highlighted"))
                            {
                                button.transition = UnityEngine.UI.Selectable.Transition.SpriteSwap;
                                UnityEngine.UI.SpriteState state = button.spriteState;
                                state.highlightedSprite = sprite;
                                button.spriteState      = state;
                            }
                        }
                        else if (image.imageSource == ImageSource.Global)
                        {
                            SpriteImport.SetGlobalImage(button.image, image);
                        }
                    }
                    else
                    {
                        //ctrl.DrawImage(image, button.gameObject);
                        ctrl.DrawLayer(layer.layers[imageIndex], button.gameObject);
                    }
                }
            }
        }