Introducing the UI and ToastManager
This commit is contained in:
25
Assets/Scripts/UI/Intro.cs
Normal file
25
Assets/Scripts/UI/Intro.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
public class Intro : MonoBehaviour {
|
||||
|
||||
[SerializeField]
|
||||
private List<ToastMessage> messages;
|
||||
[SerializeField]
|
||||
private UIManager uiManager;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
foreach( ToastMessage msg in messages)
|
||||
{
|
||||
msg.SetUIManager(uiManager);
|
||||
uiManager.QueueMessage(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/UI/Intro.cs.meta
Normal file
11
Assets/Scripts/UI/Intro.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 1d982a8393eef5c4f8a1613371d7be41
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
11
Assets/Scripts/UI/Toast.cs
Normal file
11
Assets/Scripts/UI/Toast.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class Toast : MonoBehaviour {
|
||||
|
||||
public Text textElement;
|
||||
public Image iconElement;
|
||||
|
||||
}
|
||||
11
Assets/Scripts/UI/Toast.cs.meta
Normal file
11
Assets/Scripts/UI/Toast.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3f16aad2157fe0a41a06c13d367ceab7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
139
Assets/Scripts/UI/UIManager.cs
Normal file
139
Assets/Scripts/UI/UIManager.cs
Normal file
@@ -0,0 +1,139 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
[System.Serializable]
|
||||
public class ToastMessage
|
||||
{
|
||||
[SerializeField]
|
||||
private string messageText;
|
||||
[SerializeField]
|
||||
private float duration;
|
||||
private float timeLeft;
|
||||
[SerializeField]
|
||||
private Texture2D icon;
|
||||
[SerializeField]
|
||||
private GameObject toastPrefab;
|
||||
private GameObject toastElements;
|
||||
private UIManager mngr;
|
||||
|
||||
public void SetUIManager(UIManager _mngr)
|
||||
{
|
||||
mngr = _mngr;
|
||||
}
|
||||
|
||||
public float TimeTick()
|
||||
{
|
||||
return timeLeft = timeLeft - Time.deltaTime;
|
||||
}
|
||||
|
||||
public GameObject CreateToastElements()
|
||||
{
|
||||
GameObject toastContainer = GameObject.Instantiate(toastPrefab,toastPrefab.transform.position, Quaternion.identity);
|
||||
toastContainer.transform.SetParent(mngr.worldCanvas.transform);
|
||||
Toast toast = toastContainer.GetComponent<Toast>();
|
||||
toast.textElement.text = this.messageText;
|
||||
Debug.Log(mngr);
|
||||
icon = mngr.defaultIcon;
|
||||
toast.iconElement.sprite = Sprite.Create(icon, new Rect(0.0f, 0.0f, icon.width, icon.height), Vector2.one);
|
||||
toastContainer.SetActive(false);
|
||||
toastElements = toastContainer;
|
||||
return toastContainer;
|
||||
}
|
||||
|
||||
public void ShowToastElements()
|
||||
{
|
||||
if (toastElements != null)
|
||||
{
|
||||
toastElements.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogWarning("Trying to show ToastElements before calling CreateToastElements() does not work.");
|
||||
}
|
||||
}
|
||||
|
||||
public void DestroyToastElements()
|
||||
{
|
||||
if(toastElements != null)
|
||||
{
|
||||
GameObject.Destroy(toastElements);
|
||||
} else
|
||||
{
|
||||
Debug.LogWarning("Trying to destroy ToastElements before calling CreateToastElements() does not work.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class UIManager : MonoBehaviour {
|
||||
|
||||
public GameObject worldCanvas;
|
||||
public Texture2D defaultIcon;
|
||||
|
||||
|
||||
private List<ToastMessage> messageQueue = new List<ToastMessage>();
|
||||
private ToastMessage currentMessage;
|
||||
|
||||
// Use this for initialization
|
||||
void Start () {
|
||||
|
||||
}
|
||||
|
||||
private void DisplayMessage(ToastMessage msg)
|
||||
{
|
||||
msg.CreateToastElements();
|
||||
msg.ShowToastElements();
|
||||
}
|
||||
|
||||
private ToastMessage PopNextMessage()
|
||||
{
|
||||
if (messageQueue[0] != null)
|
||||
{
|
||||
ToastMessage next = messageQueue[0];
|
||||
messageQueue.RemoveAt(0);
|
||||
return next;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update () {
|
||||
if (messageQueue.Count == 0 && currentMessage == null)
|
||||
{
|
||||
return; //No messages
|
||||
}
|
||||
|
||||
|
||||
if (currentMessage == null)
|
||||
{
|
||||
DisplayMessage(PopNextMessage());
|
||||
}
|
||||
else
|
||||
{
|
||||
float messageTimeLeft = currentMessage.TimeTick();
|
||||
if (messageTimeLeft <= 0)
|
||||
{
|
||||
currentMessage.DestroyToastElements();
|
||||
currentMessage = PopNextMessage();
|
||||
DisplayMessage(currentMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void SetCurrentMessage(ToastMessage messageObject)
|
||||
{
|
||||
this.currentMessage = messageObject;
|
||||
}
|
||||
|
||||
public ToastMessage createMessageObject(string text, float duration, Texture2D icon)
|
||||
{
|
||||
return new ToastMessage();
|
||||
}
|
||||
|
||||
public int QueueMessage(ToastMessage msg)
|
||||
{
|
||||
messageQueue.Add(msg);
|
||||
return messageQueue.Count;
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Scripts/UI/UIManager.cs.meta
Normal file
11
Assets/Scripts/UI/UIManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: b51af86ed54a4504c866f4d54eceb129
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user