public void DrawLayer(Layer layer, GameObject parent) { RectTransform obj = PSDImportUtility.LoadAndInstant <RectTransform>(PSDImporterConst.ASSET_PATH_LAYOUTELEMENT, layer.name, parent); RectTransform rectTransform = parent.GetComponent <RectTransform>(); obj.sizeDelta = new Vector2(layer.size.width, layer.size.height); obj.anchoredPosition = new Vector2(layer.position.x, layer.position.y); var le = obj.GetComponent <LayoutElement>(); le.preferredWidth = layer.size.width; le.preferredHeight = layer.size.height; if (layer.image != null) { //for (int imageIndex = 0; imageIndex < layer.layers.Length; imageIndex++) //{ PSImage image = layer.image; ctrl.DrawImage(image, obj.gameObject); //} } ctrl.DrawLayers(layer.layers, obj.gameObject); //obj.transform.SetParent(parent.transform, false); //parent.transform; }
public void DrawLayer(Layer layer, GameObject parent) { UnityEngine.UI.Scrollbar temp = AssetDatabase.LoadAssetAtPath(PSDImporterConst.ASSET_PATH_SCROLLBAR, typeof(UnityEngine.UI.Scrollbar)) as UnityEngine.UI.Scrollbar; UnityEngine.UI.Scrollbar scrollBar = GameObject.Instantiate(temp) as UnityEngine.UI.Scrollbar; scrollBar.transform.SetParent(parent.transform, false); //parent = parent.transform; string type = layer.arguments[0].ToUpper(); switch (type) { case "R": scrollBar.direction = Scrollbar.Direction.RightToLeft; break; case "L": scrollBar.direction = Scrollbar.Direction.LeftToRight; break; case "T": scrollBar.direction = Scrollbar.Direction.TopToBottom; break; case "B": scrollBar.direction = Scrollbar.Direction.BottomToTop; break; default: break; } float pecent; if (float.TryParse(layer.arguments[1], out pecent)) { scrollBar.size = pecent; } //for (int i = 0; i < layer.images.Length; i++) //{ PSImage image = layer.image; string assetPath = PSDImportCtrl.Instance.GetFilePath(image);// PSDImportUtility.baseDirectory + image.name + PSDImporterConst.PNG_SUFFIX; Debug.Log("== CommonImagePath ====" + assetPath); Sprite sprite = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite; if (image.name.ToLower().Contains("background")) { scrollBar.GetComponent <UnityEngine.UI.Image>().sprite = sprite; RectTransform rectTransform = scrollBar.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.name.ToLower().Contains("handle")) { scrollBar.handleRect.GetComponent <UnityEngine.UI.Image>().sprite = sprite; } //} }
public void DrawLayer(Layer layer, GameObject parent) { //UnityEngine.UI.Image temp = AssetDatabase.LoadAssetAtPath(PSDImporterConst.PREFAB_PATH_IMAGE, typeof(UnityEngine.UI.Image)) as UnityEngine.UI.Image; UnityEngine.UI.Image panel = PSDImportUtility.LoadAndInstant <UnityEngine.UI.Image>(PSDImporterConst.ASSET_PATH_IMAGE, layer.name, parent);//GameObject.Instantiate(temp) as UnityEngine.UI.Image; panel.name = layer.name; ctrl.DrawLayers(layer.layers, panel.gameObject);//子节点 //for (int i = 0; i < layer.images.Length; i++) //{ PSImage image = layer.image; if (image.name.ToLower().Contains("background")) { string assetPath = PSDImportCtrl.Instance.GetFilePath(image); // PSDImportUtility.baseDirectory + image.name + PSDImporterConst.PNG_SUFFIX; Sprite sprite = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite; panel.sprite = sprite; RectTransform rectTransform = panel.GetComponent <RectTransform>(); rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); //panel.transform.SetParent(parent.transform, false); //parent = parent.transform; } else { ctrl.DrawImage(image, panel.gameObject); } //} }
public void DrawImage(PSImage image, GameObject parent, GameObject ownObj = null) { if (image.imageSource == ImageSource.Common || image.imageSource == ImageSource.Private) { Image pic; if (ownObj != null) { pic = PSDImportUtility.AddMissingComponent <Image>(ownObj); } else { pic = PSDImportUtility.LoadAndInstant <Image>(PSDImporterConst.ASSET_PATH_IMAGE, image.name, parent); } RectTransform rectTransform = pic.GetComponent <RectTransform>(); rectTransform.offsetMin = new Vector2(0.5f, 0.5f); rectTransform.offsetMax = new Vector2(0.5f, 0.5f); rectTransform.anchorMin = new Vector2(0.5f, 0.5f); rectTransform.anchorMax = new Vector2(0.5f, 0.5f); var p = PSDImportCtrl.Instance.GetFilePath(image); Sprite sprite = AssetDatabase.LoadAssetAtPath(p, typeof(Sprite)) as Sprite; if (sprite == null) { Debug.Log("loading asset at path: " + p); } pic.sprite = sprite; rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); } }
public void DrawImage(PSImage image, GameObject parent, GameObject ownObj = null) { RectTransform halfRectTrans = parent.GetComponent <RectTransform>(); PSDImportUtility.SetAnchorMiddleCenter(halfRectTrans); halfRectTrans.sizeDelta = new Vector2(image.size.width, image.size.height); halfRectTrans.anchoredPosition = new Vector2(image.position.x, image.position.y); UnityEngine.UI.Image leftOrUpSprite; if (ownObj != null) { leftOrUpSprite = PSDImportUtility.AddMissingComponent <Image>(ownObj); } else { leftOrUpSprite = PSDImportUtility.LoadAndInstant <UnityEngine.UI.Image>(PSDImporterConst.ASSET_PATH_IMAGE, image.name, halfRectTrans.gameObject); } //string assetPath = ""; //if (image.imageSource == ImageSource.Common || image.imageSource == ImageSource.Custom) //{ // assetPath = PSDImportUtility.baseDirectory + image.name + PSDImporterConst.PNG_SUFFIX; //} //else //{ // assetPath = PSDImporterConst.Globle_BASE_FOLDER + image.name.Replace(".", "/") + PSDImporterConst.PNG_SUFFIX; //} //Sprite sprite = AssetDatabase.LoadAssetAtPath(assetPath, typeof(Sprite)) as Sprite; Sprite sprite = PSDImportUtility.LoadAssetAtPath <Sprite>(image) as Sprite; // if (sprite == null) // { // Debug.Log("loading asset at path: " + assetPath); // } leftOrUpSprite.sprite = sprite; RectTransform lOrURectTrans = leftOrUpSprite.GetComponent <RectTransform>(); lOrURectTrans.anchoredPosition = new Vector2(image.position.x, image.position.y); //添加镜像组件 var mirror = lOrURectTrans.gameObject.AddComponent <Mirror>(); if (image.imageType == ImageType.BottomHalfImage) { mirror.mirrorType = Mirror.MirrorType.Vertical; } else if (image.imageType == ImageType.LeftHalfImage) { mirror.mirrorType = Mirror.MirrorType.Horizontal; } else if (image.imageType == ImageType.QuarterImage) { mirror.mirrorType = Mirror.MirrorType.Quarter; } mirror.SetNativeSize(); }
public static Object LoadAssetAtPath <T>(PSImage image) { var p = PSDImportCtrl.Instance.GetFilePath(image); Object obj = AssetDatabase.LoadAssetAtPath(p, typeof(T)); if (obj == null) { Debug.LogWarning("loading asset is null, at path: " + p); } return(obj); }
public void DrawLayer(Layer layer, GameObject parent) { Button button = PSDImportUtility.LoadAndInstant <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; if (image.imageType != ImageType.Label && image.imageType != ImageType.Texture) { var p = PSDImportCtrl.Instance.GetFilePath(image); Sprite sprite = AssetDatabase.LoadAssetAtPath(p, 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 = Selectable.Transition.SpriteSwap; SpriteState state = button.spriteState; state.pressedSprite = sprite; button.spriteState = state; } else if (image.name.ToLower().Contains("disabled")) { button.transition = Selectable.Transition.SpriteSwap; SpriteState state = button.spriteState; state.disabledSprite = sprite; button.spriteState = state; } else if (image.name.ToLower().Contains("highlighted")) { button.transition = Selectable.Transition.SpriteSwap; SpriteState state = button.spriteState; state.highlightedSprite = sprite; button.spriteState = state; } } else { ctrl.DrawLayer(layer.layers[imageIndex], button.gameObject); } } } }
//根据导出文件名,获取文件路径 public string GetFilePath(PSImage image) { string fileName = image.name; for (var i = 0; i < _existFiles.Count; i++) { var uf = _existFiles[i]; switch (image.imageType) { case ImageType.Texture: for (var j = 0; j < uf.Texture.Count; j++) { if (fileName == uf.RawTexture[j]) { return(uf.System + "/texture/" + uf.Texture[j] + PSDImporterConst.PNG_SUFFIX); } } break; case ImageType.Label: break; case ImageType.Image: case ImageType.SliceImage: case ImageType.LeftHalfImage: case ImageType.BottomHalfImage: case ImageType.QuarterImage: for (var j = 0; j < uf.Atlas.Count; j++) { if (fileName == uf.RawAtlas[j]) { return(uf.System + "/atlas/" + uf.Atlas[j] + PSDImporterConst.PNG_SUFFIX); } } break; default: break; } } Debug.LogError("can not find asset:" + fileName); return(string.Empty); }
public void DrawImage(PSImage image, GameObject parent, GameObject ownObj = null) { RawImage pic; if (ownObj != null) { pic = PSDImportUtility.AddMissingComponent <RawImage>(ownObj); } else { pic = PSDImportUtility.LoadAndInstant <RawImage>(PSDImporterConst.ASSET_PATH_RAWIMAGE, image.name, parent); } Texture2D texture = PSDImportUtility.LoadAssetAtPath <Texture2D>(image) as Texture2D; pic.texture = texture as Texture; RectTransform rectTransform = pic.GetComponent <RectTransform>(); PSDImportUtility.SetAnchorMiddleCenter(rectTransform); rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); }
public void DrawLayer(Layer layer, GameObject parent) { RectTransform obj = PSDImportUtility.LoadAndInstant <RectTransform>(PSDImporterConst.ASSET_PATH_EMPTY, layer.name, parent); obj.offsetMin = Vector2.zero; obj.offsetMax = Vector2.zero; obj.anchorMin = Vector2.zero; obj.anchorMax = Vector2.one; RectTransform rectTransform = parent.GetComponent <RectTransform>(); obj.sizeDelta = rectTransform.sizeDelta; obj.anchoredPosition = rectTransform.anchoredPosition; if (layer.image != null) { PSImage image = layer.image; //ctrl.DrawImage(image, obj.gameObject); ctrl.DrawImage(image, parent, obj.gameObject); } ctrl.DrawLayers(layer.layers, obj.gameObject); }
public void DrawImage(PSImage image, GameObject parent, GameObject ownObj) { Image pic; if (ownObj != null) { pic = PSDImportUtility.AddMissingComponent <Image>(ownObj); } else { pic = PSDImportUtility.LoadAndInstant <Image>(PSDImporterConst.ASSET_PATH_IMAGE, image.name, parent); } RectTransform rectTransform = pic.GetComponent <RectTransform>(); PSDImportUtility.SetAnchorMiddleCenter(rectTransform); //var p = PSDImportCtrl.Instance.GetFilePath(image); //var imp = AssetImporter.GetAtPath(p) as TextureImporter; //if (!PSDImportCtrl.Instance.IsSetBorder(imp)) //{ // PSDImportCtrl.Instance.SetSpriteBorder(imp, image.arguments); // AssetDatabase.WriteImportSettingsIfDirty(p); // AssetDatabase.ImportAsset(p); //} //else //{ // Debug.Log("已设置九宫格:"+p); //} Sprite sprite = PSDImportUtility.LoadAssetAtPath <Sprite>(image) as Sprite; pic.sprite = sprite; pic.type = Image.Type.Sliced; //RectTransform rectTransform = pic.GetComponent<RectTransform>(); rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); }
public void DrawLayer(Layer layer, GameObject parent) { UnityEngine.UI.Toggle toggle = PSDImportUtility.LoadAndInstant <UnityEngine.UI.Toggle>(PSDImporterConst.ASSET_PATH_TOGGLE, layer.name, parent); if (layer.layers == null) { Debug.LogError("error! bad toggle layers."); return; } for (int index = 0; index < layer.layers.Length; index++) { Layer subLayer = layer.layers[index]; PSImage image = subLayer.image; if (image != null) { string lowerName = image.name.ToLower(); if (lowerName.Contains("checkmark")) { ctrl.DrawImage(image, toggle.gameObject, toggle.graphic.gameObject); } else if (lowerName.Contains("background")) { ctrl.DrawImage(image, toggle.gameObject, toggle.targetGraphic.gameObject); } else { ctrl.DrawImage(image, toggle.gameObject); } } else { ctrl.DrawLayer(subLayer, toggle.gameObject); } } }
public void DrawImage(PSImage image, GameObject parent, GameObject ownObj = null) { switch (image.imageType) { case ImageType.Image: spriteImport.DrawImage(image, parent, ownObj); break; case ImageType.Texture: textureImport.DrawImage(image, parent, ownObj); break; case ImageType.Label: textImport.DrawImage(image, parent, ownObj); break; case ImageType.SliceImage: slicedSpriteImport.DrawImage(image, parent, ownObj); break; case ImageType.LeftHalfImage: halfSpriteImport.DrawImage(image, parent, ownObj); break; case ImageType.BottomHalfImage: halfSpriteImport.DrawImage(image, parent, ownObj); break; case ImageType.QuarterImage: halfSpriteImport.DrawImage(image, parent, ownObj); break; default: break; } }
public void DrawLayer(Layer layer, GameObject parent) { Slider slider = PSDImportUtility.LoadAndInstant <Slider>(PSDImporterConst.ASSET_PATH_SLIDER, layer.name, parent); RectTransform rectTransform = slider.GetComponent <RectTransform>(); rectTransform.sizeDelta = new Vector2(layer.size.width, layer.size.height); rectTransform.anchoredPosition = new Vector2(layer.position.x, layer.position.y); PosLoader posloader = slider.gameObject.AddComponent <PosLoader>(); posloader.worldPos = rectTransform.position; string type = layer.arguments[0].ToUpper(); switch (type) { case "R": slider.direction = Slider.Direction.RightToLeft; break; case "L": slider.direction = Slider.Direction.LeftToRight; break; case "T": slider.direction = Slider.Direction.TopToBottom; break; case "B": slider.direction = Slider.Direction.BottomToTop; break; default: break; } for (int i = 0; i < layer.layers.Length; i++) { PSImage image = layer.layers[i].image; var p = PSDImportCtrl.Instance.GetFilePath(image); Sprite sprite = AssetDatabase.LoadAssetAtPath(p, typeof(Sprite)) as Sprite; if (image.name.ToLower().Contains("bg")) { var bgRect = slider.transform.Find("Background").GetComponent <RectTransform>(); var bgImage = bgRect.GetComponent <Image>(); if (image.imageType != ImageType.SliceImage) { bgImage.type = Image.Type.Simple; } bgImage.sprite = sprite; bgRect.sizeDelta = new Vector2(image.size.width, image.size.height); } else if (image.name.ToLower().Contains("fill")) { var fillImage = slider.fillRect.GetComponent <Image>(); if (image.imageType != ImageType.SliceImage) { fillImage.type = Image.Type.Simple; } fillImage.sprite = sprite; var fillArea = slider.transform.Find("Fill Area").GetComponent <RectTransform>(); fillArea.sizeDelta = new Vector2(image.size.width, image.size.height); } else if (image.name.ToLower().Contains("handle")) //默认没有handle { var handleRectTrans = slider.transform.Find("Handle Slide Area/Handle").GetComponent <RectTransform>(); var handleSprite = handleRectTrans.GetComponent <Image>(); slider.handleRect = handleRectTrans; handleSprite.sprite = sprite; handleRectTrans.gameObject.SetActive(true); } } }
public void DrawImage(PSImage image, GameObject parent, GameObject ownObj = null) { Text myText; if (ownObj != null) { myText = PSDImportUtility.AddMissingComponent <Text>(ownObj); } else { myText = PSDImportUtility.LoadAndInstant <Text>(PSDImporterConst.ASSET_PATH_TEXT, image.name, parent); } RectTransform rectTransform = myText.GetComponent <RectTransform>(); PSDImportUtility.SetAnchorMiddleCenter(rectTransform); Debug.Log("Label Color : " + image.arguments[0]); Debug.Log("fontSize : " + image.arguments[2]); Color color; if (ColorUtility.TryParseHtmlString(("#" + image.arguments[0]), out color)) { myText.color = color; } else { Debug.Log(image.arguments[0]); } float size; if (float.TryParse(image.arguments[2], out size)) { myText.fontSize = (int)size; } myText.text = image.arguments[3]; //设置字体,注意unity中的字体名需要和导出的xml中的一致 string fontFolder; if (image.arguments[1].ToLower().Contains("static")) { fontFolder = PSDImporterConst.FONT_STATIC_FOLDER; } else { 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 { myText.font = font; } //ps的size在unity里面太小,文本会显示不出来,暂时选择溢出 myText.verticalOverflow = VerticalWrapMode.Overflow; myText.horizontalOverflow = HorizontalWrapMode.Overflow; //设置对齐 if (image.arguments.Length >= 5) { myText.alignment = ParseAlignmentPS2UGUI(image.arguments[4]); } rectTransform.sizeDelta = new Vector2(image.size.width, image.size.height); rectTransform.anchoredPosition = new Vector2(image.position.x, image.position.y); myText.gameObject.name = "Text"; }
public void DrawLayer(Layer layer, GameObject parent) { InputField temp = AssetDatabase.LoadAssetAtPath(PSDImporterConst.ASSET_PATH_INPUTFIELD, typeof(InputField)) as InputField; InputField inputfield = GameObject.Instantiate(temp) as InputField; inputfield.transform.SetParent(parent.transform, false);//.parent = parent.transform; inputfield.name = layer.name; if (layer.image != null) { //for (int imageIndex = 0; imageIndex < layer.images.Length; imageIndex++) //{ PSImage image = layer.image; if (image.imageType == ImageType.Label) { if (image.name.ToLower().Contains("text")) { Text text = (Text)inputfield.textComponent; //inputfield.transform.Find("Text").GetComponent<UnityEngine.UI.Text>(); Color color; if (ColorUtility.TryParseHtmlString(("#" + image.arguments[0]), out color)) { text.color = color; } int size; if (int.TryParse(image.arguments[2], out size)) { text.fontSize = size; } } else if (image.name.ToLower().Contains("holder")) { Text text = (Text)inputfield.placeholder; //.transform.Find("Placeholder").GetComponent<UnityEngine.UI.Text>(); Color color; if (ColorUtility.TryParseHtmlString(("#" + image.arguments[0]), out color)) { text.color = color; } int size; if (int.TryParse(image.arguments[2], out size)) { text.fontSize = size; } } } else { if (image.name.ToLower().Contains("background")) { if (image.imageSource == ImageSource.Common || image.imageSource == ImageSource.Private) { string assetPath = pSDImportCtrl.GetFilePath(image); // 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); } } } //} } }