You've already forked taptap2024_GJ_chidouren
init
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UI;
|
||||
using Object = UnityEngine.Object;
|
||||
|
||||
namespace IcecreamView
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class BindUIButton : Attribute
|
||||
{
|
||||
private string[] _eventPaths;
|
||||
private Button[] _buttons;
|
||||
|
||||
[Obsolete("不推荐使用地址绑定方式,该操作会使用Transform.Find, 推荐使用直接引用按钮字段的方式进行事件绑定")]
|
||||
public BindUIButton(params string[] buttonPaths)
|
||||
{
|
||||
this._eventPaths = buttonPaths;
|
||||
}
|
||||
|
||||
public BindUIButton(params Button[] buttons)
|
||||
{
|
||||
this._buttons = buttons;
|
||||
}
|
||||
|
||||
internal void AddListener(Transform obj, UnityAction action)
|
||||
{
|
||||
if (_eventPaths == null || _eventPaths.Length == 0)
|
||||
return;
|
||||
foreach (var path in this._eventPaths)
|
||||
{
|
||||
GetButtonComponent(obj , path)?.onClick.AddListener(action);
|
||||
}
|
||||
}
|
||||
|
||||
internal void AddListenerButton(UnityAction action)
|
||||
{
|
||||
if (_buttons == null || _buttons.Length == 0)
|
||||
return;
|
||||
foreach (var button in _buttons)
|
||||
{
|
||||
// ReSharper disable once Unity.PerformanceCriticalCodeNullComparison
|
||||
if (button != null)
|
||||
{
|
||||
button.onClick.AddListener(action);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Button GetButtonComponent(Transform obj, string path)
|
||||
{
|
||||
Transform mT;
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
mT = obj.Find(path);
|
||||
else
|
||||
mT = obj;
|
||||
var btn = mT?.GetComponent<Button>();
|
||||
if (mT == null)
|
||||
{
|
||||
Debug.LogError("[IceCreamView] 指定路径无效 : " + path, obj);
|
||||
}
|
||||
else if (btn == null)
|
||||
{
|
||||
Debug.LogError("[IceCreamView] 指定路径下没有Button组件: " + path, obj);
|
||||
}
|
||||
|
||||
return btn;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user