Unity学习笔记——基础常用API(3)

射线检测

        //初始化一条向前的射线
        Ray ray = new Ray(transform.position, transform.forward);
        //检测射线是否碰到物体
        //bool isCollider = Physics.Raycast(ray);

        //射线检测  检测前方1米
        //bool isCollider = Physics.Raycast(ray, 1);

        //检测射线碰撞到的物体
        RaycastHit hit;//存储射线碰撞到的物体信息
        //获取射线碰撞到的物体
        //bool isCollider = Physics.Raycast(ray, out hit);
        //指定发生碰撞的物体  参数 射线、射线距离,指定碰撞物体层名
        bool isCollider = Physics.Raycast(ray, Mathf.Infinity,LayerMask.GetMask("eenmy1","enemy2"));

通过代码获取监听事件

btnGameObject.GetComponent<Button>().onClick.AddListener(this.ButtonOnClick);

鼠标点击事件的接口

//命名空间
using UnityEngine.EventSystems;
//接口名称
IPointerDownHandler,
IPointerClickHandler,
IPointerUpHandler,
IPointerEnterHandler,
IPointerExitHandler

鼠标拖拽事件的接口

//命名空间
using UnityEngine.EventSystems;
//拖拽接口名称
IBeginDragHandler,
IDragHandler,
IEndDragHandler,
IDropHandler

鼠标事件汇总


    IPointerEnterHandler - OnPointerEnter - Called when a pointer enters the object
    IPointerExitHandler - OnPointerExit - Called when a pointer exits the object
    IPointerDownHandler - OnPointerDown - Called when a pointer is pressed on the object
    IPointerUpHandler - OnPointerUp - Called when a pointer is released (called on the GameObject that the pointer is clicking)
    IPointerClickHandler - OnPointerClick - Called when a pointer is pressed and released on the same object
    IInitializePotentialDragHandler - OnInitializePotentialDrag - Called when a drag target is found, can be used to initialise values
    IBeginDragHandler - OnBeginDrag - Called on the drag object when dragging is about to begin
    IDragHandler - OnDrag - Called on the drag object when a drag is happening
    IEndDragHandler - OnEndDrag - Called on the drag object when a drag finishes
    IDropHandler - OnDrop - Called on the object where a drag finishes
    IScrollHandler - OnScroll - Called when a mouse wheel scrolls
    IUpdateSelectedHandler - OnUpdateSelected - Called on the selected object each tick
    ISelectHandler - OnSelect - Called when the object becomes the selected object
    IDeselectHandler - OnDeselect - Called on the selected object becomes deselected
    IMoveHandler - OnMove - Called when a move event occurs (left, right, up, down, ect)
    ISubmitHandler - OnSubmit - Called when the submit button is pressed
    ICancelHandler - OnCancel - Called when the cancel button is pressed

Touch触摸事件

        if (Input.touches.Length > 0)
        {
            Touch touch = Input.touches[0];
            //touch.position;//触摸的位置
            TouchPhase phase = touch.phase;//手指的状态 按下 移动 离开等

            //推荐easyTouch插件
        }

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值