Clean up logic for popup.
This commit is contained in:
@@ -1,29 +1,33 @@
|
||||
using System;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Cryville.Crtr.UI {
|
||||
[RequireComponent(typeof(CanvasGroup))]
|
||||
public class Popup : MonoBehaviour {
|
||||
public string Message = "";
|
||||
CanvasGroup group;
|
||||
float timer = 0;
|
||||
CanvasGroup _group;
|
||||
[SerializeField]
|
||||
TextMeshProUGUI m_text;
|
||||
public string Message {
|
||||
get { return m_text.text; }
|
||||
set { m_text.text = value; }
|
||||
}
|
||||
|
||||
const float DURATION = 5.0f;
|
||||
const float DURIN = 0.4f;
|
||||
const float DUROUT = 0.4f;
|
||||
[SerializeField]
|
||||
AnimationCurve m_fadeCurve;
|
||||
float _timer;
|
||||
|
||||
void Start() {
|
||||
group = GetComponent<CanvasGroup>();
|
||||
group.alpha = 0;
|
||||
GetComponentInChildren<Text>().text = Message;
|
||||
transform.SetParent(GameObject.Find("PopupList").transform, false);
|
||||
void Awake() {
|
||||
_group = GetComponent<CanvasGroup>();
|
||||
_group.alpha = 0;
|
||||
}
|
||||
|
||||
void Update() {
|
||||
if (timer <= DURIN) group.alpha = timer / DURIN;
|
||||
else if (timer >= DURATION) GameObject.Destroy(gameObject);
|
||||
else if (timer >= DURATION - DUROUT) group.alpha = (DURATION - timer) / DUROUT;
|
||||
timer += Time.deltaTime;
|
||||
_timer += Time.deltaTime;
|
||||
_group.alpha = m_fadeCurve.Evaluate(_timer);
|
||||
if (_timer > m_fadeCurve[m_fadeCurve.length - 1].time) {
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
public static void CreateException(Exception ex) {
|
||||
@@ -31,7 +35,7 @@ namespace Cryville.Crtr.UI {
|
||||
}
|
||||
|
||||
public static void Create(string msg) {
|
||||
Instantiate(Resources.Load<GameObject>("Common/Popup")).GetComponent<Popup>().Message = msg;
|
||||
PopupManager.Instance.Create(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user