From b8f841a35bc10fda8821bec0c76417f5f2ef5dfa Mon Sep 17 00:00:00 2001 From: biaosong Date: Mon, 2 Dec 2024 22:14:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AD=97=E4=BD=93=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../JSZXPsd2Unity/PSD导出Unity.js | 66 +++++++++++++------ .PhotoshopScript/JSZXPsd2Unity/run_step2.bat | 2 +- Assets/Editor/Entity/IEntity.cs | 2 +- Assets/Editor/Entity/TextMeshProEntity.cs | 65 +++++++++++++++++- Assets/Editor/Helper/LayoutParser.cs | 12 +++- Assets/Editor/Manager/EntityManager.cs | 18 ++++- .../Editor/ScriptObject/CacheScriptObject.cs | 4 ++ .../ScriptObject/SettingScriptObject.cs | 28 +++++++- Assets/UguiToolkit.Editor.asmdef | 3 +- 9 files changed, 168 insertions(+), 32 deletions(-) diff --git a/.PhotoshopScript/JSZXPsd2Unity/PSD导出Unity.js b/.PhotoshopScript/JSZXPsd2Unity/PSD导出Unity.js index 52aab7b..824daad 100644 --- a/.PhotoshopScript/JSZXPsd2Unity/PSD导出Unity.js +++ b/.PhotoshopScript/JSZXPsd2Unity/PSD导出Unity.js @@ -148,7 +148,7 @@ function s2t(t) { return stringIDToTypeID(t) } this.layerBlendAll(copiedDoc, copiedDoc); // this.removeCommentoutLayers(copiedDoc, copiedDoc); this.cropLayers(copiedDoc); - this.resizePsd(copiedDoc); + // this.resizePsd(copiedDoc); this.selectDocumentArea(copiedDoc); this.ungroupArtboard(copiedDoc); this.clipping(copiedDoc, copiedDoc); @@ -312,37 +312,54 @@ function s2t(t) { return stringIDToTypeID(t) } { layer.name = "#" + layer.name; } + }else{ // 文字 + if (layer.name.startsWith("%")) + { + layer.name = layer.name.substring(1, layer.name.length); + } + else + { + layer.name = "#" + layer.name.substring(1, layer.name.length); + } } } - - var ss = layer.name.split("@"); - //忽略但保留父节点宽高影响,则不移除 - if (Util.isInferiorIgnoreByName(ss[0])) { + + //解锁图层 + if (layer.allLocked) + layer.allLocked = false + + if (Util.isIgnoreByName(layer.name)) { + removeLayers.push(layer) continue } + // 去空格 + layer.name = layer.name.replace(/\s+/g, ""); + // 特殊符号处理 + layer.name = layer.name.replace(/¥/g, "-").replace(/\./g, "-") + .replace(/\[/g, "-").replace(/\]/g, "-").replace(/\#/g, "-") + .replace(/\:/g, "-").replace(/\%/g, "-").replace(/\,/g, "-") + .replace(/\+/g, "-"); + //移除不可见图层 - if (layer.visible === false || Util.isIgnoreByName(ss[0])) { + if (layer.visible === false) { // 不可见的就push到移除layer中去 removeLayers.push(layer) continue } - if (layer.bounds[0].value === 0 && layer.bounds[1].value === 0 && layer.bounds[2].value === 0 && layer.bounds[3].value === 0) { removeLayers.push(layer) continue } - //解锁所有图层 - if (layer.allLocked) - layer.allLocked = false - //光栅所有图层 if (layer.typename === 'ArtLayer') { if (layer.kind !== LayerKind.TEXT) { this.rasterize(layer) } } + + var ss = layer.name.split("@"); opt = Util.parseOption(ss[1]); //检查大小命名格式 @@ -384,11 +401,23 @@ function s2t(t) { return stringIDToTypeID(t) } } //文件名合规检测 - //var reg = /^[0-9a-zA-Z\u4e00-\u9fa5"_""\-"]*$/g; + var regCn = /^[0-9a-zA-Z\u4e00-\u9fa5"_""\-"]*$/g; var reg = /^[0-9a-zA-Z"_""\-"]*$/g; - if (!reg.test(ss[0])) { - alert("\"" + layer.name + "\" 名字不合法,只能含有英文、数字、-、_") - return false; + // if (!reg.test(ss[0])) { + // alert("\"" + layer.name + "\" 名字不合法,只能含有英文、数字、-、_") + // return false; + // } + if (layer.typename === 'ArtLayer' && layer.kind !== LayerKind.TEXT) + { + if (!reg.test(ss[0])) { + alert("\"" + layer.name + "\" 名字不合法,只能含有英文、数字、-、_") + return false; + } + }else{ + if (!regCn.test(ss[0])) { + alert("\"" + layer.name + "\" 名字不合法,只能含有中文、英文、数字、-、_") + return false; + } } //递归检测 @@ -1482,12 +1511,7 @@ function s2t(t) { return stringIDToTypeID(t) } // 忽略 Util.isIgnoreByName = function (name) { - return name.startsWith('#') || Util.isInferiorIgnoreByName(name) - }; - - // 忽略,但保留父节点宽高影响 - Util.isInferiorIgnoreByName = function (name) { - return name.startsWith('$') + return name.startsWith('#') }; Util.UpdateHash = function (hashTarget, hashRes) { diff --git a/.PhotoshopScript/JSZXPsd2Unity/run_step2.bat b/.PhotoshopScript/JSZXPsd2Unity/run_step2.bat index 3773845..b14fbbc 100644 --- a/.PhotoshopScript/JSZXPsd2Unity/run_step2.bat +++ b/.PhotoshopScript/JSZXPsd2Unity/run_step2.bat @@ -1,2 +1,2 @@ cd /d %~dp0 -start step2.exe -src C:\Users\93685\Desktop\Sample -dst C:\Users\93685\Desktop\Sample_output +start step2.exe -src C:\Users\biaosong\Desktop\yueka -dst C:\Users\biaosong\Desktop\yueka_output diff --git a/Assets/Editor/Entity/IEntity.cs b/Assets/Editor/Entity/IEntity.cs index ea73dcc..e97ffa3 100644 --- a/Assets/Editor/Entity/IEntity.cs +++ b/Assets/Editor/Entity/IEntity.cs @@ -70,7 +70,7 @@ namespace UguiToolkit.Editor rtf.sizeDelta = new Vector2(m_elementInfo.w, m_elementInfo.h); m_selectionImg = go.AddComponent(); - m_selectionImg.color = new Color(0, 1, 0, 0.15f); + m_selectionImg.color = new Color(0, 1, 0, 0.2f); } } } diff --git a/Assets/Editor/Entity/TextMeshProEntity.cs b/Assets/Editor/Entity/TextMeshProEntity.cs index 42f233d..ffda10b 100644 --- a/Assets/Editor/Entity/TextMeshProEntity.cs +++ b/Assets/Editor/Entity/TextMeshProEntity.cs @@ -1,10 +1,15 @@ #if UNITY_EDITOR +using OfficeOpenXml.FormulaParsing.Excel.Functions.Math; using Sirenix.OdinInspector; +using Sirenix.Utilities; +using System.Linq; using TMPro; using Unity.Mathematics; using UnityEditor; using UnityEngine; +using UnityEngine.TextCore.Text; using UnityEngine.UI; +using static UguiToolkit.Editor.LayoutInfo; namespace UguiToolkit.Editor { @@ -26,8 +31,7 @@ namespace UguiToolkit.Editor ui.color = ElementInfo.color; ui.alignment = TextAlignmentOptions.Center; - var rectTransform = ui.rectTransform; - rectTransform.sizeDelta = new Vector2(ElementInfo.w + 10, ElementInfo.h + 10); + SetTMPByTextInfo(ui, ElementInfo); } public override void InitPreview() @@ -42,6 +46,63 @@ namespace UguiToolkit.Editor ApplyTransform(transform); } + + private static void SetTMPByTextInfo(TextMeshProUGUI ui, LayoutInfo.TextInfo textInfo) + { + // TODO: չ + if (GetTextFontPreset(textInfo, out var textFontPreset)) + { + ui.font = textFontPreset.tmpAsset; + + if (!string.IsNullOrEmpty(textFontPreset.materialPreset)) + { + var materialPresets = TMPro.EditorUtilities.TMP_EditorUtility.FindMaterialReferences(textFontPreset.tmpAsset); + if (materialPresets != null) + { + var material = materialPresets.FirstOrDefault((material) => { + if (material.name == textFontPreset.materialPreset) return true; + return false; + }); + + if (material) ui.fontMaterial = material; + } + } + + var rectTransform = ui.rectTransform; + rectTransform.sizeDelta = new Vector2(textInfo.w + textFontPreset.sizeOffset.x, textInfo.h + textFontPreset.sizeOffset.y); + } + } + + private static bool GetTextFontPreset(LayoutInfo.TextInfo textInfo, out TextFontPresetOfUnity textFontPreset) + { + textFontPreset = null; + var setting = GlobalManager.Instance.setting; + if (setting != null && setting.textFontList != null) + { + foreach (var textFontInfoOfPsd in setting.textFontList) + { + if (textFontInfoOfPsd.fontNameOfPsd != null && textFontInfoOfPsd.fontNameOfPsd.Contains(textInfo.font)) + { + if (textFontInfoOfPsd.stroke) + { + if (!textInfo.IsStroke) return false; + if (!textFontInfoOfPsd.allStrokeColor && textFontInfoOfPsd.strokeColor != textInfo.strokeColor) return false; + textFontPreset = textFontInfoOfPsd; + return true; + } + else + { + if (!textInfo.IsStroke) { + textFontPreset = textFontInfoOfPsd; + return true; + } + } + } + } + } + + return false; + } } } diff --git a/Assets/Editor/Helper/LayoutParser.cs b/Assets/Editor/Helper/LayoutParser.cs index f46b0a9..62252d5 100644 --- a/Assets/Editor/Helper/LayoutParser.cs +++ b/Assets/Editor/Helper/LayoutParser.cs @@ -37,16 +37,22 @@ namespace UguiToolkit.Editor } else if (layoutElementJsonData.type == "Text") { - ColorUtility.TryParseHtmlString("#" + layoutElementJsonData.color, out var color); + ColorUtility.TryParseHtmlString("#" + layoutElementJsonData.color, out var fontColor); + Color strokeColor = Color.white; + if (!string.IsNullOrEmpty(layoutElementJsonData.strokeColor)) + ColorUtility.TryParseHtmlString("#" + layoutElementJsonData.strokeColor, out strokeColor); elementInfos.Add(new LayoutInfo.TextInfo() { name = layoutElementJsonData.name, text = layoutElementJsonData.text, font = layoutElementJsonData.font, + strokeSize = layoutElementJsonData.strokeSize, + strokeColor = strokeColor, + size = layoutElementJsonData.size, align = layoutElementJsonData.align, - color = color, + color = fontColor, x = layoutElementJsonData.x, y = layoutElementJsonData.y, w = layoutElementJsonData.w, @@ -113,6 +119,8 @@ namespace UguiToolkit.Editor public string color; public string align; public string font; + public float strokeSize; + public string strokeColor; } } diff --git a/Assets/Editor/Manager/EntityManager.cs b/Assets/Editor/Manager/EntityManager.cs index f70254d..25875b6 100644 --- a/Assets/Editor/Manager/EntityManager.cs +++ b/Assets/Editor/Manager/EntityManager.cs @@ -77,6 +77,10 @@ namespace UguiToolkit.Editor { entity.ApplyData(text); } + else if (tf.TryGetComponent(out var temp)) + { + entity.ApplyData(temp); + } m_lastSelectionGo = m_curSelectionGo; m_lastSelectionEntity = entity; @@ -160,11 +164,12 @@ namespace UguiToolkit.Editor private void OnSelectionChanged() { + if (m_noSelection) return; + m_curSelectionGo = null; m_selectionEntities.Clear(); m_entityRoot.gameObject.SetActive(false); - if (m_noSelection) return; if (Selection.activeGameObject != null && m_panelCache != null) { var activeGameObject = Selection.activeGameObject; @@ -196,7 +201,8 @@ namespace UguiToolkit.Editor else { imgEntity.SetTransform(rotScale.rotiation, rotScale.scale, false); } - + + imgEntity.ApplyTransform(imgEntity.transform); imgEntity.ShowSelectionImg(true); m_selectionEntities.Add(imgEntity); @@ -225,7 +231,8 @@ namespace UguiToolkit.Editor textEntity.gameObject.SetActive(false); } } - else if (activeGameObject.TryGetComponent(out var text)) + else if (activeGameObject.TryGetComponent(out var _) + || activeGameObject.TryGetComponent(out var _)) { bool IsInside = false; m_entityRoot.gameObject.SetActive(true); @@ -291,6 +298,11 @@ namespace UguiToolkit.Editor m_entityRoot.localRotation = Quaternion.identity; m_entityRoot.localScale = Vector3.one; + var canvas = m_entityRoot.gameObject.AddComponent(); + canvas.pixelPerfect = true; + canvas.overrideSorting = true; + canvas.sortingOrder = 200; + m_imageEntities = new(m_panelCache.layoutInfo.Count); m_textEntities = new(m_panelCache.layoutInfo.Count); m_selectionEntities = new(m_panelCache.layoutInfo.Count); diff --git a/Assets/Editor/ScriptObject/CacheScriptObject.cs b/Assets/Editor/ScriptObject/CacheScriptObject.cs index de33464..759806d 100644 --- a/Assets/Editor/ScriptObject/CacheScriptObject.cs +++ b/Assets/Editor/ScriptObject/CacheScriptObject.cs @@ -228,6 +228,10 @@ namespace UguiToolkit.Editor public Color color; public string align; public string font; + public float strokeSize; + public Color strokeColor; + + public bool IsStroke => strokeSize != 0; } } diff --git a/Assets/Editor/ScriptObject/SettingScriptObject.cs b/Assets/Editor/ScriptObject/SettingScriptObject.cs index 1c5319e..ba7ffc9 100644 --- a/Assets/Editor/ScriptObject/SettingScriptObject.cs +++ b/Assets/Editor/ScriptObject/SettingScriptObject.cs @@ -1,16 +1,42 @@ #if UNITY_EDITOR using Sirenix.OdinInspector; +using System; +using System.Collections.Generic; +using Unity.Mathematics; using UnityEngine; namespace UguiToolkit.Editor { - public class SettingScriptObject : ScriptableObject + public class SettingScriptObject : SerializedScriptableObject { [LabelText("ui预制体存放的路径"), FolderPath] public string prefabForUIDirPath; public float distanceDifference = 0.2f; public bool useTMP; + + // 项目配置 + public List textFontList = new (); + } + + + [Serializable] + public class TextFontPresetOfUnity + { + [Title("Ps属性")] + public string name; + public List fontNameOfPsd; + public bool stroke; // 使用描边 + public Color strokeColor; + public bool allStrokeColor; + public bool shadow; // 使用阴影 + public Color shadowColor; + public bool allShadowColor; + + [Title("Unity属性")] + public float2 sizeOffset; + public TMPro.TMP_FontAsset tmpAsset; + public string materialPreset; } } #endif \ No newline at end of file diff --git a/Assets/UguiToolkit.Editor.asmdef b/Assets/UguiToolkit.Editor.asmdef index 5ad6be7..12022c0 100644 --- a/Assets/UguiToolkit.Editor.asmdef +++ b/Assets/UguiToolkit.Editor.asmdef @@ -3,7 +3,8 @@ "rootNamespace": "", "references": [ "GUID:d8b63aba1907145bea998dd612889d6b", - "GUID:6055be8ebefd69e48b49212b09b47b2f" + "GUID:6055be8ebefd69e48b49212b09b47b2f", + "GUID:6546d7765b4165b40850b3667f981c26" ], "includePlatforms": [ "Editor",