using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; //Adapted from https://gist.github.com/mminer/975374 //TODO: Pool objects public class PopUpSystem : MonoBehaviour { public GameObject notificationButtonPrefab; public Transform notificationPanelTransform; [Tooltip("Adjust this value to delay the fade animation")] public float timeBeforeAnimationStarts = 3.0f; public Queue notificationQueue = new Queue(); private GameObject currentGo; public Color errorColor = Color.red; public Color warningColor = Color.yellow; public Color logColor = Color.black; Dictionary logTypeColors = new Dictionary() { { LogType.Assert, Color.white }, { LogType.Error, Color.red }, { LogType.Exception, Color.red }, { LogType.Log, Color.white }, { LogType.Warning, Color.yellow }, }; struct Log { public string message; public string stackTrace; public LogType type; } List logs = new List(); void OnEnable () { Application.logMessageReceived += HandleLog; notificationQueue.Clear(); } void OnDisable () { Application.logMessageReceived -= HandleLog; } /// /// Records a log from the log callback. /// /// Message. /// Trace of where the message came from. /// Type of message (error, exception, warning, assert). void HandleLog (string message, string stackTrace, LogType type) { logs.Add(new Log() { message = message, stackTrace = stackTrace, type = type, }); if(notificationPanelTransform != null && notificationButtonPrefab != null){ GameObject notif = GameObject.Instantiate(notificationButtonPrefab); notif.transform.SetParent(notificationPanelTransform); notif.transform.Find("Text").gameObject.GetComponent().text = message; Image logo = notif.transform.Find("Image").gameObject.GetComponent(); switch(type){ case LogType.Error: case LogType.Exception: logo.color = errorColor; break; case LogType.Warning: logo.color = warningColor; break; case LogType.Log: logo.color = logColor; break; default: logo.color = Color.black; break; } notificationQueue.Enqueue(notif); } } int cpt = 0; void Update(){ //Play a delayed animation once the previous gameObject is destroyed if(currentGo == null && notificationQueue.Count != 0){ currentGo = notificationQueue.Dequeue(); currentGo.GetComponent().delayedAnimation(timeBeforeAnimationStarts); } //Debuging if(cpt == 0){ Debug.LogWarning("My warning"); } if(cpt == 120){ Debug.LogError("My error"); } if(cpt == 180){ cpt = -1; Debug.Log("My Log"); } cpt++; } }