unity universal prompt window

Here is a frame for a generic prompt or warning window

First create a warning Model class (without inheriting MonoBehaviour) WarningModel 

  1. using UnityEngine;  
  2. using System.Collections;  
  3. //Declare a warning delegate method for running other programs while popping up the warning    
  4. publicdelegatevoid WarningResult();    
  5. publicclass WarningModel {   
  6. //declare the method delegate  
  7. public WarningResult result;  
  8. //text to display  
  9. publicstring value;   
  10. // How long to delay to automatically close  
  11. publicfloat delay;   
  12. // alert model  
  13. public WarningModel(string value,WarningResult result=null,float delay=-1)  
  14. {  
  15. this.value=value;  
  16. this.result=result;  
  17. this.delay=delay;  
  18. }  
  19. }  


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

  1. using System.Collections;  
  2.   
  3. publicclass WarningWindow : MonoBehaviour {   
  4. [SerializeField]  
  5. private UILabel text;    //NGUI  
  6. //private Text text;     //UGUI  
  7. //result method for receiving model  
  8. WarningResult result;  
  9. //Make the Window appear if there is a need to delay the disappearance, then disappear after the delay  
  10. publicvoid active(WarningModel value)   
  11. {  
  12. text.text=value.value;  
  13. this.result=value.result;  
  14. //If WarningModel sets a delay time  
  15. if(value.delay>0)  
  16. {  
  17. //Execute the close function after the delay time  
  18. Invoke("close",value.delay);  
  19. }  
  20. gameObject.SetActive(true);  
  21. }  
  22. //Close the Window and run it if there is a method that needs to be run  
  23. publicvoid close()   
  24. {  
  25. //Is the close function waiting to be called, obviously he has called it now to delete it  
  26. if(IsInvoking("close"))  
  27. {  
  28. // cancel the call  
  29. CancelInvoke("close");  
  30. }  
  31. gameObject.SetActive(false);  
  32. // check if there is a function to execute  
  33. if(result!=null)  
  34. {  
  35. result();  
  36. }  
  37. }  
  38. }  


Create a WarningManager script and mount it on an object that has always existed (eg: a camera)

  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. publicclass WarningManager : MonoBehaviour {   
  5. //Store a list of all warning Models  
  6. publicstatic List<WarningModel> errors=new List<WarningModel> ();   
  7.   
  8.   
  9. [SerializeField]  
  10. private  WarningWindow window;    //Warning window  
  11.   
  12.   
  13. void Update()  
  14. {  
  15. if(errors.Count>0)  
  16. {  
  17. // get the first one in the list  
  18. WarningModel err=errors[0];  
  19. //then delete  
  20. errors.RemoveAt(0);  
  21. //last display  
  22. window.active(err);  
  23. }  
  24. }  
  25. }  


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

  1. WarningManager.errors.Add( new  WarningModel( "Your gold coins are 0, congratulations, you have become a diaosi again!" )); 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326023394&siteId=291194637