I’m currently working on making my own GUIManger type system. I’m trying to find a way to pop a notification up on screen and have it go away after five seconds. I have found some ways to do this such as using Invoke(), but I would really like to get it working with delegates and an IEnumerable as I feel like this would be a much cleaner way to deal with it. Here’s the code that I currently have. The only problem is that it currently doesn’t fire the notification function like it should be doing. onNotification() is triggered, but notification() is never ran. I’m pretty sure I’m misunderstand how something works because this topic is newer to me.
using UnityEngine;
using System.Collections;
using System;
public class GUIView : MonoBehaviour {
public Texture barTexture;
public Texture lightBarTexture;
public ObjectIneraction objectInteraction;
ArrayList notifications = new ArrayList();
public event EventHandler addNotification;
// Use this for initialization
void Awake () {
onNotification(this, null);
}
// Update is called once per frame
void Update () {
}
void OnGUI()
{
GUI.backgroundColor = Color.green;
GUI.color = Color.red;
GUI.DrawTexture(new Rect(10, 10, 200, 7), lightBarTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.DrawTexture(new Rect(10, 10, 200 * ((float)objectInteraction.momentum / (float)objectInteraction.momentumMax), 7), barTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.color = Color.green;
GUI.DrawTexture(new Rect(10, 20, 200, 7), lightBarTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.DrawTexture(new Rect(10, 20, 200 * ((float)objectInteraction.health / (float)objectInteraction.healthMax), 7), barTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.color = Color.blue;
GUI.DrawTexture(new Rect(10, 30, 200, 7), lightBarTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.DrawTexture(new Rect(10, 30, 200 * ((float)objectInteraction.connections / (float)objectInteraction.connectionsMax), 7), barTexture, ScaleMode.StretchToFill, true, 0.0f);
GUI.color = Color.green;
GameObject[] inventory = objectInteraction.inventory;
int currentItem = objectInteraction.inventorySlot;
int y = 100;
for (int i = 0; i < 6; i++)
{
string objName = "";
if (currentItem == i)
{
objName += ">>";
}
objName += ((Object)inventory*.GetComponent("Object")).name;*
if (currentItem == i)
{
objName += “<<”;
}
GUI.Label(new Rect(25, y, 250, 50), objName);
y += 25;
}
//GUI.Box(new Rect(Screen.width - 250, 10, 240, 50), “this is atest”);
y = 10;
foreach (string s in notifications)
{
GUI.Box(new Rect(Screen.width - 250, y, 240, 50), s);
y += 60;
}
}
public IEnumerable notification()
{
Debug.Log(“Notification active”);
notifications.Add(“test”);
yield return new WaitForSeconds(5);
notifications.RemoveAt(0);
}
public void onNotification(object sender, EventArgs e)
{
Debug.Log(“this is a test1”);
StartCoroutine(“notification”);
}
}