using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Events; using UnityEngine.EventSystems; using TMPro; /* //Set properties in C# example codes. using RainbowArt.CleanFlatUI; public class ModalWindowRadioButtonDemo : MonoBehaviour { //The ModalWindowRadioButton Component. public ModalWindowRadioButton m_ModalWindow; //Create a List of new options. List mOptions = new List {"Option 1", "Option 2", "Option 3", "Option 4"}; void Start() { //Add OnConfirm event listener. m_ModalWindow.OnConfirm.AddListener(ModalWindowConfirm); //Add OnCancel event listener. m_ModalWindow.OnCancel.AddListener(ModalWindowCancel); //Set start index. m_ModalWindow.StartSelectedIndex = 1; //Clear the old options. m_ModalWindow.ClearOptions(); //Add the new options. m_ModalWindow.AddOptions(mOptions); //Show Modal Window. m_ModalWindow.ShowModalWindow(); } void ModalWindowConfirm(int index) { Debug.Log("ModalWindowConfirm, index:"+index); } void ModalWindowCancel(int index) { Debug.Log("ModalWindowCancel"); } } */ namespace RainbowArt.CleanFlatUI { public class ModalWindowRadioButton : MonoBehaviour { protected internal class ContentItem : MonoBehaviour { public TextMeshProUGUI itemText; public Image itemImage; public Image itemSelect; public Image itemCheckmark; public Button button; } [SerializeField] Image iconTitle; [SerializeField] TextMeshProUGUI title; [SerializeField] Button buttonClose; [SerializeField] Button buttonConfirm; [SerializeField] Button buttonCancel; [SerializeField] Animator animator; [SerializeField] RectTransform contentRect; [SerializeField] GameObject itemTemplate; [SerializeField] TextMeshProUGUI itemText; [SerializeField] Image itemImage; [SerializeField] Image itemSelect; [SerializeField] Image itemCheckmark; [SerializeField] RectOffset padding = new RectOffset(); [SerializeField] float spacing = 2; [SerializeField] int startSelectedIndex = 0; [Serializable] public class OptionItem { public string text; public Sprite icon; public OptionItem() { } public OptionItem(string newText) { text = newText; } public OptionItem(Sprite newImage) { icon = newImage; } public OptionItem(string newText, Sprite newImage) { text = newText; icon = newImage; } } [SerializeField] List options = new List(); List contentItems = new List(); [Serializable] public class ModalWindowEvent : UnityEvent{ } [SerializeField] ModalWindowEvent onConfirm = new ModalWindowEvent(); [SerializeField] ModalWindowEvent onCancel = new ModalWindowEvent(); int selectedIndex = 0; IEnumerator diableCoroutine; float disableTime = 0.5f; public int StartSelectedIndex { get => startSelectedIndex; set { startSelectedIndex = value; } } public int SelectedIndex { get => selectedIndex; set { if((value >= 0) && ( value < options.Count)) { selectedIndex = value; } else { selectedIndex = 0; } } } public string TitleValue { get { if(title != null) { return title.text; } return ""; } set { if(title != null) { title.text = value; } } } public Sprite IconValue { get { if(iconTitle != null) { return iconTitle.sprite; } return null; } set { if(iconTitle != null) { if(value != null) { iconTitle.gameObject.SetActive(true); iconTitle.sprite = value; } else { iconTitle.gameObject.SetActive(false); iconTitle.sprite = null; } } } } public ModalWindowEvent OnConfirm { get => onConfirm; set { onConfirm = value; } } public ModalWindowEvent OnCancel { get => onCancel; set { onCancel = value; } } public void ShowModalWindow() { gameObject.SetActive(true); UpdateSelectIndex(); InitButtons(); InitAnimation(); DestroyAllItems(); SetupTemplate(); CreateAllItems(options); PlayAnimation(true); } public void HideModalWindow() { PlayAnimation(false); if(animator != null) { if(diableCoroutine != null) { StopCoroutine(diableCoroutine); diableCoroutine = null; } diableCoroutine = DisableTransition(); StartCoroutine(diableCoroutine); } else { gameObject.SetActive(false); } } IEnumerator DisableTransition() { yield return new WaitForSeconds(disableTime); gameObject.SetActive(false); } void UpdateSelectIndex() { selectedIndex = 0; if ((startSelectedIndex >= 0) && (startSelectedIndex < options.Count)) { selectedIndex = startSelectedIndex; } } void InitButtons() { if(buttonClose != null) { buttonClose.onClick.RemoveAllListeners(); buttonClose.onClick.AddListener(OnCloseClick); } if(buttonConfirm != null) { buttonConfirm.onClick.RemoveAllListeners(); buttonConfirm.onClick.AddListener(OnConfirmClick); } if(buttonCancel != null) { buttonCancel.onClick.RemoveAllListeners(); buttonCancel.onClick.AddListener(OnCancelClick); } } void OnCloseClick() { OnCancelClick(); } void OnCancelClick() { HideModalWindow(); onCancel.Invoke(-1); } void OnConfirmClick() { HideModalWindow(); onConfirm.Invoke(selectedIndex); } void InitAnimation() { if(animator != null) { animator.enabled = false; animator.gameObject.transform.localScale = Vector3.one; animator.gameObject.transform.localEulerAngles = Vector3.zero; } } void PlayAnimation(bool bShow) { if(animator != null) { if(animator.enabled == false) { animator.enabled = true; } if(bShow) { animator.Play("In",0,0); } else { animator.Play("Out",0,0); } } } public void AddOptions(List optionList) { options.AddRange(optionList); } public void AddOptions(List optionList) { for (int i = 0; i < optionList.Count; i++) { options.Add(new OptionItem(optionList[i])); } } public void AddOptions(List optionList) { for (int i = 0; i < optionList.Count; i++) { options.Add(new OptionItem(optionList[i])); } } public void ClearOptions() { options.Clear(); } void SetupTemplate() { ContentItem contentItem = itemTemplate.GetComponent(); if (contentItem == null) { contentItem = itemTemplate.AddComponent(); contentItem.itemText = itemText; contentItem.itemImage = itemImage; contentItem.itemSelect = itemSelect; contentItem.itemCheckmark = itemCheckmark; contentItem.button = itemTemplate.GetComponent