You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
4 years ago
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using UnityEngine;
|
||
|
using UnityEngine.UI;
|
||
|
|
||
|
[RequireComponent(typeof(Text))]
|
||
|
public class SetTextWidth : MonoBehaviour {
|
||
|
|
||
|
// Use this for initialization
|
||
|
public float maxWidth;
|
||
|
public float minWidth;
|
||
|
private Text text;
|
||
|
private float width;
|
||
|
void Start ()
|
||
|
{
|
||
|
|
||
|
}
|
||
|
private void Awake()
|
||
|
{
|
||
|
text = GetComponent<Text>();
|
||
|
}
|
||
|
// Update is called once per frame
|
||
|
void Update ()
|
||
|
{
|
||
|
var newWidth = text.preferredWidth;
|
||
|
if (newWidth!= width)
|
||
|
{
|
||
|
setControlWidth();
|
||
|
width = newWidth;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
private void setControlWidth()
|
||
|
{
|
||
|
if (text.preferredWidth > maxWidth)
|
||
|
{
|
||
|
text.rectTransform.sizeDelta = new Vector2(maxWidth, text.rectTransform.sizeDelta.y);
|
||
|
}
|
||
|
else if (text.preferredWidth < minWidth)
|
||
|
{
|
||
|
text.rectTransform.sizeDelta = new Vector2(minWidth, text.rectTransform.sizeDelta.y);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
text.rectTransform.sizeDelta = new Vector2(text.preferredWidth, text.rectTransform.sizeDelta.y);
|
||
|
}
|
||
|
}
|
||
|
}
|