using NPOI.HWPF.Model; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; /// /// UI内部类基类 /// public abstract class UIBaseItem { /// /// 根节点 /// protected GameObject GoRoot; /// /// 以缩放判定是否显示(缩放比SetActive性能好,优先用这个) /// public bool IsScaleShow { get { if (GoRoot.transform.localScale.x == 0f) return false; if (GoRoot.transform.localScale.y == 0f) return false; return true; } set { GoRoot.IsScaleShow(value); } } /// /// 以SetActivex显示隐藏 /// public bool IsActive { get { return GoRoot.activeSelf; } set { GoRoot.SetActive(value); } } /// /// 根据现有实例构造 /// /// /// public UIBaseItem(GameObject root, bool isScaleShow = false) { GoRoot = root; IsScaleShow = isScaleShow; } /// /// 根据现有实例克隆新的实例并构造 /// /// /// /// public UIBaseItem(GameObject root, int i, bool isScaleShow = false) { GoRoot = UnityEngine.Object.Instantiate(root, root.transform.parent); GoRoot.name = i.ToString(); IsScaleShow = isScaleShow; } #region 绑定方法 public GameObject BindButton(string target, Action action, bool playAudio = true) { GameObject obj = GoRoot.transform.Find(target)?.gameObject; if (obj != null) { if (obj.TryGetComponent(out Button button)) { button.onClick.AddListener( delegate () { if (playAudio) { //AudioManager.Instance.PlaySound(SfxNameConst.button_s); } action?.Invoke(); }); } } else DebugUtil.LogError("未找到 {0}/{1}", GoRoot.name, target); return obj; } /// /// 增加一个可绑定对应变量的点击事件 /// 传入GameObject /// /// /// /// /// public GameObject BindButton(GameObject obj, Action action, bool playAudio = true) { if (obj != null) { if (obj.TryGetComponent(out Button button)) { button.onClick.AddListener( delegate () { if (playAudio) { //AudioManager.Instance.PlaySound(SfxNameConst.button_s); } action?.Invoke(); }); } } else DebugUtil.LogError("未找到 {0}/{1}", GoRoot.name, obj.name); return obj; } /// /// 传入参数 /// /// /// /// /// /// /// public GameObject BindButton(GameObject obj, Action action, T parameter, bool playAudio = true) { if (obj != null) { if (obj.TryGetComponent(out Button button)) { button.onClick.AddListener( delegate () { if (playAudio) { //AudioManager.Instance.PlaySound(SfxNameConst.button_s); } action?.Invoke(parameter); }); } } else DebugUtil.LogError("未找到 {0}/{1}", GoRoot.name, obj.name); return obj; } /// /// 直接绑按钮 /// /// /// /// /// public GameObject BindButton(Button button, Action action, bool playAudio = true) { if (button != null) { //Button button = button.GetComponent