using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Serialization; using UnityEngine.UI; public class CopyFontTextMessageOrContentSizeFitter : MonoBehaviour { private Text _text; public MessageType messageType; public Text childRect; public RectTransform prentRect; private RectTransform _headRect; private RectTransform _nameRect; private RectTransform _spriteRect; private void Start() { _text=this.GetComponent(); _headRect=prentRect.Find("head").GetComponent(); _nameRect=prentRect.Find("name").GetComponent(); _spriteRect=prentRect.Find("sprite").GetComponent(); if (messageType == MessageType.Text) { _spriteRect.gameObject.SetActive(false); _text.gameObject.SetActive(true); }else if (messageType == MessageType.Sprite) { _spriteRect.gameObject.SetActive(true); _text.gameObject.SetActive(false); } } void Update() { childRect.text = _text.text; float screenHeight = 0; if (messageType == MessageType.Text) { screenHeight = _headRect.sizeDelta.y+ _nameRect.sizeDelta.y + _text.rectTransform.sizeDelta.y; }else if (messageType == MessageType.Sprite) { screenHeight = _headRect.sizeDelta.y+ _nameRect.sizeDelta.y + _spriteRect.sizeDelta.y; } prentRect.sizeDelta = new Vector2(prentRect.sizeDelta.x, screenHeight); } } public enum MessageType { Text, Sprite, }