59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class MessageBox : MonoBehaviour
|
|
{
|
|
public static MessageBox instance { get; private set;}
|
|
void Awake(){
|
|
if(instance != null){Destroy(gameObject);}
|
|
instance = this;
|
|
canvasGroup = GetComponent<CanvasGroup>();
|
|
|
|
closeButton.onClick.AddListener(()=>{SetActive(false);});
|
|
}
|
|
|
|
private CanvasGroup canvasGroup;
|
|
[SerializeField]private Text messageTxt;
|
|
[SerializeField]private Text titleTxt;
|
|
[SerializeField]private Button closeButton;
|
|
void Start()
|
|
{
|
|
DontDestroyOnLoad(gameObject);
|
|
SetActive(false);
|
|
}
|
|
|
|
void SetActive(bool value){
|
|
// StartCoroutine(setActive(value));
|
|
canvasGroup.alpha= value ? 1 : 0;
|
|
canvasGroup.blocksRaycasts = value;
|
|
canvasGroup.interactable = value;
|
|
}
|
|
private static string message;
|
|
public static void ShowMessage(string message,string title = "Notice"){
|
|
if(instance == null){Debug.LogError("Message was shown before message box was init");return;}
|
|
|
|
instance.showMessage(message,title);
|
|
}
|
|
|
|
public void showMessage(string _message, string title){
|
|
message = _message;
|
|
titleTxt.text = title;
|
|
StartCoroutine(_showMessage());
|
|
SetActive(true);
|
|
}
|
|
|
|
IEnumerator _showMessage(){
|
|
messageTxt.text = "";
|
|
closeButton.gameObject.SetActive(false);
|
|
for(int i=0; i < message.Length; i++){
|
|
messageTxt.text += message[i];
|
|
|
|
yield return new WaitForSeconds(0.01f);
|
|
}
|
|
closeButton.gameObject.SetActive(true);
|
|
|
|
}
|
|
}
|