Author Topic: Dialog support  (Read 5881 times)

klumhru

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Dialog support
« on: May 29, 2012, 04:55:13 AM »
Hi,

I'm wondering if you plan to add some form of dialog support, i.e. rendering a game object or panel that can have a DialogResult like windows forms?

Regards,
Klumhru

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: Dialog support
« Reply #1 on: May 29, 2012, 05:28:20 AM »
It is not hard to do it yourself, but I would like to see an official solution.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dialog support
« Reply #2 on: May 29, 2012, 01:52:27 PM »
  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.                 UIEventListener.Get(okButton).onClick += OnButtonYes;
  40.                 UIEventListener.Get(yesButton).onClick += OnButtonYes;
  41.                 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.                 if (callback != null) callback(true);
  54.         }
  55.  
  56.         /// <summary>
  57.         /// "No" button press.
  58.         /// </summary>
  59.  
  60.         void OnButtonNo (GameObject go)
  61.         {
  62.                 mIsVisible = false;
  63.                 ActiveAnimation.Play(animation, null, Direction.Reverse, EnableCondition.DoNothing, DisableCondition.DisableAfterReverse);
  64.                 if (UIDimmer.instance != null) UIDimmer.instance.SetActive(false);
  65.                 if (callback != null) callback(false);
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Show a single-button dialog box.
  70.         /// </summary>
  71.  
  72.         public void Show (string titleText, string bodyText, string okText)
  73.         {
  74.                 if (!mIsVisible)
  75.                 {
  76.                         NGUITools.SetActive(gameObject, true);
  77.                         NGUITools.SetActive(yesButton, false);
  78.                         NGUITools.SetActive(noButton, false);
  79.                 }
  80.  
  81.                 title.text = titleText;
  82.                 body.text = bodyText;
  83.                 okButton.GetComponentInChildren<UILabel>().text = okText;
  84.                 UICamera.selectedObject = okButton;
  85.  
  86.                 if (!mIsVisible)
  87.                 {
  88.                         ActiveAnimation.Play(animation, null, Direction.Forward, EnableCondition.DoNothing, DisableCondition.DisableAfterReverse);
  89.                         if (UIDimmer.instance != null) UIDimmer.instance.SetActive(true);
  90.                         mIsVisible = true;
  91.                 }
  92.         }
  93.  
  94.         /// <summary>
  95.         /// Show a yes/no dialog box.
  96.         /// </summary>
  97.  
  98.         public void Show (string titleText, string bodyText, string yesText, string noText)
  99.         {
  100.                 mIsVisible = true;
  101.                 NGUITools.SetActive(gameObject, true);
  102.                 NGUITools.SetActive(okButton, false);
  103.  
  104.                 title.text = titleText;
  105.                 body.text = bodyText;
  106.  
  107.                 yesButton.GetComponentInChildren<UILabel>().text = yesText;
  108.                 noButton.GetComponentInChildren<UILabel>().text = noText;
  109.                 UICamera.selectedObject = yesButton;
  110.  
  111.                 ActiveAnimation.Play(animation, null, Direction.Forward, EnableCondition.DoNothing, DisableCondition.DisableAfterReverse);
  112.  
  113.                 if (UIDimmer.instance != null) UIDimmer.instance.SetActive(true);
  114.         }
  115. }

  1. using UnityEngine;
  2.  
  3. [AddComponentMenu("Game/UI/Message Box")]
  4. public class UIMessageBox : MonoBehaviour
  5. {
  6.         static UIMessageBox mInst;
  7.  
  8.         public GameObject prefab;
  9.  
  10.         GameObject mChild;
  11.         UIMessageBoxContainer mBox;
  12.  
  13.         /// <summary>
  14.         /// Convenience function that instantiates a new message box if one was not found.
  15.         /// </summary>
  16.  
  17.         UIMessageBoxContainer messageBox
  18.         {
  19.                 get
  20.                 {
  21.                         if (mBox == null && prefab != null)
  22.                         {
  23.                                 mChild = NGUITools.AddChild(gameObject, prefab);
  24.                                 mChild.transform.localPosition = new Vector3(0f, 1400f, 0f);
  25.  
  26.                                 mBox = mChild.GetComponent<UIMessageBoxContainer>();
  27.  
  28.                                 if (mBox == null)
  29.                                 {
  30.                                         Debug.LogError("No message box found", this);
  31.                                         Destroy(mChild);
  32.                                         prefab = null;
  33.                                 }
  34.                         }
  35.                         return mBox;
  36.                 }
  37.         }
  38.  
  39.         /// <summary>
  40.         /// Set the instance.
  41.         /// </summary>
  42.  
  43.         void Awake () { mInst = this; }
  44.  
  45.         /// <summary>
  46.         /// Show a single-button message box.
  47.         /// </summary>
  48.  
  49.         static public void Show (string title, string body, string ok, UIMessageBoxContainer.Callback callback)
  50.         {
  51.                 if (mInst != null)
  52.                 {
  53.                         UIMessageBoxContainer mb = mInst.messageBox;
  54.  
  55.                         if (mb != null)
  56.                         {
  57.                                 mb.callback = callback;
  58.                                 mb.Show(title, body, ok);
  59.                         }
  60.                 }
  61.         }
  62.  
  63.         /// <summary>
  64.         /// Show a yes/no message box.
  65.         /// </summary>
  66.  
  67.         static public void Show (string title, string body, string yes, string no, UIMessageBoxContainer.Callback callback)
  68.         {
  69.                 if (mInst != null)
  70.                 {
  71.                         UIMessageBoxContainer mb = mInst.messageBox;
  72.  
  73.                         if (mb != null)
  74.                         {
  75.                                 mb.callback = callback;
  76.                                 mb.Show(title, body, yes, no);
  77.                         }
  78.                 }
  79.         }
  80. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Dialog support
« Reply #3 on: May 29, 2012, 01:53:25 PM »
UIDimmer is what I use to dim the rest of the UI in Windward. It also has a collider covering the entire screen so the events don't go through.