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

相关推荐
qiminixi4 小时前
Unity 6000下载
unity·unity 6000·unity 6000下载
CreasyChan8 小时前
Unity Shader 入门指南
unity·c#·游戏引擎·shader
漂视数字孪生世界9 小时前
Unity团结引擎的前世今生
unity·游戏引擎·数字孪生
心前阳光14 小时前
Unity通过ScriptableObject学习访问者模式
学习·unity·访问者模式
fcm1915 小时前
unity之重新导入TMP
unity
心疼你的一切15 小时前
【技术创作的璀璨盛宴——2025年CSDN博客之星总评选深度总结】
microsoft·unity·游戏引擎·游戏程序·csdn·博客之星
心前阳光16 小时前
Unity批量实例化UI后设置UI位置失效
unity
心前阳光16 小时前
Unity的VideoPlayer准备完成回调注意
unity·游戏引擎
地狱为王18 小时前
Unity使用sherpa-onnx实现关键词检测
unity·onnx·sherpa-onnx·关键词检测
猫不吃咸鱼20 小时前
Unity单手轮盘控制2D/3D物体移动
3d·unity·游戏引擎