ソースコード
using System.Linq;
using TMPro;
public static class TMP_TextExtensionMethods
{
public static float GetLocalEdgeLeft( this TMP_Text self )
{
return self.transform.localPosition.x + self.textInfo.characterInfo.Min( x => x.bottomLeft.x );
}
public static float GetLocalEdgeBottom( this TMP_Text self )
{
return self.transform.localPosition.y + self.textInfo.characterInfo.Min( x => x.bottomLeft.y );
}
public static float GetLocalEdgeRight( this TMP_Text self )
{
return self.transform.localPosition.x + self.textInfo.characterInfo.Max( x => x.topRight.x );
}
public static float GetLocalEdgeTop( this TMP_Text self )
{
return self.transform.localPosition.y + self.textInfo.characterInfo.Max( x => x.topRight.y );
}
}
使用例
using TMPro;
using UnityEngine;
public class Example : MonoBehaviour
{
public TMP_Text m_tmpText;
private void Update()
{
var x = m_tmpText.GetLocalEdgeLeft();
var y = m_tmpText.GetLocalEdgeBottom();
transform.localPosition = new( x, y );
}
}