Bnd10706
Bnd10706

Reputation: 2373

Unity UI Pop Up

I am new to Unity, and many of these principals I am just blanking on.

I build a dialogue system and its working great, but I want to make a confirmation box.

I have managed to get the confirmation box to pull up, but I cant seem to figure out how to wait for one of the Yes / No buttons are pushed before the code is returned to the Dialogue manager with the proper bool.

Right now the code pushes on behind the dialogue box. Which is expected.

I tried an enumerator but I must have been doing it wrong because it did not matter.

I just cleaned up the code to try to start from scratch.

public bool AskForTakingMoney(){
        takingMoneyBox.SetActive(true);
        goldText.text = "Current Gold: " + GameManager.instance.currentGold.ToString() + "G";
        questionText.text = "Pay the " + DialogManager.instance.goldAmount + "G?";
        //Wait here until yes or no button pushed
        return saysYes;
    }
    public void SaysYes(){
        saysYes = true;
        selectedAnswer = true;
        takingMoneyBox.SetActive(false);
    }
     public void SaysNo(){
        saysYes = false;
        selectedAnswer = true;
        takingMoneyBox.SetActive(false);

    }

I really just need the return function to not go back until the yes or no button is pushed.

I am at a complete loss.

Upvotes: 1

Views: 2073

Answers (3)

Immorality
Immorality

Reputation: 2174

If you want to show a popup, then the popup should be responsible for what happens next.

So your AskForTakingMoney should never return a value. Instead you need to assign the onclick events for the Yes and No button.

Your code should simply be:

public void AskForTakingMoney(){
        takingMoneyBox.SetActive(true);
        goldText.text = "Current Gold: " + GameManager.instance.currentGold.ToString() + "G";
        questionText.text = "Pay the " + DialogManager.instance.goldAmount + "G?";
    }

    public void SaysYes(){           
        takingMoneyBox.SetActive(false);
        // Your withdraw money code here.
    }

     public void SaysNo(){
        takingMoneyBox.SetActive(false);
    }

Then from within the Editor, click on your Yes Button. You should see an onclick field like the one in the picture. Click on the plus sign, then drag in your MonoBehaviour object which is holding the code with above functions. After dragging it in, You should be able to select SaysYes from the dropdown that appears under 'YourScriptName' -> 'SaysYes()'

Do the same for your No Button and you should be all set.

OnClick

Upvotes: 1

Laskio
Laskio

Reputation: 95

Make Two Buttons, Set them to active at the end of AskForTakingMoney(), Make those buttons lead to SaysYes(); and SaysNo();

Edit: Comment if you need clarification. I may not be understanding you clear enough. Do you even have buttons set up?

Upvotes: 0

Rishaan Gupta
Rishaan Gupta

Reputation: 563

You would either need the function to not care about returning what they clicked, and each frame check (in the update method, or in a coroutine) if the button has been pressed by checking your selectedAnswer variable.

Or you could run some code in a separate thread, and use WaitHandle's, to cause the thread to pause until the button has been pressed.

Example using your code:

using System.Threading;
using UnityEngine;

public class Manager : MonoBehaviour
{
    EventWaitHandle handle = new EventWaitHandle(false,EventResetMode.AutoReset);
    Thread _thread;
    public void StartThread()
    {
        _thread = new Thread(BackgroundThreadFunction);
        _thread.Start();
    }

    public void BackgroundThreadFunction()
    {
        if (AskForTakingMoney())
        {
            //Yes
        }
        else
        {
            //No
        }
    }

    public bool AskForTakingMoney()
    {
        takingMoneyBox.SetActive(true);
        goldText.text = "Current Gold: " + GameManager.instance.currentGold.ToString() + "G";
        questionText.text = "Pay the " + DialogManager.instance.goldAmount + "G?";

        //Wait here until yes or no button pushed
        handle.WaitOne();

        return saysYes;
    }

    public void SaysYes()
    {
        saysYes = true;
        selectedAnswer = true;
        takingMoneyBox.SetActive(false);

        handle.Set();
    }

    public void SaysNo()
    {
        saysYes = false;
        selectedAnswer = true;
        takingMoneyBox.SetActive(false);

        handle.Set();
    }
}

Upvotes: 0

Related Questions