#if UNITY_EDITOR using Sirenix.OdinInspector; using System; using System.Linq; using TMPro; using Unity.Mathematics; using UnityEditor; using UnityEngine; namespace UguiToolkit.Editor { public class TextMeshProEntity : BaseEntity { private TextMeshProUGUI m_previewText; protected override void OnApplyTransform(Transform tf) { var rt = tf as RectTransform; var pos = ElementInfo.Position; var worldPos = StageManager.Instance.PrefabContentsRoot.transform.TransformPoint(new Vector3(pos.x, pos.y, 0)); Quaternion rotation = Quaternion.identity; var oldPiovt = rt.pivot; rt.pivot = new Vector2(0.5f, 0.5f); // 设置世界坐标 rt.position = worldPos; rt.rotation = rotation; //修正中心点 var oldRect = rt.rect; rt.pivot = oldPiovt; var offsetRectPos = oldRect.position - rt.rect.position; rt.Translate(offsetRectPos); } protected override void OnApplyData(TextMeshProUGUI ui) { SetTMPByTextInfo(ui, ElementInfo); } public override void InitPreview() { if (ElementInfo == null) return; if (!TryGetComponent(out m_previewText)) { m_previewText = gameObject.AddComponent(); } OnApplyData(m_previewText); ApplyTransform(transform); } private static void SetTMPByTextInfo(TextMeshProUGUI ui, LayoutInfo.TextInfo textInfo) { // TODO: 自行扩展 float2 sizeOffset = float2.zero; if (!ui.hasStringID) ui.text = textInfo.text; ui.fontSize = (int)textInfo.size; ui.color = textInfo.color; ui.alignment = TextAlignmentOptions.Center; 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; } } ui.fontStyle = textInfo.font.ToLower().Contains("bold") ? TMPro.FontStyles.Bold : TMPro.FontStyles.Normal; sizeOffset = textFontPreset.sizeOffset; } var rt = ui.rectTransform; var anchorMin = rt.anchorMin; var anchorMax = rt.anchorMax; // 调整大小 rt.anchorMin = new Vector2(0.5f, 0.5f); rt.anchorMax = anchorMin; rt.sizeDelta = new Vector2(textInfo.w + sizeOffset.x, textInfo.h + sizeOffset.y); rt.anchorMin = anchorMin; rt.anchorMax = anchorMax; // 调整margins ui.margin = Vector4.zero; // 设置字间距 ui.characterSpacing = 0f; } private static bool GetTextFontPreset(LayoutInfo.TextInfo textInfo, out TextFontPresetOfUnity textFontPreset) { float CalcColorDifference(in UnityEngine.Color color1, in UnityEngine.Color color2) { return math.abs(color1.r - color2.r) * 255 + math.abs(color1.g - color2.g) * 255 + math.abs(color1.b - color2.b) * 255; } 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) continue; if (!textFontInfoOfPsd.allStrokeColor) { if (CalcColorDifference(textFontInfoOfPsd.strokeColor ,textInfo.strokeColor) > textFontInfoOfPsd.strokeColorThreshold) continue; } } textFontPreset = textFontInfoOfPsd; return true; } } } return false; } } } #endif