射线检测
//初始化一条向前的射线
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插件
}