当前位置:网站首页>Implementing click on the 3D model in RenderTexture in Unity
Implementing click on the 3D model in RenderTexture in Unity
2022-07-31 15:22:00 【overgrown valley】
来自: https://blog.csdn.net/zhaojunkuan/article/details/113344129
重写RawImage,两种情况,一种是UI有专门的UI相机,一种是没有专门的UI相机
using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Lylibs
{
public class LyRawImage : RawImage, IPointerClickHandler
{
// 点击RawImage时,相对RawImage自身的坐标
private Vector2 ClickPosInRawImg;
// 预览映射相机
private Camera PreviewCamera;
private Camera UICamera;
private Canvas canvasa;
protected override void Start()
{
// 初始获取预览映射相机
if (PreviewCamera == null)
{
PreviewCamera = GameObject.Find("PreviewCamera").transform.GetComponent<Camera>();
}
if (UICamera == null)
{
UICamera = GameObject.Find("UICamera").transform.GetComponent<Camera>();
}
if (canvasa == null)
{
canvasa = GameObject.Find("Canvas").transform.GetComponent<Canvas>();
}
}
void IPointerClickHandler.OnPointerClick(PointerEventData eventData)
{
//GetRawImageObj(eventData, rectTransform, PreviewCamera);
CheckDrawRayLine(canvasa, eventData.position, this, PreviewCamera, UICamera);
}
#region UI不绑定相机
/// <summary>
/// 通过点击RawImage中映射的RenderTexture画面,对应的相机发射射线,得到物体
/// </summary>
/// <param name="data">rawimage点击的数据</param>
/// <param name="rawImgRectTransform">rawimage的recttransfotm</param>
/// <param name="previewCamera">生成rendertexture中画面的相机</param>
/// <returns>返回射线碰撞到的物体</returns>
private GameObject GetRawImageObj(PointerEventData data, RectTransform rawImgRectTransform, Camera previewCamera)
{
GameObject obj = null;
var pos = (data.position - (Vector2)rawImgRectTransform.position) / rawImgRectTransform.lossyScale - rawImgRectTransform.rect.position;
var rate = pos / rawImgRectTransform.rect.size;
var ray = previewCamera.ViewportPointToRay(rate);
RaycastHit raycastHit;
if (Physics.Raycast(ray, out raycastHit))
{
Debug.Log(raycastHit.transform.name);
obj = raycastHit.transform.gameObject;
}
return obj;
}
#endregion
#region UI有专门的UI相机
/// <summary>
/// 射线投射
/// </summary>
/// <param name="canvas">画布</param>
/// <param name="mousePosition">当前Canvas下点击的鼠标位置</param>
/// <param name="previewImage">预览图</param>
/// <param name="previewCamera">预览映射图的摄像机</param>
private void CheckDrawRayLine(Canvas canvas, Vector3 mousePosition, RawImage previewImage, Camera previewCamera, Camera UiCamera)
{
Vector2 ClickPosInRawImg;
// 将UI相机下点击的UI坐标转为相对RawImage的坐标
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(canvas.transform as RectTransform, mousePosition, UiCamera, out ClickPosInRawImg))
{
//获取预览图的长宽
float imageWidth = previewImage.rectTransform.rect.width;
float imageHeight = previewImage.rectTransform.rect.height;
//获取预览图的坐标,此处RawImage的Pivot需为(0,0),不然自己再换算下
float localPositionX = previewImage.rectTransform.localPosition.x;
float localPositionY = previewImage.rectTransform.localPosition.y;
//获取在预览映射相机viewport内的坐标(坐标比例)
float p_x = (ClickPosInRawImg.x - localPositionX) / imageWidth;
float p_y = (ClickPosInRawImg.y - localPositionY) / imageHeight;
//从视口坐标发射线
Ray p_ray = previewCamera.ViewportPointToRay(new Vector2(p_x, p_y));
RaycastHit p_hitInfo;
if (Physics.Raycast(p_ray, out p_hitInfo))
{
//显示射线,只有在scene视图中才能看到
Debug.DrawLine(p_ray.origin, p_hitInfo.point);
// Debug.Log(p_hitInfo.transform.name);
}
}
}
#endregion
}
}
边栏推荐
- R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(legend、修改可视化图像的图例在整图中的位置)
- TRACE32 - SNOOPer-based variable logging
- 工程水文学试卷
- Ubantu专题5:设置静态ip地址
- 为什么毕业季不要表白?
- How useful is four-quadrant time management?
- Internet banking stolen?This article tells you how to use online banking safely
- 【MySQL】Mysql范式及外键作用
- Gorm—Go语言数据库框架
- Ubantu专题4:xshell、xftp连接接虚拟机以及设置xshell复制粘贴快捷键
猜你喜欢
随机推荐
工程水文学试卷
模板与泛型编程值typelist实现
AVH部署实践 (一) | 在Arm虚拟硬件上部署飞桨模型
为什么毕业季不要表白?
R language ggplot2 visualization: use the ggboxplot function of the ggpubr package to visualize the box plot, use the font function to customize the font size, color, style (bold, italic) of the legen
mongo进入报错
TextBlock控件入门基础工具使用用法,取上法入门
R语言检验样本是否符合正态性(检验样本是否来自一个正态分布总体):shapiro.test函数检验样本是否符合正态分布(normality test)
「秋招系列」MySQL面试核心25问(附答案)
WPF project - basic usage of controls entry, you must know XAML
LeetCode二叉树系列——222.完全二叉树的节点个数
Unity中实现点选RenderTexture中的3D模型
Efficient use of RecyclerView Section 1
LeetCode二叉树系列——110.平衡二叉树
实现防抖与节流函数
Doing things software development - the importance of law and understanding of reasonable conclusions
Selenium自动化中无头浏览器的应用
ASP.NET Core 产生连续 Guid
OpenCV测量物体的尺寸技能 get~
浏览器自带的拾色器