namespace UIWidgets { using UIWidgets.l10n; using UnityEngine; using UnityEngine.UI; /// /// Button instance. /// /// Type of the owner. /// Base type of the owner. /// Type of the button configuration. public class DialogButtonCustom where TOwner : class, IHideable, TOwnerBase where TButtonConfig : ButtonConfiguration { /// /// Button. /// public Button Button { get; private set; } /// /// Button template index. /// public int TemplateIndex { get { return Info.TemplateIndex; } } /// /// Button index. /// public int Index { get; protected set; } /// /// Owner. /// protected TOwner Owner; /// /// Button info component. /// public DialogButtonComponentBase ButtonComponent { get; protected set; } /// /// Button info. /// public TButtonConfig Info { get; protected set; } /// /// Button container. /// protected RectTransform Container; /// /// Initializes a new instance of the class. /// /// Owner. /// Button index. /// Button info. /// Template. /// Button container. public DialogButtonCustom(TOwner owner, int index, TButtonConfig info, Button template, RectTransform container) { Owner = owner; Index = index; Info = info; Container = container; CreateButton(template); } /// /// Create button. /// /// Template. protected void CreateButton(Button template) { Button = Compatibility.Instantiate(template); Button.transform.SetParent(Container != null ? Container : template.transform.parent, false); Button.onClick.AddListener(Click); ButtonComponent = Button.GetComponent(); UpdateButtonName(); } /// /// Change the button index and info. /// /// Button index. /// Button info. public void Change(int index, TButtonConfig info) { Index = index; Info = info; UpdateButtonName(); } /// /// Replace button with the new template. /// /// Template. public void Replace(Button template) { DestroyButton(); CreateButton(template); SetActive(true); } /// /// Update button name. /// public virtual void UpdateButtonName() { if (ButtonComponent != null) { ButtonComponent.SetButtonName(Localization.GetTranslation(Info.Label)); } } /// /// Set button gameObject state. /// /// State. public void SetActive(bool active) { if (Button == null) { return; } Button.gameObject.SetActive(active); if (active) { Button.transform.SetAsLastSibling(); } } /// /// Process click event. /// protected void Click() { if (Info.Process(Owner, Index)) { Owner.Hide(); } } /// /// Destroy button gameobject. /// public void Destroy() { DestroyButton(); Owner = null; } void DestroyButton() { if (Button != null) { Button.onClick.RemoveListener(Click); Object.Destroy(Button.gameObject); } } } }