|
@@ -0,0 +1,120 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using UnityEngine;
|
|
|
+using UnityEngine.UI;
|
|
|
+
|
|
|
+public class ToggleHelp : MonoBehaviour
|
|
|
+{
|
|
|
+
|
|
|
+ #region tog
|
|
|
+ public class TogItem
|
|
|
+ {
|
|
|
+ public GameObject on;
|
|
|
+ public GameObject off;
|
|
|
+ public Button btn;
|
|
|
+ public int idx;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Action<int> funCallBack;
|
|
|
+ public int curChooseIdx;
|
|
|
+ public List<TogItem> listTogItem;
|
|
|
+
|
|
|
+
|
|
|
+ public Func<bool> funCantClick;
|
|
|
+
|
|
|
+ public Transform togTransform;
|
|
|
+ #endregion
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ #region tog
|
|
|
+
|
|
|
+ public void TogCreateBindEvent(Action<int, GameObject, GameObject> CreateFun, Action<int> fun, int count)
|
|
|
+ {
|
|
|
+ togTransform = GetComponent<Transform>();
|
|
|
+ var self = this;
|
|
|
+ var togTmp = self.togTransform.Find($"togTmp").transform;
|
|
|
+ togTmp.gameObject.SetActive(false);
|
|
|
+ for (int i = 0; i < count; i++)
|
|
|
+ {
|
|
|
+ TogItem tmp = new TogItem();
|
|
|
+ var tog = GameObject.Instantiate(togTmp, self.togTransform);
|
|
|
+ tog.gameObject.SetActive(true);
|
|
|
+ tmp.btn = tog.Find($"btn").GetComponent<Button>();
|
|
|
+ tmp.on = tog.Find($"on").gameObject;
|
|
|
+ tmp.off = tog.Find($"off").gameObject;
|
|
|
+ tmp.idx = i;
|
|
|
+ CreateFun?.Invoke(i, tmp.on, tmp.off);
|
|
|
+ tmp.btn.onClick.RemoveAllListeners();
|
|
|
+ tmp.btn.onClick.AddListener(() => { TogOnClick(tmp.idx); });
|
|
|
+
|
|
|
+ self.listTogItem.Add(tmp);
|
|
|
+ }
|
|
|
+
|
|
|
+ self.funCallBack = fun;
|
|
|
+ }
|
|
|
+ public void TogSetActive(bool isShow)
|
|
|
+ {
|
|
|
+ var self = this;
|
|
|
+ self.togTransform.gameObject.SetActive(isShow);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void TogOnClick(int idx)
|
|
|
+ {
|
|
|
+ var self = this;
|
|
|
+ if (self.funCantClick != null && self.funCantClick.Invoke()) return;
|
|
|
+ if (self.curChooseIdx != idx)
|
|
|
+ {
|
|
|
+ self.curChooseIdx = idx;
|
|
|
+
|
|
|
+ TogUpdateChooseUI(idx);
|
|
|
+ self.funCallBack?.Invoke(idx);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ public void TogSetCantClickFun(Func<bool> funCantClick)
|
|
|
+ {
|
|
|
+ var self = this;
|
|
|
+ self.funCantClick = funCantClick;
|
|
|
+ }
|
|
|
+ //如果 全部 重置为都不选中 idx 传入 -1
|
|
|
+ public void TogInitUI(int idx = 0)
|
|
|
+ {
|
|
|
+ var self = this;
|
|
|
+ self.funCantClick = null;
|
|
|
+
|
|
|
+ self.curChooseIdx = idx;
|
|
|
+ TogUpdateChooseUI(idx);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public int TogGetCurChooseIdx()
|
|
|
+ {
|
|
|
+ var self = this;
|
|
|
+ return self.curChooseIdx;
|
|
|
+ }
|
|
|
+ //
|
|
|
+ public void TogUpdateChooseUI(int idx)
|
|
|
+ {
|
|
|
+ var self = this;
|
|
|
+ for (int i = 0; i < self.listTogItem.Count; i++)
|
|
|
+ {
|
|
|
+
|
|
|
+ self.listTogItem[i].on.SetActive(i == idx);
|
|
|
+ self.listTogItem[i].off.SetActive(i != idx);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+
|
|
|
+}
|