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

相关推荐
为你写首诗ge15 小时前
【Unity知识分享】Mirror实现房间等待功能(创建房间 / 搜索房间、加入房间、房间准备、房间内角色设置、返回房间)
unity·mirror·房间等待功能
游乐码15 小时前
Unity坦克案例疑难记录(二)
unity·游戏引擎
小白学鸿蒙16 小时前
Funplay Unity MCP 接入 trae 实战
unity·游戏引擎·mcp
游乐码18 小时前
Unity基础(一)游戏中的数学Mathf函数
游戏·unity·游戏引擎
地狱为王1 天前
Unity实现猫脸关键点检测
unity·游戏引擎·猫脸关键点检测
598866753@qq.com2 天前
Unity Job System笔记
unity
winlife_2 天前
Funplay Unity MCP 与 Unity AI Assistant 详细对比:开源 MCP 工具集 vs 官方全栈 AI 产品
人工智能·unity·开源·ai编程·claude·mcp
御水流红叶2 天前
Android-Unity游戏逆向思路
android·游戏·unity
ellis19702 天前
Unity图集Atlas
unity
想不明白的过度思考者2 天前
Unity全局事件中心与新版输入架构实现练习——上帝模式与英雄模式的输入系统映射切换
java·unity·架构