Search Unity

Confirmation popup

Discussion in 'Game Foundation' started by Reelsy, Oct 16, 2020.

  1. Reelsy

    Reelsy

    Joined:
    Jan 12, 2020
    Posts:
    7
    Hi everyone,

    I wanted to know if there is a way to add a confirmation popup to prevent misclick on virtual transactions?

    Thank you in advance.

    Regards
     
  2. erika_d

    erika_d

    Joined:
    Jan 20, 2016
    Posts:
    413
    Hi @Reelsy,

    That's probably not possible with the current Transactions prefab and component, you could however use our TransactionView component as a jumping off point, copy and paste it into your own class and edit it so that it changes the onclick behavior to showing your own dialog game object, instead of immediately initiating the transaction. (You would also need to duplicate our PurchaseButton component and, if you're using our Store prefab, the StoreView component, but you shouldn't need to make any significant changes in those files, they just wouldn't be able to connect to your custom TransactionView component as is.) You can see this thread for an example of a user who customized our StoreView component: https://forum.unity.com/threads/putting-out-an-item-after-buying-from-store.932874/

    It's a good idea though, and I've made note of it on our jira board to look into expanding functionality in this way during future prefab improvement passes.
     
  3. Reelsy

    Reelsy

    Joined:
    Jan 12, 2020
    Posts:
    7
    Hi @erika_d,

    Thank you for your answer.
    I will dig into this solution.

    Regards
     
    erika_d likes this.
  4. erika_d

    erika_d

    Joined:
    Jan 20, 2016
    Posts:
    413
    Hi @Reelsy ,

    I wanted to let you know that we just released Game Foundation preview - 0.8.0 (a release forum post is forthcoming). The release includes a change to the Purchase Button prefab that exposes the onClick method subscription, so you could remove PurchaseButton.Purchase as an onClick listener, and instead put a method of your own that opens a confirmation dialog, and which itself calls PurchaseButton.Purchase. To get this new version of the PurchaseButton you'll have to go to re-import the prefabs via Window > Game Foundation > Import Prefabs and Samples

    As an example (i.e. unofficial and unsupported) here's the steps and code I used when testing the change:
    1. Create a simple ConfirmationDialog prefab (ideally with a purchase and cancel button)
    2. To your confirmationDialog prefab add the ConfirmationDialogManager, assign the purchase and cancel on click listeners to the methods in this component.
    3. To the PurchaseButton prefab add the ConfirmationDialogLauncher class, set your confirmationdialog prefab to the property in the launcher component.
    4. Add a store to your scene.
    5. Play scene, make a purchase by clicking PurchaseButton. Should see Confirmation dialog open, and when you select purchase, purchase should be initiated, and when you select cancel, it should close dialog and do nothing else.
    ConfirmationDialogManager:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.GameFoundation.Components;
    3.  
    4. public class ConfirmationDialogManager : MonoBehaviour
    5. {
    6.    public PurchaseButton currentPurchaseButton;
    7.  
    8.    public void StartPurchase()
    9.    {
    10.        if (currentPurchaseButton != null)
    11.        {
    12.            Debug.Log("Initiating purchase");
    13.            currentPurchaseButton.Purchase();
    14.        }
    15.        Destroy(gameObject);
    16.    }
    17.  
    18.    public void CancelPurchase()
    19.    {
    20.        Debug.Log("Cancelling purchase");
    21.        Destroy(gameObject);
    22.    }
    23. }
    ConfirmationDialogLauncher:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.GameFoundation.Components;
    3.  
    4. public class ConfirmationDialogLauncher : MonoBehaviour
    5. {
    6.    public ConfirmationDialogManager dialogBox;
    7.  
    8.    public void ShowDialog()
    9.    {
    10.        var currentPurchaseButton = gameObject.GetComponent<PurchaseButton>();
    11.        var confirmationDialogManager = Instantiate(dialogBox, FindObjectOfType<Canvas>().rootCanvas.transform);
    12.        confirmationDialogManager.currentPurchaseButton = currentPurchaseButton;
    13.        Debug.Log("showing confirmation dialog");
    14.    }
    15. }
     
    tony_c-unity3d likes this.