using UnityEngine.EventSystems;
private Button jump;
private EventTrigger trigger;
void Start()
{
jump = GameObject.Find("btn_Jump").GetComponent<Button>();
trigger = jump.gameObject.GetComponent<EventTrigger>();//(在按钮上添加EventTrigger组件)
//按下,抬起事件
EventTrigger.Entry down = new EventTrigger.Entry();//声明事件委托对象
down.eventID = EventTriggerType.PointerDown;委托对象的属性,按下类型(事件类型)
down.callback = new EventTrigger.TriggerEvent();//声明回调函数对象
down.callback.AddListener(JumpButtonDown);//要调用的方法
trigger.triggers.Add(down);将委托加入EvenSystem的事件委托链
EventTrigger.Entry undown = new EventTrigger.Entry();
undown.eventID = EventTriggerType.PointerUp;//抬起事件
undown.callback = new EventTrigger.TriggerEvent();
undown.callback.AddListener(JumpButtonUp);
trigger.triggers.Add(undown);
//光标移入,移出事件
/*EventTrigger.Entry downing = new EventTrigger.Entry();
downing.eventID = EventTriggerType.PointerEnter;
downing.callback = new EventTrigger.TriggerEvent();
downing.callback.AddListener(JumpButton);
trigger.triggers.Add(downing);
EventTrigger.Entry undowning = new EventTrigger.Entry();
undowning.eventID = EventTriggerType.PointerExit;
undowning.callback = new EventTrigger.TriggerEvent();
undowning.callback.AddListener(UnJumpButton);
trigger.triggers.Add(downing);*/
}
public void JumpButtonDown(BaseEventData pointDate)
{
print("按下跳跃键");
jumpPressed = true;//模拟ButtonDown
jumpHeld = true;
Invoke("SetBack", 0.1f);
}
public void JumpButtonUp(BaseEventData pointDate)
{
print("抬起跳跃键");
jumpHeld = false;
}
public void SetBack()
{
jumpPressed = false;
}