1.2D对象响应点击

(1)Canvas对象上有Graphic Raycaster

(2)被点击的对象加上Image,并勾选Raycast Target

(3)场景里有唯一对象挂上EventSystem和StandaloneInputModule

(4)被点击的对象加上Button, 然后调用:

        GetComponent<Button>().onClick.AddListener(() =>{Debug.Log("button click");});

2.3D对象响应点击

(1)看3d对象的camera上挂上Physics Raycaster

(2)被点击对象上加上Box Collider

(3)场景里有唯一对象挂上EventSystem和StandaloneInputModule

(4)挂上点击脚本,内容如下:

public class SceneObjClick : EventTrigger {public override void OnPointerClick(PointerEventData eventData){base.OnPointerClick(eventData);Debug.Log("Scene Obj Click");}public override void OnDrag(PointerEventData eventData){base.OnDrag(eventData);Debug.Log("Scene Obj Drag");}
}

其中EventTrigger.cs是unity自身的脚本,内如如下:

namespace UnityEngine.EventSystems
{//// 摘要://     /////     Receives events from the EventSystem and calls registered functions for each//     event.//     ///[AddComponentMenu("Event/Event Trigger")]public class EventTrigger : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler, IPointerClickHandler, IInitializePotentialDragHandler, IBeginDragHandler, IDragHandler, IEndDragHandler, IDropHandler, IScrollHandler, IUpdateSelectedHandler, ISelectHandler, IDeselectHandler, IMoveHandler, ISubmitHandler, ICancelHandler, IEventSystemHandler{//// 摘要://     /////     All the functions registered in this EventTrigger (deprecated).//     ///[Obsolete("Please use triggers instead (UnityUpgradable) -> triggers", true)]public List<Entry> delegates;protected EventTrigger();//// 摘要://     /////     All the functions registered in this EventTrigger.//     ///public List<Entry> triggers { get; set; }//// 摘要://     /////     Called before a drag is started.//     /////// 参数://   eventData://     Current event data.public virtual void OnBeginDrag(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when a Cancel event occurs.//     /////// 参数://   eventData://     Current event data.public virtual void OnCancel(BaseEventData eventData);//// 摘要://     /////     Called by the EventSystem when a new object is being selected.//     /////// 参数://   eventData://     Current event data.public virtual void OnDeselect(BaseEventData eventData);//// 摘要://     /////     Called by the EventSystem every time the pointer is moved during dragging.//     /////// 参数://   eventData://     Current event data.public virtual void OnDrag(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when an object accepts a drop.//     /////// 参数://   eventData://     Current event data.public virtual void OnDrop(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem once dragging ends.//     /////// 参数://   eventData://     Current event data.public virtual void OnEndDrag(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when a drag has been found, but before it is valid//     to begin the drag.//     /////// 参数://   eventData://     Current event data.public virtual void OnInitializePotentialDrag(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when a Move event occurs.//     /////// 参数://   eventData://     Current event data.public virtual void OnMove(AxisEventData eventData);//// 摘要://     /////     Called by the EventSystem when a Click event occurs.//     /////// 参数://   eventData://     Current event data.public virtual void OnPointerClick(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when a PointerDown event occurs.//     /////// 参数://   eventData://     Current event data.public virtual void OnPointerDown(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when the pointer enters the object associated with//     this EventTrigger.//     /////// 参数://   eventData://     Current event data.public virtual void OnPointerEnter(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when the pointer exits the object associated with this//     EventTrigger.//     /////// 参数://   eventData://     Current event data.public virtual void OnPointerExit(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when a PointerUp event occurs.//     /////// 参数://   eventData://     Current event data.public virtual void OnPointerUp(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when a Scroll event occurs.//     /////// 参数://   eventData://     Current event data.public virtual void OnScroll(PointerEventData eventData);//// 摘要://     /////     Called by the EventSystem when a Select event occurs.//     /////// 参数://   eventData://     Current event data.public virtual void OnSelect(BaseEventData eventData);//// 摘要://     /////     Called by the EventSystem when a Submit event occurs.//     /////// 参数://   eventData://     Current event data.public virtual void OnSubmit(BaseEventData eventData);//// 摘要://     /////     Called by the EventSystem when the object associated with this EventTrigger is//     updated.//     /////// 参数://   eventData://     Current event data.public virtual void OnUpdateSelected(BaseEventData eventData);//// 摘要://     /////     An Entry in the EventSystem delegates list.//     ///public class Entry{//// 摘要://     /////     The desired TriggerEvent to be Invoked.//     ///public TriggerEvent callback;//// 摘要://     /////     What type of event is the associated callback listening for.//     ///public EventTriggerType eventID;public Entry();}//// 摘要://     /////     UnityEvent class for Triggers.//     ///public class TriggerEvent : UnityEvent<BaseEventData>{public TriggerEvent();}}
}

3.点击原理

底层调用Physics.RaycastAll,从摄像机发出一条射线,拿到所有碰撞到的物体,进行排序,获取第一个碰撞物体,然后获取该物体上得事件函数,进行调用。事件函数有EventTrigger继承的那些接口。

