Created
November 30, 2016 12:14
-
-
Save iwashihead/db6e88e7de74f43c43fb9ebbd6769516 to your computer and use it in GitHub Desktop.
【Unity】【UI】テキストのサイズ調整を行うコンポーネントです
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.UI; | |
/// <summary> | |
/// テキストのサイズ調整を行うコンポーネントです | |
/// </summary> | |
[ExecuteInEditMode] | |
[DisallowMultipleComponent] | |
[RequireComponent(typeof(Text))] | |
public class TextSizeAdjuster : MonoBehaviour | |
{ | |
public enum AdjustMode { None, Height, Width, HeightAndWidth } | |
public AdjustMode Mode; | |
public bool AlwaysUpdate; | |
/// <summary>最大横幅 0以下で制限なし</summary> | |
public float MaxWidth = -1f; | |
/// <summary>最大縦幅 0以下で制限なし</summary> | |
public float MaxHeight = -1f; | |
/// <summary>最小横幅 0以下で制限なし</summary> | |
public float MinWidth = -1f; | |
/// <summary>最小縦幅 0以下で制限なし</summary> | |
public float MinHeight = -1f; | |
private Text text; | |
private Text Text { get { return text ?? (text = GetComponent<Text>()); } } | |
private Vector2 buffer = Vector2.zero; | |
void Start() | |
{ | |
Adjust(); | |
} | |
void Update() | |
{ | |
if (AlwaysUpdate) Adjust(); | |
} | |
void OnValidate() | |
{ | |
Adjust(); | |
} | |
[ContextMenu("Adjust")] | |
public void Adjust() | |
{ | |
// 水平方向に制限をかける場合、Textを折り返し無し設定にする. | |
// これを行わないとText.preferredWidthの値がText.textを更新した直後のみ大きい値をとる | |
if ((Mode == AdjustMode.Width || Mode == AdjustMode.HeightAndWidth) && text.horizontalOverflow != HorizontalWrapMode.Overflow) | |
{ | |
text.horizontalOverflow = HorizontalWrapMode.Overflow; | |
} | |
switch (Mode) | |
{ | |
case AdjustMode.Height: | |
AdjustHeight(); | |
break; | |
case AdjustMode.Width: | |
AdjustWidth(); | |
break; | |
case AdjustMode.HeightAndWidth: | |
AdjustBoth(); | |
break; | |
} | |
} | |
void AdjustBoth() | |
{ | |
var x = Text.preferredWidth; | |
var y = Text.preferredHeight; | |
if (MaxWidth > 0f && x > MaxWidth) x = MaxWidth; | |
if (MinWidth > 0f && x < MinWidth) x = MinWidth; | |
if (MaxHeight > 0f && y > MaxHeight) y = MaxHeight; | |
if (MinHeight > 0f && y < MinHeight) y = MinHeight; | |
buffer.x = x; | |
buffer.y = y; | |
Text.rectTransform.sizeDelta = buffer; | |
} | |
void AdjustHeight() | |
{ | |
var x = Text.rectTransform.sizeDelta.x; | |
var y = Text.preferredHeight; | |
if (MaxWidth > 0f && x > MaxWidth) x = MaxWidth; | |
if (MinWidth > 0f && x < MinWidth) x = MinWidth; | |
if (MaxHeight > 0f && y > MaxHeight) y = MaxHeight; | |
if (MinHeight > 0f && y < MinHeight) y = MinHeight; | |
buffer.x = x; | |
buffer.y = y; | |
Text.rectTransform.sizeDelta = buffer; | |
} | |
void AdjustWidth() | |
{ | |
var x = Text.preferredWidth; | |
var y = Text.rectTransform.sizeDelta.y; | |
if (MaxWidth > 0f && x > MaxWidth) x = MaxWidth; | |
if (MinWidth > 0f && x < MinWidth) x = MinWidth; | |
if (MaxHeight > 0f && y > MaxHeight) y = MaxHeight; | |
if (MinHeight > 0f && y < MinHeight) y = MinHeight; | |
buffer.x = x; | |
buffer.y = y; | |
Text.rectTransform.sizeDelta = buffer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment