Hi,
I didn't find any included script that would scale a label to predefined box area.
I think it is a very important feature when considering localizations and custom input.
ie. If you want the text to fit inside a button even if the text might be very different length in different languages.
Luckily the NGUI is flexible enough that making such script was simple enough.
In case someone else needs this I'll post the code here.
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
[RequireComponent
(typeof(UIBaseLabel
))] public class LabelFit : MonoBehaviour
{
public enum FitType
{
First,
Horizontal,
Vertical,
Both
}
public FitType type = FitType.First;
public float width = 100;
public float height = 100;
UIBaseLabel m_label = null;
void OnEnable()
{
m_label = GetComponent<UIBaseLabel>();
}
public void Update()
{
float width_factor = width / m_label.relativeSize.x;
float height_factor = height / m_label.relativeSize.y;
Vector3 local_scale = m_label.transform.localScale;
if (type == FitType.First)
{
if (width_factor < height_factor) // Find first limiting factor and adjust the other one to achieve uniform scaling
local_scale.x = local_scale.y = width_factor;
else
local_scale.x = local_scale.y = height_factor;
}
else
{
if (type == FitType.Both || type == FitType.Horizontal)
local_scale.x = width_factor;
if (type == FitType.Both || type == FitType.Vertical)
local_scale.y = height_factor;
}
m_label.transform.localScale = local_scale;
}
public void OnDrawGizmos()
{
// Handles only the case when pivot is in center.
Gizmos.color = Color.green;
Gizmos
.DrawWireCube(m_label
.transform.position,
new Vector3
(width
* transform
.parent.lossyScale.x, height
* transform
.parent.lossyScale.y, 0
.01f
)); }
}
Usage:
Attach this script to a label, set the fit type and adjust the box size. Doesn't store the original scale before attaching the label, that could be added to make it more convenient to switch between fit types.