Clean up logic for popup.

This commit is contained in:
2023-11-23 17:08:46 +08:00
parent 155ce0bb22
commit e9d0f4ce1a
9 changed files with 417 additions and 248 deletions

View File

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

View File

@@ -0,0 +1,25 @@
using System;
using UnityEngine;
using UnityEngine.UI;
namespace Cryville.Crtr.UI {
public class PopupManager : MonoBehaviour {
static PopupManager s_instance;
public static PopupManager Instance { get { return s_instance; } }
[SerializeField]
GameObject m_popupPrefab;
void Awake() {
if (s_instance != null) {
Destroy(gameObject);
throw new InvalidOperationException("Attempted to initialize a singleton twice.");
}
s_instance = this;
}
public void Create(string msg) {
Instantiate(m_popupPrefab, transform, false).GetComponent<Popup>().Message = msg;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0163e060f642c57498b7c58073d19e3e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: