Firstborn/Assets/SWAN Dev/DynamicUI/DLabel.cs
Schaken-Mods 959e80cf72 assets upload
assets upload description.
2023-03-28 12:16:30 -05:00

176 lines
3.6 KiB
C#

/// <summary>
/// Created by SWAN Dev
/// </summary>
using UnityEngine;
using System;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Events;
/// <summary>
/// Dynamic UI Label.
/// (Last updated: 2018-10-11)
/// </summary>
public class DLabel : MonoBehaviour
{
[Header("[ Text ]")]
public Text title;
#if UNITY_EDITOR
//for checking label height at development runtime,
//check the boolean forceUpdate in inspector to update value.
public float m_LabelHeight = 0f;
public bool forceUpdate = false;
void Update()
{
if(forceUpdate)
{
forceUpdate = false;
m_LabelHeight = LabelHeight;
}
}
#endif
[Header("[ InputField - Basic ]")]
public bool m_IsInputField = false;
public int m_CharacterLimit = 0;
public InputField.ContentType m_ContentType = InputField.ContentType.Standard;
public bool m_ReadOnly = false;
[HideInInspector] public InputField m_InputField = null;
[Header("[ Number ]")]
public bool m_EnableValueLimit = false;
public float m_MinValue = 0;
public float m_MaxValue = 0;
[Header("[ Callback ]")]
public UnityEvent m_OnSetTextEvent;
public Action<DLabel> m_OnSetText;
void OnEnable()
{
if(title == null)
{
title = GetComponent<Text>();
}
_cachedText = title.text;
if(m_IsInputField)
{
if(m_InputField == null)
{
m_InputField = GetComponent<InputField>()? GetComponent<InputField>() : gameObject.AddComponent<InputField>();
}
m_InputField.textComponent = title;
m_InputField.onValueChanged.RemoveAllListeners();
m_InputField.onValueChanged.AddListener(delegate {
_OnInputFieldValueChange();
});
m_InputField.characterLimit = m_CharacterLimit;
m_InputField.contentType = m_ContentType;
m_InputField.readOnly = m_ReadOnly;
}
}
private void _OnInputFieldValueChange()
{
if(m_InputField) SetText(m_InputField.text);
}
private string _cachedText = "";
public void SetText(string text)
{
if(m_EnableValueLimit)
{
float f = 0f;
if(float.TryParse(text, out f))
{
if(f < m_MinValue || f > m_MaxValue)
{
f = Mathf.Clamp(f, m_MinValue, m_MaxValue);
}
}
text = f.ToString();
}
if(m_InputField == null)
{
title.text = text;
}
else
{
m_InputField.text = text;
}
_cachedText = text;
m_OnSetTextEvent.Invoke();
if (m_OnSetText != null)
{
m_OnSetText(this);
}
}
public string GetText(bool noUguiDelay = false)
{
return noUguiDelay ? _cachedText : title.text;
}
public int GetInt(bool noUguiDelay = false)
{
string tempText = noUguiDelay ? _cachedText : title.text;
int num = 0;
int.TryParse(tempText, out num);
return num;
}
public float GetFloat(bool noUguiDelay = false)
{
string tempText = noUguiDelay ? _cachedText : title.text;
float f = 0f;
float.TryParse(tempText, out f);
return f;
}
public void ClearText()
{
SetText("");
}
public void SetColor(Color color)
{
title.color = color;
}
public void UpdateRectSizeDeltaW(float padding = 0)
{
title.rectTransform.sizeDelta = new Vector2(LabelWidth + padding, title.rectTransform.sizeDelta.y);
}
public void UpdateRectSizeDeltaH(float padding = 0)
{
title.rectTransform.sizeDelta = new Vector2(title.rectTransform.sizeDelta.x, LabelHeight + padding);
}
public float LabelHeight
{
get{
// Update the canvases to get the updated rect of the text
Canvas.ForceUpdateCanvases();
return title.preferredHeight;
}
}
public float LabelWidth
{
get{
// Update the canvases to get the updated rect of the text
Canvas.ForceUpdateCanvases();
return title.preferredWidth;
}
}
}