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

48 lines
1.4 KiB
C#
Raw Normal View History

2024-11-28 10:34:17 +00:00
#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);
}
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);
}
}
}
#endif