public static UINode CreateRectTransform(Layer layer) { GameObject go = new GameObject(layer.Name); RectTransform goRectTransform = go.AddComponent <RectTransform>(); UINode node = new UINode(); node.rect = layer.Rect; node.Go = go; SetAnchor(goRectTransform, layer.Name); PsdUtils.SetNodeName(node, go.name); return(node); }
public static UINode CreateImage(Layer layer, bool isTexture = false) { Layer imgLayer = layer; foreach (var child in layer.Children) { if (!child.IsTextLayer && !PsdUtils.IsGroupLayer(child)) { imgLayer = child; break; } } layer.Children.Remove(imgLayer); float width = imgLayer.Rect.width; float height = imgLayer.Rect.height; GameObject go = new GameObject(layer.Name); RectTransform goRectTransform = go.AddComponent <RectTransform>(); goRectTransform.sizeDelta = new Vector2(width, height); if (!isTexture) { Image img = go.AddComponent <Image>(); PsdImporter.CreateSprite(imgLayer, img); if (imgLayer.Is9Slice) { img.type = Image.Type.Sliced; } } else { RawImage img = go.AddComponent <RawImage>(); img.texture = PsdImporter.CreateTexture2D(imgLayer); } SetAnchor(goRectTransform, layer.Name); UINode node = new UINode(); node.rect = imgLayer.Rect; node.Go = go; PsdUtils.SetNodeName(node, go.name); return(node); }
public static UINode CreateUIText(Layer layer) { Color color = layer.FillColor; GameObject gameObject = new GameObject(layer.Name); Font font = PsdImporter.GetFontInfo(); Text textUI = gameObject.GetComponent <Text>(); if (textUI == null) { textUI = gameObject.AddComponent <Text>(); } RectTransform rectTransform = gameObject.GetComponent <RectTransform>(); if (rectTransform == null) { rectTransform = gameObject.AddComponent <RectTransform>(); } textUI.text = layer.Text; textUI.font = font; textUI.horizontalOverflow = HorizontalWrapMode.Overflow; textUI.verticalOverflow = VerticalWrapMode.Overflow; textUI.raycastTarget = false; //描边信息 if (layer.TextOutlineColor.a != 0f) { Outline outline = textUI.GetComponent <Outline>(); if (outline == null) { outline = textUI.gameObject.AddComponent <Outline>(); } outline.effectColor = layer.TextOutlineColor; outline.effectDistance = new Vector2(layer.OutLineDis, layer.OutLineDis); } float fontSize = layer.FontSize; float ceiling = Mathf.Ceil(fontSize); textUI.fontSize = (int)fontSize; textUI.color = color; textUI.alignment = TextAnchor.MiddleCenter; switch (layer.Justification) { case TextJustification.Left: textUI.alignment = TextAnchor.MiddleLeft; break; case TextJustification.Right: textUI.alignment = TextAnchor.MiddleRight; break; case TextJustification.Center: textUI.alignment = TextAnchor.MiddleCenter; break; } SetAnchor(rectTransform, layer.Name); UINode node = new UINode(); node.rect = layer.Rect; node.Go = gameObject; PsdUtils.SetNodeName(node, gameObject.name); return(node); }
public static UINode CreateProgress(Layer layer) { Direction direction = Direction.LeftToRight; if (layer.Name.ContainsIgnoreCase(RIGHT2LEFT)) { direction = Direction.RightToLeft; } else if (layer.Name.ContainsIgnoreCase(TOP2BUTTOM)) { direction = Direction.TopToButtom; } else if (layer.Name.ContainsIgnoreCase(BUTTOM2TOP)) { direction = Direction.ButtomToTop; } Layer fgLayer = null; Layer bgLayer = null; foreach (var child in layer.Children) { if (!child.IsTextLayer && !PsdUtils.IsGroupLayer(child)) { if (child.Name.ContainsIgnoreCase("@fg")) { fgLayer = child; } if (child.Name.ContainsIgnoreCase("@bg")) { bgLayer = child; } } } if (fgLayer == null) { Debug.LogError("progress can't find @fg"); return(null); } //GameObject go GameObject progressGo = new GameObject(layer.Name); float width = fgLayer.Rect.width; float height = fgLayer.Rect.height; RectTransform rectTransform = progressGo.AddComponent <RectTransform>(); rectTransform.sizeDelta = new Vector2(width, height); GameObject fgGo = new GameObject("Foreground"); RectTransform fgRectTransform = fgGo.AddComponent <RectTransform>(); Image fgImage = fgGo.AddComponent <Image>(); fgRectTransform.sizeDelta = new Vector2(width, height); PsdImporter.CreateSprite(fgLayer, fgImage); GameObject maskGo = new GameObject("Mask"); RectTransform maskRectTransform = maskGo.AddComponent <RectTransform>(); Image maskImg = maskGo.AddComponent <Image>(); maskGo.AddComponent <UnityEngine.UI.Mask>(); maskRectTransform.sizeDelta = new Vector2(width, height); //maskImg.sprite = Resources.Load<Sprite>("unity_builtin_extra/UIMask"); maskImg.color = new Color(1, 1, 1, 0.01f); GameObject bgGo; RectTransform bgRectTransform = null; if (bgLayer != null) { float bgWidth = bgLayer.Rect.width; float bgHeight = bgLayer.Rect.height; bgGo = new GameObject("Background"); bgRectTransform = bgGo.AddComponent <RectTransform>(); Image bgImage = bgGo.AddComponent <Image>(); bgRectTransform.sizeDelta = new Vector2(bgWidth, bgHeight); PsdImporter.CreateSprite(bgLayer, bgImage); bgRectTransform.SetParent(rectTransform); bgRectTransform.localPosition = Vector3.zero; layer.Children.Remove(bgLayer); } Vector2 pivotOrAnchor = Vector2.zero; switch (direction) { case Direction.LeftToRight: pivotOrAnchor = new Vector2(0, 0.5f); break; case Direction.RightToLeft: pivotOrAnchor = new Vector2(1, 0.5f); break; case Direction.TopToButtom: pivotOrAnchor = new Vector2(0.5f, 1); break; case Direction.ButtomToTop: pivotOrAnchor = new Vector2(0.5f, 0); break; } maskRectTransform.SetParent(rectTransform); maskRectTransform.localPosition = new Vector3((pivotOrAnchor.x - 0.5f) * fgLayer.Rect.width, (pivotOrAnchor.y - 0.5f) * fgLayer.Rect.height, 0); fgRectTransform.SetParent(maskRectTransform); fgRectTransform.localPosition = Vector3.zero; maskRectTransform.pivot = pivotOrAnchor; fgRectTransform.pivot = pivotOrAnchor; fgRectTransform.anchorMax = pivotOrAnchor; fgRectTransform.anchorMin = pivotOrAnchor; layer.Children.Remove(fgLayer); SetAnchor(rectTransform, layer.Name); UINode node = new UINode(); node.rect = fgLayer.Rect; node.Go = progressGo; PsdUtils.SetNodeName(node, progressGo.name); return(node); }
public static UINode CreateScrollRect(Layer layer) { bool horizontal = false; bool vertical = true; if (layer.Name.Contains(RIGHT2LEFT) || layer.Name.Contains(LEFT2RIGHT)) { horizontal = true; } if (layer.Name.Contains(TOP2BUTTOM) || layer.Name.Contains(BUTTOM2TOP)) { vertical = true; } Layer sizeLayer = null; foreach (var child in layer.Children) { if (!child.IsTextLayer && !PsdUtils.IsGroupLayer(child)) { if (child.Name.ContainsIgnoreCase("@size")) { sizeLayer = child; } } } if (sizeLayer == null) { Debug.LogError("Scroll Rect can't find @size"); return(null); } GameObject scrollRectGo = new GameObject(layer.Name); RectTransform rectTransform = scrollRectGo.AddComponent <RectTransform>(); ScrollRect scrollRect = scrollRectGo.AddComponent <ScrollRect>(); float width = sizeLayer.Rect.width; float height = sizeLayer.Rect.height; rectTransform.sizeDelta = new Vector2(width, height); GameObject viewPortGo = new GameObject("Viewport"); RectTransform viewPortTransform = viewPortGo.AddComponent <RectTransform>(); viewPortGo.AddComponent <UnityEngine.UI.Mask>(); Image maskImage = viewPortGo.AddComponent <Image>(); maskImage.color = new Color(1, 1, 1, 0.01f); viewPortTransform.sizeDelta = new Vector2(width, height); GameObject contentGo = new GameObject("Content"); RectTransform contentTransform = contentGo.AddComponent <RectTransform>(); contentTransform.sizeDelta = new Vector2(width, height); scrollRect.viewport = viewPortTransform; scrollRect.content = contentTransform; scrollRect.vertical = vertical; scrollRect.horizontal = horizontal; viewPortTransform.anchorMax = new Vector2(1, 1); viewPortTransform.anchorMin = new Vector2(0, 0); viewPortTransform.pivot = new Vector2(0, 1); contentTransform.anchorMax = new Vector2(0, 1); contentTransform.anchorMin = new Vector2(0, 1); contentTransform.pivot = new Vector2(0, 1); UINode scrollNode = new UINode(); scrollNode.rect = sizeLayer.Rect; scrollNode.Go = scrollRectGo; PsdUtils.SetNodeName(scrollNode, scrollRectGo.name); UINode viewportNode = new UINode(); viewportNode.rect = sizeLayer.Rect; viewportNode.Go = viewPortGo; viewportNode.pivot = viewPortTransform.pivot; UINode contentNode = new UINode(); contentNode.rect = sizeLayer.Rect; contentNode.Go = contentGo; contentNode.pivot = contentTransform.pivot; scrollNode.children.Add(viewportNode); viewportNode.children.Add(contentNode); layer.Children.Remove(sizeLayer); SetAnchor(rectTransform, layer.Name); PsdImporter.ExportTree(layer.Children, contentNode); layer.Children.Clear(); return(scrollNode); }