com.soviby.unity.ui.ugui-to.../Assets/Editor/Entity/TextMeshProEntity.cs

162 lines
5.8 KiB
C#
Raw Normal View History

2024-11-28 10:34:17 +00:00
#if UNITY_EDITOR
using Sirenix.OdinInspector;
2024-12-06 10:46:00 +00:00
using System;
2024-12-02 14:14:39 +00:00
using System.Linq;
2024-11-28 10:34:17 +00:00
using TMPro;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;
namespace UguiToolkit.Editor
{
public class TextMeshProEntity : BaseEntity<TextMeshProUGUI, LayoutInfo.TextInfo>
{
private TextMeshProUGUI m_previewText;
2024-12-10 10:14:38 +00:00
protected override void OnApplyTransform(Transform tf)
2024-11-28 10:34:17 +00:00
{
2024-12-06 10:46:00 +00:00
var rt = tf as RectTransform;
var pos = ElementInfo.Position;
2024-12-17 03:07:00 +00:00
Vector2 size = new Vector2(ElementInfo.w, ElementInfo.h);
2024-12-06 10:46:00 +00:00
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);
2024-12-17 03:07:00 +00:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size.x);
rt.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size.y);
2024-12-06 10:46:00 +00:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
rt.position = worldPos;
rt.rotation = rotation;
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>
var oldRect = rt.rect;
2024-12-17 03:07:00 +00:00
var oldLocalPosition = rt.localPosition;
2024-12-06 10:46:00 +00:00
rt.pivot = oldPiovt;
2024-12-17 03:07:00 +00:00
rt.localPosition = oldLocalPosition;
2024-12-06 10:46:00 +00:00
var offsetRectPos = oldRect.position - rt.rect.position;
rt.Translate(offsetRectPos);
2024-11-28 10:34:17 +00:00
}
protected override void OnApplyData(TextMeshProUGUI ui)
{
2024-12-02 14:14:39 +00:00
SetTMPByTextInfo(ui, ElementInfo);
2024-11-28 10:34:17 +00:00
}
2024-12-01 16:33:27 +00:00
public override void InitPreview()
2024-11-28 10:34:17 +00:00
{
if (ElementInfo == null) return;
if (!TryGetComponent<TextMeshProUGUI>(out m_previewText))
{
m_previewText = gameObject.AddComponent<TextMeshProUGUI>();
2024-12-17 03:07:00 +00:00
m_previewText.alignment = TextAlignmentOptions.Center;
2024-11-28 10:34:17 +00:00
}
OnApplyData(m_previewText);
ApplyTransform(transform);
}
2024-12-02 14:14:39 +00:00
2024-12-17 03:07:00 +00:00
// <20>ڱ<EFBFBD>֤<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD><EAB2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>ʽ
private static void SetAlignment(TextMeshProUGUI textMeshPro, TextAlignmentOptions newAlignment)
{
textMeshPro.ForceMeshUpdate();
// <20><>ȡ<EFBFBD><C8A1>ǰ<EFBFBD>ı<EFBFBD><C4B1>ı߽<C4B1><DFBD><EFBFBD>
Bounds originalBounds = textMeshPro.textBounds;
// <20><><EFBFBD><EFBFBD><EFBFBD>µĶ<C2B5><C4B6>ʽ
textMeshPro.alignment = newAlignment;
// <20><><EFBFBD>²<EFBFBD><C2B2><EFBFBD>
textMeshPro.ForceMeshUpdate();
// <20><>ȡ<EFBFBD>µı߽<C4B1><DFBD><EFBFBD>
Bounds newBounds = textMeshPro.textBounds;
// <20><><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
Vector3 offset = originalBounds.center - newBounds.center;
// <20><><EFBFBD><EFBFBD>TextMeshPro<72><6F>λ<EFBFBD><CEBB>
textMeshPro.transform.position += offset;
}
2024-12-02 14:14:39 +00:00
private static void SetTMPByTextInfo(TextMeshProUGUI ui, LayoutInfo.TextInfo textInfo)
{
2024-12-10 10:14:38 +00:00
// TODO: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ
2024-12-06 10:46:00 +00:00
2024-12-17 03:07:00 +00:00
// <20><><EFBFBD>浱ǰ<E6B5B1>Ķ<EFBFBD><C4B6>ʽ
TextAlignmentOptions originalAlignment = ui.alignment;
2024-12-06 10:46:00 +00:00
float2 sizeOffset = float2.zero;
2024-12-10 10:14:38 +00:00
2024-12-17 03:07:00 +00:00
ui.alignment = TextAlignmentOptions.Center;
2024-12-10 10:14:38 +00:00
if (!ui.hasStringID) ui.text = textInfo.text;
ui.fontSize = (int)textInfo.size;
ui.color = textInfo.color;
2024-12-02 14:14:39 +00:00
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;
}
}
2024-12-06 10:46:00 +00:00
ui.fontStyle = textInfo.font.ToLower().Contains("bold") ? TMPro.FontStyles.Bold : TMPro.FontStyles.Normal;
sizeOffset = textFontPreset.sizeOffset;
2024-12-02 14:14:39 +00:00
}
2024-12-06 10:46:00 +00:00
// <20><><EFBFBD><EFBFBD>margins
ui.margin = Vector4.zero;
// <20><><EFBFBD><EFBFBD><EFBFBD>ּ<EFBFBD><D6BC><EFBFBD>
ui.characterSpacing = 0f;
2024-12-17 03:07:00 +00:00
// <20><><EFBFBD>ö<EFBFBD><C3B6>ʽ
if (originalAlignment != TextAlignmentOptions.Center)
SetAlignment(ui, originalAlignment);
2024-12-02 14:14:39 +00:00
}
private static bool GetTextFontPreset(LayoutInfo.TextInfo textInfo, out TextFontPresetOfUnity textFontPreset)
{
2024-12-06 10:46:00 +00:00
float CalcColorDifference(in UnityEngine.Color color1, in UnityEngine.Color color2)
2024-12-03 05:05:13 +00:00
{
return math.abs(color1.r - color2.r) * 255 + math.abs(color1.g - color2.g) * 255 + math.abs(color1.b - color2.b) * 255;
}
2024-12-02 14:14:39 +00:00
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)
{
2024-12-03 05:05:13 +00:00
if (!textInfo.IsStroke) continue;
if (!textFontInfoOfPsd.allStrokeColor)
{
if (CalcColorDifference(textFontInfoOfPsd.strokeColor ,textInfo.strokeColor) > textFontInfoOfPsd.strokeColorThreshold) continue;
2024-12-02 14:14:39 +00:00
}
}
2024-12-03 05:05:13 +00:00
textFontPreset = textFontInfoOfPsd;
return true;
2024-12-02 14:14:39 +00:00
}
}
}
return false;
}
2024-11-28 10:34:17 +00:00
}
}
#endif