Waiting for User Response for Pop-up window.
Ok, so this is a bit complex.
I am trying to complete a pup-up manager that I can use to process ALL of my pop-up messages that I could incur, and include the potential for user response at the same time. Here is what I have right now. This is the full code to my pop-up manager, and I am trying to develop a callback method for the user responses. I may be going about this all wrong, and I am hoping that someone will be able to help me out. I can't find anything on this particular subject, so I am struggling a bit with this, and I may be attempting to use callbacks for something that they can't really be used for. Here is the Pop-Up Manager code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System;
public class PopUpManager : MonoBehaviour{
public void NewInterPopUp(string titleString, string messageString, Action<WaitForUserResponse> callback)
{
StartCoroutine(ShowNewInterPopUp(titleString, messageString));
}
public IEnumerator ShowNewPopUp(string titleString, string messageString)
{
//Do stuff.
}
public IEnumerator ShowNewInterPopUp(string titleString, string messageString)
{
//Do Stuff.
}
public void HidePopUp()
{
//Do Stuff.
}
public void Yes()
{
WaitForUserResponse response;
response = new WaitForUserResponse(true);
}
public void No()
{
WaitForUserResponse response;
response = new WaitForUserResponse(false);
}
public IEnumerator AnimateText(string strComplete)
{
\\Do Stuff
}
}
public class WaitForUserResponse : ProcessResponse
{
public bool userResponse;
public WaitForUserResponse(bool response)
{
userResponse = response;
}
public bool response
{
get
{
return userResponse;
}
set
{
userResponse = value;
}
}
}
public interface ProcessResponse
{
bool response
{
get;
set;
}
}
Here is where I am generating a Pop-Up from another script.
public void LoginToServices()
{
Social.localUser.Authenticate((bool success) =>
{
if (success)
{
OpenSavedGame("SavedGame");
}
else
{
PopUpManager.instance.NewInterPopUp("Error!", "Google Play services could not login, try again?", OnLoginFailed);
}
});
}
public void OnLoginFailed(WaitForUserResponse ans)
{
if (ans.response == true)
{
LoginToServices();
}
else
{
PopUpManager.instance.ShowNewPopUp("", "Using local data. Next time you are connected cloud data will update with local progress.");
}
}
I have a yes button and a no button that correspond to;
public void Yes()
{
WaitForUserResponse response;
response = new WaitForUserResponse(true);
}
public void No()
{
WaitForUserResponse response;
response = new WaitForUserResponse(false);
}
In the Pop-Up Manager code. This however does not seem to send the callback to the function where I am calling for the callback.
What am I doing wrong here?
Answer by Spykill · Jul 30, 2016 at 11:12 PM
In your Yes and No methods, you have a LOCAL variable called "responses". You then instantiate that local variable to be "new WaitForUserResponse(" and either true or false depending on whether you pressed Yes or No. But then you don't do anything with this local variable. What you need to do is call the Action callback, which you have in your NewInterPopUp. What you could do is store this callback as a variable in PopUpManager, and then in your Yes and No function do "callback(response);". Unfortunately this means you can only have one pop up at a time, since the callback variable will be overwritten. There are ways to have multiple pop ups at once, but that depends on how you implement your pop up. So for the callback variable:
public class PopUpManager : MonoBehaviour{
Action<WaitForUserResponse> callback;
For setting it:
public void NewInterPopUp(string titleString, string messageString, Action<WaitForUserResponse> callback)
{
this.callback = callback;
And for calling it (this one applies to both Yes and No, just change up the method name and the bool):
public void Yes()
{
WaitForUserResponse response;
response = new WaitForUserResponse(true);
callback(response);
}
Good luck!
Thank you for your help. I've been working on this for so long I had blinders on, and didn't even see that.
Thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220612085850im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Custom Editor - Lauch callback at the end of the SerializeField. 0 Answers
Callback when vuforia AR camera finishes loading 0 Answers
UI Text is not being rebuilt/updated in edit mode, when changing text from OnInspectorGUI 1 Answer
How to call a non-static method from another class without using an instance 1 Answer
What is a Callback? 1 Answer