#我从RawImage.texture中利用Texture2D截取了一张图片,但是我现在想要根据矩形在这张图片的正中间,截取一张100*100的图片,目前各种方法都试过了,chat gpt也问过了,怎么都实现不了,我在想是不是我的思路出现了问题。
#劳烦各位给个解决方案。或者代码我借鉴借鉴。

unity中截取RawImage.texture图片的问题
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
- 小赵爱吃肉 2023-05-26 14:28关注
// 获取 RawImage 控件 RawImage rawImage = GetComponent<RawImage>(); // 获取 RawImage 的 Texture2D Texture2D texture2D = rawImage.texture as Texture2D; // 计算正中间100*100像素的左下角坐标 int startX = (texture2D.width / 2) - 50; int startY = (texture2D.height / 2) - 50; // 创建新的Texture2D,指定宽度、高度和像素格式 Texture2D newTexture = new Texture2D(100, 100, TextureFormat.RGBA32, false); // 循环遍历原始Texture2D中的像素,将正中间100*100像素复制到新的Texture2D中 for (int x = startX; x < startX + 100; x++) { for (int y = startY; y < startY + 100; y++) { Color color = texture2D.GetPixel(x, y); newTexture.SetPixel(x - startX, y - startY, color); } } // 应用更改并生成新的mipmap newTexture.Apply(true);
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决评论 打赏 举报无用 1