public override void OnInspectorGUI() { if (_nativeEditor != null) { // check if it is a PSD file selected string assetPath = ((TextureImporter)target).assetPath; if (assetPath.EndsWith(PsdImporter.PSD_TAIL)) { GUILayout.Label("<b>PSD Layout Tool</b>", _guiStyle, GUILayout.Height(23)); //set ui width and height; GUIContent screenSize = new GUIContent("屏幕分辨率", "UI 宽*高"); PsdImporter.ScreenResolution = EditorGUILayout.Vector2Field(screenSize, PsdImporter.ScreenResolution); GUIContent imageSizeLimit = new GUIContent("小图限制(超过该尺寸 建议拆分图集)", "小图尺寸限制(超过该尺寸 建议拆出图集)"); PsdImporter.LargeImageAlarm = EditorGUILayout.Vector2Field(imageSizeLimit, PsdImporter.LargeImageAlarm); //set textFont GUIContent fontName = new GUIContent("字体名称", "字体名称"); PsdImporter.textFont = EditorGUILayout.TextField(fontName, PsdImporter.textFont); if (GUILayout.Button("Layout in Current Scene")) { PsdImporter.LayoutInCurrentScene(assetPath); } if (GUILayout.Button("Generate Prefab")) { PsdImporter.GeneratePrefab(assetPath); } GUILayout.Space(3); GUILayout.Box(string.Empty, GUILayout.Height(1), GUILayout.MaxWidth(Screen.width - 30)); GUILayout.Space(3); GUILayout.Label("<b>Unity Texture Importer Settings</b>", _guiStyle, GUILayout.Height(23)); // draw the default Inspector for the PSD _nativeEditor.OnInspectorGUI(); } else { // It is a "normal" Texture, not a PSD _nativeEditor.OnInspectorGUI(); } } }
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 void CreateSprite(Layer layer, Image img) { Match match = PsdControl.colorImgRegex.Match(layer.Name); if (match.Success) { string numStr = match.Groups[1].Value; float alpha = float.Parse(numStr) / 100.0f; img.color = new Color(0, 0, 0, alpha); } else if (!layer.Name.StartsWith(PsdImporter.IMG_REF)) { img.sprite = PsdImporter.CreateSpriteInternal(layer); } else { layer.Name = layer.Name.Replace(PsdImporter.IMG_REF, string.Empty); string writePath; string path = PsdImporter.GetFilePath(layer, out writePath); PsdImporter.AddScaleImg(path, img); } }
public static void UpdateAllUINodeRectTransform(UINode root) { if (PsdImporter.CanvasObj == null) { Debug.LogError("No Canvas"); return; } RectTransform canvasRectTransform = PsdImporter.CanvasObj.transform as RectTransform; Vector3 canvasWorldPosition = canvasRectTransform.position; Rect rect = GetUINodeRectTransform(root); Vector3 nodeWorldPosition = RectToPostion(rect, PsdImporter.ScreenResolution.x, PsdImporter.ScreenResolution.y); //宽 和 高 float width = rect.width; float height = rect.height; Vector3 canvasPosition = PsdImporter.GetCanvasPosition(); RectTransform rectTransform = root.Go.transform as RectTransform; if (rectTransform.anchorMax.x == rectTransform.anchorMin.x && rectTransform.anchorMax.y == rectTransform.anchorMin.y) { rectTransform.sizeDelta = new Vector2(width, height); } //root.Go.transform.position = new Vector3(x + (rect.width) - (0.5f - root.pivot.x) * rect.width // , y - (0.5f - root.pivot.y) * rect.height, 0); root.Go.transform.position = new Vector3(nodeWorldPosition.x + canvasWorldPosition.x - (0.5f - root.pivot.x) * rect.width, nodeWorldPosition.y + canvasWorldPosition.y - (0.5f - root.pivot.y) * rect.height, 0); foreach (var node in root.children) { UpdateAllUINodeRectTransform(node); } }
/// <summary> /// Draws the Inspector GUI for the TextureImporter. /// Normal Texture files should appear as they normally do, however PSD files will have additional items. /// </summary> public override void OnInspectorGUI() { if (nativeEditor != null) { // check if it is a PSD file selected string assetPath = ((TextureImporter)target).assetPath; if (assetPath.EndsWith(".psd")) { GUILayout.Label("<b>PSD Layout Tool</b>", guiStyle, GUILayout.Height(23)); GUIContent maximumDepthLabel = new GUIContent("Maximum Depth", "The Z value of the far back plane. The PSD will be laid out from here to 0."); PsdImporter.MaximumDepth = EditorGUILayout.FloatField(maximumDepthLabel, PsdImporter.MaximumDepth); GUIContent pixelsToUnitsLabel = new GUIContent("Pixels to Unity Units", "The scale of the Sprite objects, in the number of pixels to Unity world units."); PsdImporter.PixelsToUnits = EditorGUILayout.FloatField(pixelsToUnitsLabel, PsdImporter.PixelsToUnits); //GUIContent useUnityUILabel = new GUIContent("Use Unity UI", "Create Unity UI elements instead of \"normal\" GameObjects."); //PsdImporter.UseUnityUI = EditorGUILayout.Toggle(useUnityUILabel, PsdImporter.UseUnityUI); GUIContent addCanvasUILabel = new GUIContent("Add Canvas", "Add Canvas on the GameObjects."); PsdImporter.CreateCanvas = EditorGUILayout.Toggle(addCanvasUILabel, PsdImporter.CreateCanvas); // draw our custom buttons for PSD files //if (GUILayout.Button("Export Layers as Textures")) //{ // PsdImporter.ExportLayersAsTextures(assetPath); //} //if (GUILayout.Button("Layout in Current Scene")) //{ // PsdImporter.LayoutInCurrentScene(assetPath); //} if (GUILayout.Button("Generate Prefab")) { string prefabFullPath = GetPrefabFullPath(assetPath); if (System.IO.File.Exists(prefabFullPath)) { bool b = UnityEditor.EditorUtility.DisplayDialog("提示", prefabFullPath + "已经存在,要替换吗?", "确定", "取消"); if (!b) { return; } } PsdImporter.GeneratePrefab(assetPath); } if (GUILayout.Button("Delete Prefab")) { string prefabAssetPath = PsdImporter.prefabPath + System.IO.Path.GetFileNameWithoutExtension(assetPath); prefabAssetPath = PsdImporter.GetRelativePath(prefabAssetPath); bool b = UnityEditor.EditorUtility.DisplayDialog("提示", "确定要删除 " + prefabAssetPath + " 吗?", "确定", "取消"); if (b) { bool ret = AssetDatabase.DeleteAsset(prefabAssetPath + ".prefab"); if (ret) { AssetDatabase.Refresh(); } } } GUILayout.Space(6); //GUIContent usemde = new GUIContent("compare textures by md5", "use md5"); //PsdImporter.UseMd5 = EditorGUILayout.Toggle(usemde, PsdImporter.UseMd5); if (GUILayout.Button("Delete Repeat sprites (all psd files)")) { PsdImporter.DeleteRepeatSprites(PsdImporter.UseMd5); } //if (GUILayout.Button("Delete Repeat sprites (current psd file)")) //{ // PsdImporter.DeleteCurrentPsdRepeatSprites(System.IO.Path.GetFileNameWithoutExtension(assetPath), PsdImporter.UseMd5); //} if (GUILayout.Button("Delete unused sprites")) { PsdImporter.DeleteUnusedSprites(); } GUILayout.Space(3); GUILayout.Box(string.Empty, GUILayout.Height(1), GUILayout.MaxWidth(Screen.width - 30)); GUILayout.Space(3); GUILayout.Label("<b>Unity Texture Importer Settings</b>", guiStyle, GUILayout.Height(23)); // draw the default Inspector for the PSD nativeEditor.OnInspectorGUI(); } else if (assetPath.EndsWith(".png")) { if (GUILayout.Button("delete other repeat images")) { string fullname = PsdImporter.GetFullProjectPath() + assetPath; PsdImporter.DeleteRepeatSpritesNameof(fullname); } if (GUILayout.Button("find all refenerce")) { PsdImporter.PrintAllReference(assetPath); } nativeEditor.OnInspectorGUI(); } else { // It is a "normal" Texture, not a PSD nativeEditor.OnInspectorGUI(); } } // Unfortunately we cant hide the ImportedObject section because the interal InspectorWindow checks via // "if (editor is AssetImporterEditor)" and all flags that this check sets are method local variables // so aside from direct patching UnityEditor.dll, reflection cannot be used here. // Therefore we just move the ImportedObject section out of view ////GUILayout.Space(2048); }
/// <summary> /// Draws the Inspector GUI for the TextureImporter. /// Normal Texture files should appear as they normally do, however PSD files will have additional items. /// </summary> public override void OnInspectorGUI() { if (nativeEditor != null) { // check if it is a PSD file selected string assetPath = ((TextureImporter)target).assetPath; if (assetPath.EndsWith(PsdImporter.PSD_TAIL)) { GUILayout.Label("<b>PSD Layout Tool</b>", guiStyle, GUILayout.Height(23)); //All ui depth is the same 1 //GUIContent maximumDepthLabel = new GUIContent("Maximum Depth", "The Z value of the far back plane. The PSD will be laid out from here to 0."); //PsdImporter.MaximumDepth = EditorGUILayout.FloatField(maximumDepthLabel, PsdImporter.MaximumDepth); //modify by yanru //GUIContent pixelsToUnitsLabel = new GUIContent("Pixels to Unity Units", "The scale of the Sprite objects, in the number of pixels to Unity world units."); //PsdImporter.PixelsToUnits = EditorGUILayout.FloatField(pixelsToUnitsLabel, PsdImporter.PixelsToUnits); //GUIContent useUnityUILabel = new GUIContent("Use Unity UI", "Create Unity UI elements instead of \"normal\" GameObjects."); //PsdImporter.UseUnityUI = EditorGUILayout.Toggle(useUnityUILabel, PsdImporter.UseUnityUI); //set ui width and height; GUIContent screenSize = new GUIContent("屏幕分辨率", "UI 宽*高"); PsdImporter.ScreenResolution = EditorGUILayout.Vector2Field(screenSize, PsdImporter.ScreenResolution); GUIContent imageSizeLimit = new GUIContent("小图限制(超过该尺寸 建议拆分图集)", "小图尺寸限制(超过该尺寸 建议拆出图集)"); PsdImporter.LargeImageAlarm = EditorGUILayout.Vector2Field(imageSizeLimit, PsdImporter.LargeImageAlarm); //set textFont GUIContent fontName = new GUIContent("字体名称", "字体名称"); PsdImporter.textFont = EditorGUILayout.TextField(fontName, PsdImporter.textFont); //force use fullScreenUI //GUIContent useFullScreenUI = new GUIContent("useFullScreenUI", "useFullScreenUI"); //PsdImporter.fullScreenUI = EditorGUILayout.Toggle(useFullScreenUI, PsdImporter.fullScreenUI); // draw our custom buttons for PSD files //if (GUILayout.Button("Export Layers as Textures")) //{ // PsdImporter.ExportLayersAsTextures(assetPath); //} if (GUILayout.Button("Layout in Current Scene")) { PsdImporter.LayoutInCurrentScene(assetPath); } if (GUILayout.Button("Layout in Current Scene(use image real size)")) { PsdImporter.LayoutInCurrentScene(assetPath, true); } if (GUILayout.Button("Generate Prefab")) { PsdImporter.GeneratePrefab(assetPath); } if (GUILayout.Button("test ClickButton")) { PsdImporter.TestClick(); } GUILayout.Space(3); GUILayout.Box(string.Empty, GUILayout.Height(1), GUILayout.MaxWidth(Screen.width - 30)); GUILayout.Space(3); GUILayout.Label("<b>Unity Texture Importer Settings</b>", guiStyle, GUILayout.Height(23)); // draw the default Inspector for the PSD nativeEditor.OnInspectorGUI(); } else { // It is a "normal" Texture, not a PSD nativeEditor.OnInspectorGUI(); } } // Unfortunately we cant hide the ImportedObject section because the interal InspectorWindow checks via // "if (editor is AssetImporterEditor)" and all flags that this check sets are method local variables // so aside from direct patching UnityEditor.dll, reflection cannot be used here. // Therefore we just move the ImportedObject section out of view ////GUILayout.Space(2048); }
/// <summary> /// Draws the Inspector GUI for the TextureImporter. /// Normal Texture files should appear as they normally do, however PSD files will have additional items. /// </summary> public override void OnInspectorGUI() { if (nativeEditor != null) { // check if it is a PSD file selected string assetPath = ((TextureImporter)target).assetPath; if (assetPath.EndsWith(".psd")) { GUILayout.Label("<b>PSD Layout Tool</b>", guiStyle, GUILayout.Height(23)); GUIContent maximumDepthLabel = new GUIContent("Maximum Depth", "The Z value of the far back plane. The PSD will be laid out from here to 0."); PsdImporter.MaximumDepth = EditorGUILayout.FloatField(maximumDepthLabel, PsdImporter.MaximumDepth); GUIContent pixelsToUnitsLabel = new GUIContent("Pixels to Unity Units", "The scale of the Sprite objects, in the number of pixels to Unity world units."); PsdImporter.PixelsToUnits = EditorGUILayout.FloatField(pixelsToUnitsLabel, PsdImporter.PixelsToUnits); GUIContent useUnityUILabel = new GUIContent("Use Unity UI", "Create Unity UI elements instead of \"normal\" GameObjects."); PsdImporter.UseUnityUI = EditorGUILayout.Toggle(useUnityUILabel, PsdImporter.UseUnityUI); // draw our custom buttons for PSD files if (GUILayout.Button("Export Layers as Textures")) { PsdImporter.ExportLayersAsTextures(assetPath); } if (GUILayout.Button("Layout in Current Scene")) { PsdImporter.LayoutInCurrentScene(assetPath); } if (GUILayout.Button("Generate Prefab")) { PsdImporter.GeneratePrefab(assetPath); } GUILayout.Space(3); GUILayout.Box(string.Empty, GUILayout.Height(1), GUILayout.MaxWidth(Screen.width - 30)); GUILayout.Space(3); GUILayout.Label("<b>Unity Texture Importer Settings</b>", guiStyle, GUILayout.Height(23)); // draw the default Inspector for the PSD nativeEditor.OnInspectorGUI(); } else { // It is a "normal" Texture, not a PSD nativeEditor.OnInspectorGUI(); } } // Unfortunately we cant hide the ImportedObject section because the interal InspectorWindow checks via // "if (editor is AssetImporterEditor)" and all flags that this check sets are method local variables // so aside from direct patching UnityEditor.dll, reflection cannot be used here. // Therefore we just move the ImportedObject section out of view ////GUILayout.Space(2048); }
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); }