com.alicizax.unity.ui.exten.../Runtime/UXComponent/Controller/Property/TextContentState.cs

50 lines
1.4 KiB
C#
Raw Normal View History

2026-03-11 15:23:44 +08:00
using System;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
namespace AlicizaX.UI
{
/// <summary>
/// Text 内容状态控制
/// 控制 Text 或 TextMeshProUGUI 的文本内容
/// </summary>
[Serializable]
[ControlerStateName("Text/Content")]
[ControlerStateAttachType(true)]
public class TextContentState : ControllerStateBase
{
[SerializeField] [TextArea(3, 10)] private string _text = "";
public override void Init(UXControllerStateRecorder recorder)
{
}
public override void Execute(UXControllerStateRecorder recorder, int entryIndex, int selectionIndex)
{
if (recorder == null || entryIndex != selectionIndex) return;
if (recorder.TryGetComponent<Text>(out var text))
{
text.text = _text;
}
else if (recorder.TryGetComponent<TextMeshProUGUI>(out var tmp))
{
tmp.text = _text;
}
}
public override bool Valid(UXControllerStateRecorder recorder)
{
return recorder != null &&
(recorder.GetComponent<Text>() != null || recorder.GetComponent<TextMeshProUGUI>() != null);
}
public override string GetDescription()
{
string preview = _text.Length > 30 ? _text.Substring(0, 30) + "..." : _text;
return $"匹配时: \"{preview}\"";
}
}
}