using UnityEngine; using UnityEngine.UI; namespace UIWidgets { /// /// Popup. /// [AddComponentMenu("UI/UIWidgets/Popup")] public class Popup : MonoBehaviour, ITemplatable { [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 icon; /// /// Gets or sets the icon component. /// /// The icon. public Image Icon { get { return icon; } set { icon = value; } } DialogInfoBase info; /// /// Gets the info component. /// /// The info component. public DialogInfoBase Info { get { if (info==null) { info = GetComponent(); } return info; } } 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; /// /// Popup 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() { if (!IsTemplate) { templates = null; return ; } //if FindTemplates never called than TemplateName==null if (TemplateName!=null) { Templates.Delete(TemplateName); } } /// /// Return popup instance by the specified template name. /// /// Template name. static public Popup Template(string template) { return Templates.Instance(template); } /// /// Return popup instance using current instance as template. /// public Popup 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 popup. /// /// Title. /// Message. /// Position. /// Icon. /// If set to true modal. /// Modal sprite. /// Modal color. /// Canvas. public virtual void Show(string title = null, string message = 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, Close); } else { ModalKey = null; } transform.SetAsLastSibling(); transform.localPosition = (Vector3)position; gameObject.SetActive(true); } /// /// Sets the info. /// /// Title. /// Message. /// Icon. public virtual void SetInfo(string title=null, string message=null, Sprite icon=null) { if (Info!=null) { Info.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 popup. /// public virtual void Close() { if (ModalKey!=null) { ModalHelper.Close((int)ModalKey); } Return(); } /// /// Return this instance to cache. /// protected virtual void Return() { Templates.ToCache(this); ResetParametres(); } /// /// Resets the parametres. /// protected virtual void ResetParametres() { var template = Templates.Get(TemplateName); SetInfo(template.TitleText.text, template.ContentText.text, template.Icon.sprite); } } }