博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
unity中利用layer的方式来获取指定层级上的对象,例如UI层的某个对象
阅读量:4347 次
发布时间:2019-06-07

本文共 1456 字,大约阅读时间需要 4 分钟。

1.按下鼠标左键,检测鼠标点击到的UI对象

using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.EventSystems;public class CustomBookMouseController : MonoBehaviour {    public LayerMask m_layerMask = 1;    public float m_dis = 1000;    public Camera m_rayCamera;            // Update is called once per frame    void Update () {        if (Input.GetMouseButtonDown(0))        {            if (EventSystem.current.IsPointerOverGameObject())            {                PointerEventData pointerData = new PointerEventData(EventSystem.current);                pointerData.position = Input.mousePosition;                List
results = new List
(); EventSystem.current.RaycastAll(pointerData, results); for (int i = 0; i < results.Count; i++) { if (results[i].gameObject.layer==LayerMask.NameToLayer("BookUI")) { Debug.Log(results[i].gameObject.name); } Debug.Log(results[i].gameObject); } } } }}

2.物理射线的应用 需要在检测对象上添加碰撞器组件

var ray = m_rayCamera.ScreenPointToRay(Input.mousePosition);            RaycastHit hitInfo;            if (Physics.Raycast(ray, out hitInfo, m_dis, m_layerMask))            {                Debug.Log(hitInfo.collider.gameObject.name);            }

 

转载于:https://www.cnblogs.com/nanyang0310/p/9145057.html

你可能感兴趣的文章
哈希(1) hash的基本知识回顾
查看>>
dockerfile_nginx+PHP+mongo数据库_完美搭建
查看>>
Http协议的学习
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
Detours信息泄漏漏洞
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>
(简单)华为Nova青春 WAS-AL00的USB调试模式在哪里开启的流程
查看>>
图论知识,博客
查看>>
[原创]一篇无关技术的小日记(仅作暂存)
查看>>
20145303刘俊谦 Exp7 网络欺诈技术防范
查看>>