常用的功能自己经常忘记,所以记录下代码,以备忘记时能够查阅!!!
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;
public VoidDelegate onSub;
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);
}
public override void OnSubmit(BaseEventData eventData)
{
if (onSub != null) onSub(gameObject);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class EventTriggerListener : MonoBehaviour, IPointerClickHandler, IPointerDownHandler, IPointerEnterHandler,
IPointerExitHandler, IPointerUpHandler, ISelectHandler, IUpdateSelectedHandler, IBeginDragHandler, IDragHandler, IEndDragHandler
{
public delegate void VoidDelegate(GameObject go);
public VoidDelegate onClick;
public VoidDelegate onDoubleClick;
public VoidDelegate onDown;
public VoidDelegate onEnter;
public VoidDelegate onExit;
public VoidDelegate onUp;
public VoidDelegate onSelect;
public VoidDelegate onUpdateSelect;
public VoidDelegate onBeginDrag;
public VoidDelegate onEndDrag;
public VoidDelegate onDrag;
float t1;
float t2;
static public EventTriggerListener Get(GameObject go)
{
EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
if (listener == null) listener = go.AddComponent<EventTriggerListener>();
return listener;
}
public void OnPointerClick(PointerEventData eventData)
{
t2 = Time.realtimeSinceStartup;
if (t2 - t1 < 0.2)
{
if (onDoubleClick != null) onDoubleClick(gameObject);
}
else
{
if (onClick != null) onClick(gameObject);
}
t1 = t2;
//if (eventData.clickCount == 2)
//{
// if (onDoubleClick != null) onDoubleClick(gameObject);
//}
//else if (eventData.clickCount == 1)
//{
// if (onClick != null) onClick(gameObject);
//}
}
public void OnPointerDown(PointerEventData eventData)
{
if (onDown != null) onDown(gameObject);
}
public void OnPointerEnter(PointerEventData eventData)
{
if (onEnter != null) onEnter(gameObject);
}
public void OnPointerExit(PointerEventData eventData)
{
if (onExit != null) onExit(gameObject);
}
public void OnPointerUp(PointerEventData eventData)
{
if (onUp != null) onUp(gameObject);
}
public void OnSelect(BaseEventData eventData)
{
if (onSelect != null) onSelect(gameObject);
}
public void OnUpdateSelected(BaseEventData eventData)
{
if (onUpdateSelect != null) onUpdateSelect(gameObject);
}
public void OnBeginDrag(PointerEventData eventData)
{
if (onBeginDrag != null) onBeginDrag(gameObject);
}
public void OnEndDrag(PointerEventData eventData)
{
if (onEndDrag != null) onEndDrag(gameObject);
}
public void OnDrag(PointerEventData eventData)
{
if (onDrag != null) onDrag(gameObject);
}
}
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit, 1000f, LayerMask.GetMask("EquipmentInfo")))
{
Debug.Log("click object name is " + hit.collider.gameObject.name);
}
//射线检查
_ray = new Ray(m_eye.position, m_eye.forward);
Debug.DrawRay(m_eye.position, m_eye.forward * 2, Color.red);
if (Physics.Raycast(_ray, out _hit, 500f, 1 << LayerMask.NameToLayer("EyeCheck")))
{
if (m_IsDebug) Debug.DrawRay(m_eye.position, m_eye.forward * 2, Color.green);
Debug.Log(_hit.transform.name);
}