using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace MPStudio
{
//Author: SkyAllen Email: 894982165@qq.com Website: www.ytn0.com
    public class TabImage : MonoBehaviour
    {
        public ToggleGroup tg;

        private int idx = 0;

        public List<Image> images;

        void Start()
        {
            Toggle[] arr = tg.GetComponentsInChildren<Toggle>();
            if (images.Count != arr.Length)
            {
                Debug.Log("Error len is not equal");
            }

            UpdateImg();

            // 为所有Toggle添加监听
            foreach (Toggle toggle in arr)
            {
                toggle.onValueChanged.AddListener((isOn) => OnToggleChanged(toggle, isOn));
            }
        }

        public void UpdateImg()
        {
            foreach (var img in images)
            {
                img.gameObject.SetActive(false);
            }

            images[idx].gameObject.SetActive(true);
        }

        private void OnToggleChanged(Toggle changedToggle, bool isOn)
        {
            if (isOn)
            {
                idx = changedToggle.transform.GetSiblingIndex();
                UpdateImg();
            }
        }

        void Update()
        {

        }
    }
}