Input

作者: 泱千澈 | 来源:发表于2019-06-08 13:08 被阅读0次

Q:我在场景中实例化一物体,并且该物体会对鼠标指针的进入和进出做出响应(例如,指针指向物体时改变颜色,移出时恢复颜色)。同时,我还需要对鼠标的按键状态进行响应,左键和右键产生不同的操作。这种需求,除了在Object脚本的Update里做判断外,还有没有更效率的实现方式?

A:可以借助IpointerClickHandler接口:
https://docs.unity3d.com/ScriptReference/EventSystems.IPointerClickHandler.html

using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.Events;

 public class RightClick : MonoBehaviour, IPointerClickHandler
 {

     public UnityEvent leftClick;
     public UnityEvent middleClick;
     public UnityEvent rightClick;

     public void OnPointerClick(PointerEventData eventData)
     {
         if (eventData.button == PointerEventData.InputButton.Left)
             leftClick.Invoke ();
         else if (eventData.button == PointerEventData.InputButton.Middle)
             middleClick.Invoke ();
         else if (eventData.button == PointerEventData.InputButton.Right)
             rightClick.Invoke ();
     }
 }

将以上脚本挂在Object之上即可。

相关文章

网友评论

      本文标题:Input

      本文链接:https://www.haomeiwen.com/subject/ztrixctx.html