Unity 2D和3D对象的点击相关推荐

  1. 《Unity 2D与3D手机游戏开发实战》简介

    #好书推荐##好书奇遇季#<Unity 2D与3D手机游戏开发实战>,京东当当天猫都有发售.彩色印制,定价89元,网店打折销售更便宜.本书配套源码.PPT课件,适合Unity游戏开发初学者 ...

  2. Unity开发2 3D对象的简单操作

    一.游戏3D对象 1.立方体 可修改长宽至长方体 2.球体 可修改至椭圆球 3.胶囊 可用作人身体 4.圆柱 5.平面 6.四边形 二.平面与四边形的区别 所有对象都是由一个或许多个三角形拼凑而成的, ...

  3. 【学堂上新】Unity UGUI 渲染 3D 对象详解

    UI界面中3D 对象的渲染,是游戏制作开发中的常见应用,看似简单,却隐藏许多的问题,比如渲染颜色的混合.遮盖关系.区域的裁剪.相机后效的处理--本文从两种常见的渲染方案入手,对常见问题进行分析,给出合 ...

  4. 《Unity 2D与3D手机游戏开发实战》上架了。

    新书上架了. 这本书主要是Unity开发的入门,附带了一个简单的2D例子,一个3D RPG的简单例子和一个尽可能用插件实现的射击游戏的例子. 书很薄,不过因为是彩页印刷,价钱不是那么实惠.不过说实话, ...

  5. Unity 2D及3D物体随鼠标转向的方法(类似孤胆枪手效果)

    首先明确效果,如孤胆枪手那样任务随鼠标方向转向的效果 要实现此效果我们首先要知道鼠标的位置 话不多说直接上代码 Vector3 point = Input.mousePosition;//注意,此时的 ...

  6. unity怎么实现人脸追踪_Unity 2019.2 beta为AR增加面部追踪、2D图像追踪、3D对象追踪等功能...

    Unity今天正式放出了Unity 2019.2 beta.对于这个版本,Unity集成了热门的Polybrush工具,添加了Unity Distribution Platform,同时扩展了用于XR ...

  7. Unity制作出《超级马里奥》的2D和3D混合效果

    现在来做点别的东西.Nintendo Switch上刚推出的<超级马里奥>中,有一些关卡混合了2D和3D的画面,这种效果十分让人印象深刻.如何在Unity中实现这个效果呢? 正常情况下,摄 ...

  8. T4M插件放入unity后怎么找不到_Unity制作人专场 | 闪耀暖暖从2D到3D的创作与进化之路...

    2019年,Unity在北京.成都.深圳分别举办了三场制作人专场活动,搭建优秀游戏制作人与Unity官方面对面交流的桥梁,内容涵盖项目优化.技术美术与解决方案等多个方面. 本文为大家带来的是叠纸游戏美 ...

  9. C#和Unity游戏开发者大师班2021 (2D,3D和FPS)

    流派:电子学习| MP4 |视频:h264,1280×720 |音频:AAC,44.1 KHz 语言:英语+中英文字幕(根据原英文字幕机译更准确) |大小:11.1 GB |时长:22h 31m 含项 ...

最新文章

  1. 为什么 MySQL的常用引擎都默认使用 B+ 树作为索引?
  2. Loj#6434「PKUSC2018」主斗地(搜索)
  3. 树视图主要属性 c# 0207
  4. sublime text3插件TrailingSpaces无法使用的解决方法
  5. Kafka常见面试问题
  6. js调用后台,后台调用前台等方法总结
  7. FastDFS的配置文件tracker.conf、storage.conf、client.conf 讲解
  8. HTML5与传统HTML的区别
  9. 苹果手机清灰_手机听筒喇叭声音越来越小?来试试这三个小技巧
  10. ECharts 全新大版本 4.0 正式发布!百度数据可视化实验室成立
  11. 网站监控服务都包括哪些具体内容?
  12. 更适合运动的耳机,设计时尚轻巧好用,南卡Runner CC3上手
  13. 微信小程序开发笔记1——使用npm脚本实现自动化切换环境配置
  14. 论创业者的幸存者偏差
  15. Django企业开发实战--by胡阳,学习记录1015
  16. pc调试微信h5页面提示Pending authentication:please accept debugging session on the device的解决方法
  17. C# 监控笔记本/平板的充电/电源状态
  18. 什么是前端渲染和后端渲染和SPA页面
  19. 在Qt中如何用纯代码写一个本地视频播放器
  20. 如何在Mac上停止使用iCloud照片库?

热门文章

  1. FastDFS启动报错
  2. kibana常见启动报错
  3. mac更新之后vmware fusion下vmware tools报错
  4. Linux中,Mysql安装
  5. js控制按钮n秒后可用
  6. 【Linux】tee命令
  7. 解决 安装或卸载软件时报错Error 1001 的问题
  8. React—Native开发之 Could not connect to development server(Android)解决方法
  9. C# asp.net页面通过URL参数传值中文乱码问题解决办法
  10. 【SpringBoot】拦截器使用@Autowired注入接口为null解决方法