1
0
Fork 0
Bildschirmflausch-LD41/Assets/Scripts/NotificationManager.cs

79 lines
1.8 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
2018-04-22 19:07:50 +02:00
public class NotificationManager : MonoBehaviour {
List<Notification> messages;
bool showingMessage;
float delay;
Text text;
class Notification {
string text;
float duration;
public Notification(string text, float duration) {
this.text = text;
this.duration = duration;
}
public string getText() {
return text;
2018-04-22 19:07:50 +02:00
}
public float getDuration() {
return duration;
}
}
// Use this for initialization
void Start() {
delay = 0;
messages = new List<Notification>();
text = GetComponent<Text>();
2018-04-22 20:10:05 +02:00
hide();
}
void Update() {
if ( showingMessage ) {
if ( Time.time > delay ) {
2018-04-22 20:10:05 +02:00
if (messages.Count <= 1) {
messages.Remove(messages[0]);
hide();
} else {
2018-04-22 20:10:05 +02:00
messages.Remove(messages[0]);
text.text = messages[0].getText();
delay = Time.time + messages[0].getDuration();
}
}
}
}
public void showMessage(string text, float duration) {
if ( showingMessage ) {
messages.Add(new Notification(text, duration));
} else {
showingMessage = true;
2018-04-22 19:07:50 +02:00
GetComponent<Text>().text = text;
delay = Time.time + duration;
2018-04-22 20:10:05 +02:00
show();
}
}
2018-04-22 20:10:05 +02:00
void show() {
GetComponentInParent<Image>().enabled = true;
text.enabled = true;
showingMessage = true;
}
void hide() {
GetComponentInParent<Image>().enabled = false;
text.enabled = false;
showingMessage = false;
}
}