using UnityEngine; using UnityEngine.Events; using UnityEngine.UI; using System.Collections.Generic; namespace UIWidgets { /// /// Dialog. /// [AddComponentMenu("UI/UIWidgets/Dialog")] public class Dialog : MonoBehaviour, ITemplatable { [SerializeField] Button defaultButton; /// /// Gets or sets the default button. /// /// The default button. public Button DefaultButton { get { return defaultButton; } set { defaultButton = value; } } [SerializeField] Text titleText; /// /// Gets or sets the text component. /// /// The text. public Text TitleText { get { return titleText; } set { titleText = value; } } [SerializeField] Text contentText; /// /// Gets or sets the text component. /// /// The text. public Text ContentText { get { return contentText; } set { contentText = value; } } [SerializeField] Image dialogIcon; /// /// Gets or sets the icon component. /// /// The icon. public Image Icon { get { return dialogIcon; } set { dialogIcon = value; } } DialogInfoBase dialogInfo; /// /// Gets the dialog info. /// /// The dialog info. public DialogInfoBase DialogInfo { get { if (dialogInfo==null) { dialogInfo = GetComponent(); } return dialogInfo; } } bool isTemplate = true; /// /// Gets a value indicating whether this instance is template. /// /// true if this instance is template; otherwise, false. public bool IsTemplate { get { return isTemplate; } set { isTemplate = value; } } /// /// Gets the name of the template. /// /// The name of the template. public string TemplateName { get; set; } static Templates templates; /// /// Dialog templates. /// public static Templates Templates { get { if (templates==null) { templates = new Templates(); } return templates; } set { templates = value; } } /// /// Awake is called when the script instance is being loaded. /// protected virtual void Awake() { if (IsTemplate) { gameObject.SetActive(false); } } /// /// This function is called when the MonoBehaviour will be destroyed. /// protected virtual void OnDestroy() { DeactivateButtons(); if (!IsTemplate) { templates = null; return ; } //if FindTemplates never called than TemplateName==null if (TemplateName!=null) { Templates.Delete(TemplateName); } } /// /// Return dialog instance by the specified template name. /// /// Template name. static public Dialog Template(string template) { return Templates.Instance(template); } /// /// Return Dialog instance using current instance as template. /// public Dialog Template() { if ((TemplateName!=null) && Templates.Exists(TemplateName)) { //do nothing } else if (!Templates.Exists(gameObject.name)) { Templates.Add(gameObject.name, this); } else if (Templates.Get(gameObject.name)!=this) { Templates.Add(gameObject.name, this); } return Templates.Instance(gameObject.name); } /// /// The modal key. /// protected int? ModalKey; /// /// Show dialog. /// /// Title. /// Message. /// Buttons. /// Set focus on button with specified name. /// Position. /// Icon. /// If set to true modal. /// Modal sprite. /// Modal color. /// Canvas. public virtual void Show(string title = null, string message = null, DialogActions buttons = null, string focusButton = null, Vector3? position = null, Sprite icon = null, bool modal = false, Sprite modalSprite = null, Color? modalColor = null, Canvas canvas = null) { if (position==null) { position = new Vector3(0, 0, 0); } SetInfo(title, message, icon); var parent = (canvas!=null) ? canvas.transform : Utilites.FindTopmostCanvas(gameObject.transform); if (parent!=null) { transform.SetParent(parent, false); } if (modal) { ModalKey = ModalHelper.Open(this, modalSprite, modalColor); } else { ModalKey = null; } transform.SetAsLastSibling(); transform.localPosition = (Vector3)position; gameObject.SetActive(true); CreateButtons(buttons, focusButton); } /// /// Sets the info. /// /// Title. /// Message. /// Icon. public virtual void SetInfo(string title=null, string message=null, Sprite icon=null) { if (DialogInfo!=null) { DialogInfo.SetInfo(title, message, icon); } else { if ((title!=null) && (TitleText!=null)) { TitleText.text = title; } if ((message!=null) && (ContentText!=null)) { ContentText.text = message; } if ((icon!=null) && (Icon!=null)) { Icon.sprite = icon; } } } /// /// Close dialog. /// public virtual void Hide() { if (ModalKey!=null) { ModalHelper.Close((int)ModalKey); } Return(); } /// /// The buttons cache. /// protected Stack