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

109 lines
3.9 KiB
C#
Raw Permalink Normal View History

2024-11-28 10:34:17 +00:00
#if UNITY_EDITOR
using Sirenix.OdinInspector;
2024-12-02 14:14:39 +00:00
using Sirenix.Utilities;
using System.Linq;
2024-11-28 10:34:17 +00:00
using TMPro;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;
2024-12-02 14:14:39 +00:00
using UnityEngine.TextCore.Text;
2024-11-28 10:34:17 +00:00
using UnityEngine.UI;
2024-12-02 14:14:39 +00:00
using static UguiToolkit.Editor.LayoutInfo;
2024-11-28 10:34:17 +00:00
namespace UguiToolkit.Editor
{
public class TextMeshProEntity : BaseEntity<TextMeshProUGUI, LayoutInfo.TextInfo>
{
private TextMeshProUGUI m_previewText;
public override void ApplyTransform(Transform tf)
{
var position = ElementInfo.Position;
tf.position = StageManager.Instance.PrefabContentsRoot.transform.TransformPoint(new Vector3(position.x, position.y, 0));
tf.rotation = Quaternion.identity;
}
protected override void OnApplyData(TextMeshProUGUI ui)
{
ui.text = ElementInfo.text;
ui.fontSize = (int)ElementInfo.size;
ui.color = ElementInfo.color;
ui.alignment = TextAlignmentOptions.Center;
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>();
}
OnApplyData(m_previewText);
ApplyTransform(transform);
}
2024-12-02 14:14:39 +00:00
private static void SetTMPByTextInfo(TextMeshProUGUI ui, LayoutInfo.TextInfo textInfo)
{
// TODO: <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>չ
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)
{
2024-12-03 05:05:13 +00:00
float CalcColorDifference(in Color color1, in Color color2)
{
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