Unity 获取鼠标点击位置物体贴图颜色

实现

复制代码
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
    textureCoord = hit.textureCoord;
    textureCoord.x *= textureMat.width;
    textureCoord.y *= textureMat.height;
    textureColor = textureMat.GetPixel(Mathf.FloorToInt(textureCoord.x), Mathf.FloorToInt(textureCoord.y));
}

*贴图和物体的Read/Write Enabled 都需处于开启状态,贴图的Use Crunch Compression 需处于关闭状态。

效果

*右侧为世界中的3d 物体,左侧为Canvas 上的Image

相关推荐
mxwin20 小时前
Unity Shader 深度写入与关闭ZWrite Off · 半透明排序 · 粒子穿插
unity·游戏引擎·shader
张老师带你学21 小时前
宇宙飞船完整Unity项目
科技·游戏·unity·游戏引擎·模型
mxwin21 小时前
Unity URP 下的流体模拟 深入解析 Navier-Stokes 方程与浅水方程的数学原理
unity·游戏引擎
mxwin1 天前
Unity Shader 深度重建世界坐标
unity·游戏引擎·shader
雪儿waii1 天前
Unity 中继承(父类子类)用法详解
unity·游戏引擎
总写bug的程序员1 天前
用 AI 蒸馏球员的思维操作系统:qiuyuan-skill 技术解析
人工智能·unity·游戏引擎
mxwin1 天前
Unity Shader 预乘 Alpha 完全指南 解决半透明纹理边缘黑边问题,让你的 UI 渲染更干净
unity·游戏引擎
mxwin1 天前
Unity URP 软粒子(Soft Particles)完全指南
unity·游戏引擎·shader
mxwin1 天前
Unity Shader 深度偏移Depth Bias / Offset 完全指南
unity·游戏引擎·shader
星河耀银海1 天前
Unity基础:UI组件详解:Button按钮的点击事件绑定
ui·unity·lucene