using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace Kill.UI.Components
{
public class MutiSelectPage : MonoBehaviour
{
///
/// 选项背景颜色 0:未选中 1:已选中
///
public Color[] bgColors;
public Color[] textColors;
public List options = new List();
int nowSelectedIndex = -1;
public Action onConfirm;
public Button confirmBtn;
public Button cancelBtn;
public void Init(int selectedIndex, Action onConfirm)
{
confirmBtn.onClick.AddListener(() =>
{
Confirm();
});
cancelBtn.onClick.AddListener(() =>
{
Cancel();
});
this.onConfirm = onConfirm;
nowSelectedIndex = selectedIndex;
for (int i = 0; i < options.Count; i++)
{
int index = i;
options[i].onClick.RemoveAllListeners();
options[i].onClick.AddListener(() =>
{
OnClickOption(index);
});
if (i == selectedIndex)
{
options[i].GetComponentInChildren().color = textColors[1];
options[i].GetComponent().color = bgColors[1];
}
else
{
options[i].GetComponentInChildren().color = textColors[0];
options[i].GetComponent().color = bgColors[0];
}
}
}
public void OnClickOption(int index)
{
if (nowSelectedIndex == index)
{
return;
}
else
{
nowSelectedIndex = index;
}
for (int i = 0; i < options.Count; i++)
{
if (i == nowSelectedIndex)
{
options[i].GetComponentInChildren().color = textColors[1];
options[i].GetComponent().color = bgColors[1];
}
else
{
options[i].GetComponentInChildren().color = textColors[0];
options[i].GetComponent().color = bgColors[0];
}
}
}
public void Confirm()
{
onConfirm?.Invoke(nowSelectedIndex);
Destroy(gameObject);
}
public void Cancel()
{
Destroy(gameObject);
}
}
}