58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
using Cysharp.Threading.Tasks;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using TMPro;
|
||
using UnityEngine;
|
||
using UnityEngine.UI;
|
||
using static UnityEngine.UI.ContentSizeFitter;
|
||
//============================================================
|
||
//支持中文,文件使用UTF-8编码
|
||
//@author Adam
|
||
//@create 20240710
|
||
//@company Adam
|
||
//
|
||
//@description:
|
||
//============================================================
|
||
namespace Components
|
||
{
|
||
public class ThreeDtipAttach : MonoBehaviour
|
||
{
|
||
public TMP_Text selfTMP;
|
||
public RectTransform selfRT;
|
||
// Use this for initialization
|
||
private void OnEnable()
|
||
{
|
||
ComputeSize();
|
||
}
|
||
|
||
private async void ComputeSize()
|
||
{
|
||
await UniTask.Delay(10);
|
||
GetPreferredSize(selfTMP);
|
||
//await UniTask.Delay(10);
|
||
selfRT.sizeDelta = new Vector2(GetPreferredSize(selfTMP).x + 20f, selfRT.sizeDelta.y);
|
||
}
|
||
|
||
public Vector2 GetPreferredSize(TMP_Text temp)
|
||
{
|
||
LayoutRebuilder.ForceRebuildLayoutImmediate(temp.GetComponent<RectTransform>());
|
||
return new Vector2((HandleSelfFittingAlongAxis(0, temp)), (HandleSelfFittingAlongAxis(1, temp)));
|
||
}
|
||
|
||
public float HandleSelfFittingAlongAxis(int axis, TMP_Text temp)
|
||
{
|
||
ContentSizeFitter c = temp.GetComponent<ContentSizeFitter>();
|
||
RectTransform r = temp.GetComponent<RectTransform>();
|
||
FitMode fitting = (axis == 0 ? c.horizontalFit : c.verticalFit);
|
||
if (fitting == FitMode.MinSize)
|
||
{
|
||
return LayoutUtility.GetMinSize(r, axis);
|
||
}
|
||
else
|
||
{
|
||
return LayoutUtility.GetPreferredSize(r, axis);
|
||
}
|
||
}
|
||
}
|
||
}
|