Here is a frame for a generic prompt or warning window
First create a warning Model class (without inheriting MonoBehaviour) WarningModel
- using UnityEngine;
- using System.Collections;
- //Declare a warning delegate method for running other programs while popping up the warning
- publicdelegatevoid WarningResult();
- publicclass WarningModel {
- //declare the method delegate
- public WarningResult result;
- //text to display
- publicstring value;
- // How long to delay to automatically close
- publicfloat delay;
- // alert model
- public WarningModel(string value,WarningResult result=null,float delay=-1)
- {
- this.value=value;
- this.result=result;
- this.delay=delay;
- }
- }
Then create a warning UI window, create a WarningWindow script and drag it to the window, and drag its own sub-object UILabel (NGUI) or Text (UGUI) to the inspector window, and then drag it into a prefab
- using System.Collections;
- publicclass WarningWindow : MonoBehaviour {
- [SerializeField]
- private UILabel text; //NGUI
- //private Text text; //UGUI
- //result method for receiving model
- WarningResult result;
- //Make the Window appear if there is a need to delay the disappearance, then disappear after the delay
- publicvoid active(WarningModel value)
- {
- text.text=value.value;
- this.result=value.result;
- //If WarningModel sets a delay time
- if(value.delay>0)
- {
- //Execute the close function after the delay time
- Invoke("close",value.delay);
- }
- gameObject.SetActive(true);
- }
- //Close the Window and run it if there is a method that needs to be run
- publicvoid close()
- {
- //Is the close function waiting to be called, obviously he has called it now to delete it
- if(IsInvoking("close"))
- {
- // cancel the call
- CancelInvoke("close");
- }
- gameObject.SetActive(false);
- // check if there is a function to execute
- if(result!=null)
- {
- result();
- }
- }
- }
Create a WarningManager script and mount it on an object that has always existed (eg: a camera)
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- publicclass WarningManager : MonoBehaviour {
- //Store a list of all warning Models
- publicstatic List<WarningModel> errors=new List<WarningModel> ();
- [SerializeField]
- private WarningWindow window; //Warning window
- void Update()
- {
- if(errors.Count>0)
- {
- // get the first one in the list
- WarningModel err=errors[0];
- //then delete
- errors.RemoveAt(0);
- //last display
- window.active(err);
- }
- }
- }
If you want to reuse it in other windows, drag the created prefab to the Canvas (UGUI) or UI Root (NGUI), then drag the WarningManager to an object that has always existed (eg: camera)
If you want to show a warning window, use it in your code
- WarningManager.errors.Add( new WarningModel( "Your gold coins are 0, congratulations, you have become a diaosi again!" ));