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
)); }
}