WinterJamSnowman/Assets/Scripts/LemonGenericLib/UI/QuickUIManagers/TextUIManager.cs

112 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using TMPro;
namespace Lemon.GenericLib.UI
{
public class TextUIManager : MonoBehaviour
{
public static TextUIManager Instance;
//singleton pattern
private void Awake()
{
if (Instance == null)
{
Instance = this;
}
if (Instance != this)
{
Destroy(gameObject);
}
InitTextUI();
}
public void InitTextUI()
{
for (int i = 0; i < textUIList.Length; i++)
{
textUIList[i].InitUI();
}
}
[SerializeField] TextUI[] textUIList;
[Serializable]
struct TextUI
{
public string UIName;
public TextMeshProUGUI textMesh;
public bool textGoUp;
public float textGoUpDistance;
public float textEffectTime;
public LeanTweenType easeType;
[SerializeField] private Vector3 originalPos;
private bool canAnimate;
[SerializeField] private bool explixitDefineStartY;
[SerializeField] Vector3 startPos;
public void SetText(string text)
{
textMesh.text = text;
if (textGoUp && canAnimate) LeanTween.moveLocalY(textMesh.gameObject, originalPos.y + textGoUpDistance, textEffectTime).setOnComplete(AnimateEffectBack);
}
public void SetColour(Color colour)
{
textMesh.color = colour;
}
public void InitUI()
{
if (explixitDefineStartY) originalPos = startPos;
else originalPos = textMesh.GetComponent<RectTransform>().localPosition;
canAnimate = true;
}
private void AnimateEffectBack()
{
LeanTween.moveLocalY(textMesh.gameObject, originalPos.y, textEffectTime);
}
}
public bool SetText(string UIName, string text)
{
for (int i = 0; i < textUIList.Length; i++)
{
if (UIName == textUIList[i].UIName)
{
textUIList[i].SetText(text);
return true;
}
}
Debug.LogError("text UI of nane " + UIName + " does not exist");
return false;
}
public bool SetColour(string UIName, Color colour)
{
for (int i = 0; i < textUIList.Length; i++)
{
if (UIName == textUIList[i].UIName)
{
textUIList[i].SetColour(colour);
return true;
}
}
Debug.LogError("text UI of nane " + UIName + " does not exist");
return false;
}
}
}