using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger
{
public delegate void VoidDelegate(GameObject go);
public VoidDelegate onClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
static public EventTriggerListener Get(GameObject go)
{
EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
if (listener == null) listener = go.AddComponent<EventTriggerListener>();
return listener;
}
public override void OnPointerClick(PointerEventData eventData)
{
if (onClick != null) onClick(gameObject);
}
public override void OnPointerDown(PointerEventData eventData)
{
if (onDown != null) onDown(gameObject);
}
public override void OnPointerEnter(PointerEventData eventData)
{
if (onEnter != null) onEnter(gameObject);
}
public override void OnPointerExit(PointerEventData eventData)
{
if (onExit != null) onExit(gameObject);
}
public override void OnPointerUp(PointerEventData eventData)
{
if (onUp != null) onUp(gameObject);
}
public override void OnSelect(BaseEventData eventData)
{
if (onSelect != null) onSelect(gameObject);
}
public override void OnUpdateSelected(BaseEventData eventData)
{
if (onUpdateSelect != null) onUpdateSelect(gameObject);
}
}
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class ButtonOnClick : MonoBehaviour
{
private Transform trans;
private Button[] but;
private Transform hk;
public Action ClickButton;
void Awake()
{
trans = transform.FindChild("PropertyPanel/Content/FighterProperty/FighterGrid");
hk = GameObject.Find("SelectedImage").transform;
but = trans.GetComponentsInChildren<Button>();
}
/// <summary>
/// 初始化绑定事件
/// </summary>
void Start()
{
foreach (var button1 in but)
{
EventTriggerListener.Get(button1.gameObject).onClick = OnButtonClick;
}
}
/// <summary>
/// 监听事件
/// </summary>
/// <param name="buttonGO"></param>
private void OnButtonClick(GameObject buttonGO)
{
//在这里监听按钮的点击事件
Debug.LogError(buttonGO.gameObject.name);
hk.transform.parent = buttonGO.transform;
hk.transform.localPosition = Vector3.zero;
}
}