Unity中计算字体宽度及Text组件的自适应
主界面调用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ChatView : MonoBehaviour
{
#region RectTransform
private RectTransform ChatContent;
#endregion
#region GameObject
private GameObject LeftChatObj;
private GameObject RightChatObj;
#endregion
private void Awake()
{
InitGetComponent();
}
private void Start()
{
Test();
}
private void InitGetComponent()
{
#region RectTransform
ChatContent = transform.Find("ChatPanel/ScrollView/Viewport/ChatContent").GetComponent<RectTransform>();
#endregion
#region GameObject
LeftChatObj = Resources.Load<GameObject>("Info/LeftChatInfo");
RightChatObj = Resources.Load<GameObject>("Info/RightChatInfo");
#endregion
}
//测试方法
private void Test()
{
ChatInfo info1 = Instantiate(LeftChatObj, ChatContent).GetComponent<ChatInfo>();
ChatInfo info2