41 lines
1.1 KiB
C#
41 lines
1.1 KiB
C#
|
using UnityEngine;
|
|||
|
using UnityEngine.UI;
|
|||
|
using System.Collections;
|
|||
|
|
|||
|
//字体变大缩小处理的 ,现在在游戏中直接修改Text的Scale会变模糊,所以改成修改Text的FontSize (目前该功能只能想到这种方法)
|
|||
|
public class TextFontScale : MonoBehaviour {
|
|||
|
|
|||
|
int fontSize = 1;
|
|||
|
Text m_textScript = null;
|
|||
|
Vector2 m_Delta = Vector2.one;
|
|||
|
RectTransform m_rect = null;
|
|||
|
|
|||
|
void Start () {
|
|||
|
|
|||
|
m_textScript = GetComponent<Text>();
|
|||
|
if (m_textScript == null)
|
|||
|
return;
|
|||
|
fontSize = m_textScript.fontSize;
|
|||
|
m_rect = GetComponent<RectTransform>();
|
|||
|
if(m_rect != null)
|
|||
|
{
|
|||
|
m_Delta = m_rect.sizeDelta;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void Update () {
|
|||
|
|
|||
|
if (m_textScript == null)
|
|||
|
return;
|
|||
|
if (m_rect == null)
|
|||
|
return;
|
|||
|
float scaleX = m_rect.sizeDelta.x / m_Delta.x;
|
|||
|
float scaleY = m_rect.sizeDelta.y / m_Delta.y;
|
|||
|
if(scaleX == scaleY)
|
|||
|
{
|
|||
|
int size = (int)(fontSize * scaleX);
|
|||
|
m_textScript.fontSize = size;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|