NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/UI/UIBaseItem.cs

293 lines
7.9 KiB
C#
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using NPOI.HWPF.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// UI内部类基类
/// </summary>
public abstract class UIBaseItem
{
/// <summary>
/// 根节点
/// </summary>
protected GameObject GoRoot;
/// <summary>
/// 以缩放判定是否显示(缩放比SetActive性能好,优先用这个)
/// </summary>
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);
}
}
/// <summary>
/// 以SetActivex显示隐藏
/// </summary>
public bool IsActive
{
get { return GoRoot.activeSelf; }
set { GoRoot.SetActive(value); }
}
/// <summary>
/// 根据现有实例构造
/// </summary>
/// <param name="root"></param>
/// <param name="isScaleShow"></param>
public UIBaseItem(GameObject root, bool isScaleShow = false)
{
GoRoot = root;
IsScaleShow = isScaleShow;
}
/// <summary>
/// 根据现有实例克隆新的实例并构造
/// </summary>
/// <param name="root"></param>
/// <param name="i"></param>
/// <param name="isScaleShow"></param>
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;
}
/// <summary>
/// 增加一个可绑定对应变量的点击事件
/// 传入GameObject
/// </summary>
/// <param name="obj"></param>
/// <param name="action"></param>
/// <param name="playAudio"></param>
/// <returns></returns>
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;
}
/// <summary>
/// 传入参数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="action"></param>
/// <param name="parameter"></param>
/// <param name="playAudio"></param>
/// <returns></returns>
public GameObject BindButton<T>(GameObject obj, Action<T> 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;
}
/// <summary>
/// 直接绑按钮
/// </summary>
/// <param name="button"></param>
/// <param name="action"></param>
/// <param name="playAudio"></param>
/// <returns></returns>
public GameObject BindButton(Button button, Action action, bool playAudio = true)
{
if (button != null)
{
//Button button = button.GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener
(
delegate ()
{
if (playAudio)
{
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
}
action?.Invoke();
}
);
}
}
else
{
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!!!");
//DebugUtil.LogError($"未找到 {gameObject.name}/{button.name}");
return null;
}
return button.gameObject;
}
/// <summary>
/// 可传入参数
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="button"></param>
/// <param name="action"></param>
/// <param name="parameter"></param>
/// <param name="playAudio"></param>
/// <returns></returns>
public GameObject BindButton<T>(Button button, Action<T> action, T parameter, bool playAudio = true)
{
if (button != null)
{
//Button button = obj.GetComponent<Button>();
if (button != null)
{
button.onClick.AddListener
(
delegate ()
{
if (playAudio)
{
//AudioManager.Instance.PlaySound(SfxNameConst.button_s);
}
if (parameter == null)
{
DebugUtil.LogError("=== 传入参数为空! ===");
}
action?.Invoke(parameter);
}
);
}
}
else
{
DebugUtil.LogError("传入按钮为空,请检查按钮绑定参数!!!");
return null;
}
return button.gameObject;
}
public void ClearBind(Button button)
{
if (button != null)
{
button.onClick.RemoveAllListeners();
}
}
public void ClearBind(GameObject go)
{
if (go.TryGetComponent(out Button button))
button.onClick.RemoveAllListeners();
}
public GameObject FindObj(string path)
{
GameObject obj = GoRoot.transform.Find(path)?.gameObject;
if (obj == null)
{
DebugUtil.LogError("未找到 {0}/{1}", GoRoot.name, path);
}
return obj;
}
public T GetComponent<T>(string path = "") where T : Behaviour
{
T component = GoRoot.transform.Find(path)?.GetComponent<T>();
if (component == null)
{
DebugUtil.LogError("未找到组件{0} {1}", typeof(T).Name, path);
}
return component;
}
#endregion
}
public static class GameObjectExtension
{
/// <summary>
/// 修改缩放的方式显示隐藏
/// </summary>
/// <param name="go"></param>
/// <param name="isShow"></param>
public static void IsScaleShow(this GameObject go, bool isShow)
{
if (isShow)
go.transform.localScale = Vector3.one;
else
go.transform.localScale = Vector3.zero;
}
}