Author Topic: generic yes/no popup  (Read 6567 times)

jmiller

  • Guest
generic yes/no popup
« on: November 25, 2012, 09:53:22 PM »
What is the best way to create a generic yes/no popup that can be called and can return yes or no to confirm something.  I would like to use this for buying powerups in my game to confirm there purchase.  Is there an example of this?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: generic yes/no popup
« Reply #1 on: November 26, 2012, 12:32:07 AM »
Create the UI layout for it and save it as a prefab. I also recommend creating a script to go with it that references the text on the buttons, title, and description so that you can easily change it.

jmiller

  • Guest
Re: generic yes/no popup
« Reply #2 on: November 26, 2012, 08:32:03 AM »
Ok.  So how would I link up this prefab to the calling script after it instantiates it so it will know what was selected?

jmiller

  • Guest
Re: generic yes/no popup
« Reply #3 on: November 26, 2012, 09:05:45 AM »
Could the prefab have a gameobject public variable that is assgined the object calling it and then in the yes button on click do a gameobject.SendMessage to execute a function on the calling object to perform the code?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: generic yes/no popup
« Reply #4 on: November 26, 2012, 04:16:05 PM »
Here is one of two scripts I used in Windward for this purpose:
  1. using UnityEngine;
  2. using AnimationOrTween;
  3.  
  4. /// <summary>
  5. /// Generic message box class capable of displaying variable text. Should not be used directly, but rather through some manager.
  6. /// </summary>
  7.  
  8. [AddComponentMenu("Game/UI/Message Box Container")]
  9. public class UIMessageBoxContainer : MonoBehaviour
  10. {
  11.         public UILabel title;
  12.         public UILabel body;
  13.         public GameObject okButton;
  14.         public GameObject yesButton;
  15.         public GameObject noButton;
  16.  
  17.         public delegate void Callback (bool response);
  18.  
  19.         /// <summary>
  20.         /// Callback function that will be invoked when the message box is closed.
  21.         /// </summary>
  22.  
  23.         public Callback callback;
  24.  
  25.         bool mIsVisible = false;
  26.  
  27.         /// <summary>
  28.         /// Whether the message box is currently visible.
  29.         /// </summary>
  30.  
  31.         public bool isVisible { get { return mIsVisible; } }
  32.  
  33.         /// <summary>
  34.         /// Register the listener callbacks.
  35.         /// </summary>
  36.  
  37.         void Awake ()
  38.         {
  39.                 if (okButton != null) UIEventListener.Get(okButton).onClick = OnButtonYes;
  40.                 if (yesButton != null) UIEventListener.Get(yesButton).onClick = OnButtonYes;
  41.                 if (noButton != null) UIEventListener.Get(noButton).onClick = OnButtonNo;
  42.         }
  43.  
  44.         /// <summary>
  45.         /// "Yes" or "OK" button press.
  46.         /// </summary>
  47.  
  48.         void OnButtonYes (GameObject go)
  49.         {
  50.                 mIsVisible = false;
  51.                 ActiveAnimation.Play(animation, null, Direction.Reverse, EnableCondition.DoNothing, DisableCondition.DisableAfterReverse);
  52.                 if (UIDimmer.instance != null) UIDimmer.instance.SetActive(false);
  53.                 UICamera.selectedObject = null;
  54.                 if (callback != null) callback(true);
  55.         }
  56.  
  57.         /// <summary>
  58.         /// "No" button press.
  59.         /// </summary>
  60.  
  61.         void OnButtonNo (GameObject go)
  62.         {
  63.                 mIsVisible = false;
  64.                 ActiveAnimation.Play(animation, null, Direction.Reverse, EnableCondition.DoNothing, DisableCondition.DisableAfterReverse);
  65.                 if (UIDimmer.instance != null) UIDimmer.instance.SetActive(false);
  66.                 UICamera.selectedObject = null;
  67.                 if (callback != null) callback(false);
  68.         }
  69.  
  70.         /// <summary>
  71.         /// Show a single-button dialog box.
  72.         /// </summary>
  73.  
  74.         public void Show (string titleText, string bodyText, string okText)
  75.         {
  76.                 if (!mIsVisible)
  77.                 {
  78.                         NGUITools.SetActive(gameObject, true);
  79.                         NGUITools.SetActive(yesButton, false);
  80.                         NGUITools.SetActive(noButton, false);
  81.                 }
  82.  
  83.                 title.text = titleText;
  84.                 body.text = bodyText;
  85.  
  86.                 NGUITools.SetActive(okButton, true);
  87.                 UILabel lbl = okButton.GetComponentInChildren<UILabel>();
  88.                 if (lbl != null) lbl.text = okText;
  89.                 UICamera.selectedObject = okButton;
  90.  
  91.                 if (!mIsVisible)
  92.                 {
  93.                         ActiveAnimation.Play(animation, null, Direction.Forward, EnableCondition.DoNothing, DisableCondition.DisableAfterReverse);
  94.                         if (UIDimmer.instance != null) UIDimmer.instance.SetActive(true);
  95.                         mIsVisible = true;
  96.                 }
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Show a yes/no dialog box.
  101.         /// </summary>
  102.  
  103.         public void Show (string titleText, string bodyText, string yesText, string noText)
  104.         {
  105.                 mIsVisible = true;
  106.                 NGUITools.SetActive(gameObject, true);
  107.                 NGUITools.SetActive(okButton, false);
  108.  
  109.                 title.text = titleText;
  110.                 body.text = bodyText;
  111.  
  112.                 yesButton.GetComponentInChildren<UILabel>().text = yesText;
  113.                 noButton.GetComponentInChildren<UILabel>().text = noText;
  114.                 UICamera.selectedObject = yesButton;
  115.  
  116.                 ActiveAnimation.Play(animation, null, Direction.Forward, EnableCondition.DoNothing, DisableCondition.DisableAfterReverse);
  117.  
  118.                 if (UIDimmer.instance != null) UIDimmer.instance.SetActive(true);
  119.         }
  120. }

jmiller

  • Guest
Re: generic yes/no popup
« Reply #5 on: November 26, 2012, 07:03:44 PM »
Thanks!  I appreciate the example.  I haven't used delegates before.

jmiller

  • Guest
Re: generic yes/no popup
« Reply #6 on: November 26, 2012, 11:40:59 PM »
What is UIDimmer?  Is it a singleton?  Can you provide the code for it?
« Last Edit: November 27, 2012, 12:13:01 AM by jmiller »

jmiller

  • Guest
Re: generic yes/no popup
« Reply #7 on: November 27, 2012, 12:18:20 PM »
Can anyone explain the callback part of this script?  Is like a pointer to a class method?  Is callback the method you want executed from the class that open the message box?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: generic yes/no popup
« Reply #8 on: November 27, 2012, 12:37:10 PM »
UIDimmer is what I use to dim the screen behind the popup (and block input). For your case you don't need it.

Yes, callback is what will be executed when the dialog box's result is known (some button gets clicked).

jmiller

  • Guest
Re: generic yes/no popup
« Reply #9 on: November 28, 2012, 08:19:28 PM »
Actually I could use the UIDimmer.  Seems simple enough to make.  I appreciate your awesome support.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: generic yes/no popup
« Reply #10 on: November 29, 2012, 07:46:26 AM »
I could post it... but there is also UIDimmerOnClick which activates it when a button is clicked... and the more of these useful classes I reveal, the more code I'd expose from Windward -- something I may end up putting on the Asset Store in some form eventually.

jmiller

  • Guest
Re: generic yes/no popup
« Reply #11 on: November 29, 2012, 12:40:38 PM »
I understand.  Releasing your GUI code on the Unity store would be a great idea and could help sell NGUI even more.  I would be buyer.  I prefer focusing on gameplay and not gui stuff so if I could buy mostly what I need that would be great.