com.soviby.unity.ui.ugui-to.../Assets/Editor/Entity/TextMeshProEntity.cs
2024-12-02 00:33:27 +08:00

48 lines
1.4 KiB
C#

#if UNITY_EDITOR
using Sirenix.OdinInspector;
using TMPro;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
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;
var rectTransform = ui.rectTransform;
rectTransform.sizeDelta = new Vector2(ElementInfo.w + 10, ElementInfo.h + 10);
}
public override void InitPreview()
{
if (ElementInfo == null) return;
if (!TryGetComponent<TextMeshProUGUI>(out m_previewText))
{
m_previewText = gameObject.AddComponent<TextMeshProUGUI>();
}
OnApplyData(m_previewText);
ApplyTransform(transform);
}
}
}
#endif