Question by
tharinduwika · May 09, 2021 at 06:19 AM ·
button trigger eventswait
How to wait for a button response
I want to make a model for a message box. This message box contains 2 texts and two buttons (ok & cancel). This message box is called from another script. Here I call messageBox method which return boolion. But this message box should wait until the button press. How do I do this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MessageBoxScript : MonoBehaviour
{
public float duration = 0.5f;
private bool btnClicked ;
private bool result;
public GameObject panel;
private void Start()
{
btnClicked = true;
}
public bool messageBox(string heading, string body)
{
var canvasGroup = GetComponent<CanvasGroup>();
StartCoroutine(DoFade(canvasGroup, canvasGroup.alpha, 1));
StartCoroutine(waitForResult());
return result;
}
private IEnumerator DoFade(CanvasGroup canvasGroup, float start, float end)
{
float counter = 0f;
while (counter < duration)
{
counter += Time.deltaTime;
canvasGroup.alpha = Mathf.Lerp(start, end, counter / duration);
yield return null;
}
}
private IEnumerator waitForResult()
{
if (btnClicked == false)
{
yield return result;
}
}
public void onOKBtnClick()
{
Debug.Log("Button clisked " + btnClicked + "");
btnClicked = false;
result = true;
}
public void onClosebtnClick()
{
Debug.Log("Button clisked " + btnClicked + "");
btnClicked = false;
result = false;
}
}
Comment