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()); return new Vector2((HandleSelfFittingAlongAxis(0, temp)), (HandleSelfFittingAlongAxis(1, temp))); } public float HandleSelfFittingAlongAxis(int axis, TMP_Text temp) { ContentSizeFitter c = temp.GetComponent(); RectTransform r = temp.GetComponent(); FitMode fitting = (axis == 0 ? c.horizontalFit : c.verticalFit); if (fitting == FitMode.MinSize) { return LayoutUtility.GetMinSize(r, axis); } else { return LayoutUtility.GetPreferredSize(r, axis); } } } }