Unity3d-Texture2D.ReadPixels

新的版本出来了,带来了一些新的变化

其中关于Texture2D.ReadPixels有点特殊说明

在移动平台上,该函数被推迟到本frame最后时完成

如果有Apply的话也推迟到最后完成

要使用的话使用corotine

 

On mobiles, if you read pixels from screen, the call will be deferred to the end of the frame. If you read pixels from the screen and call Apply right after it - apply will be deferred too. If you want to read from the screen and do something with the result you need to use coroutine.

*************************************************************************************************************

按照实例代码改写了,竟然还是有意外情况,这个要研究下

在PC测试没问题,问题出在使用了投影仪,有时间测试下,哎,我的青春时间哇

*************************************************************************************************************

明白在在投影仪上的问题是因为投影仪分辨率的问题造成的,一直没有时间记录下来,更新下

加载的图片是否会变形取决于加载的图片和目标 Texture2D 的宽高比是否一致。如果图片和 Texture2D 的宽高比不一致,加载的图片可能会被拉伸或压缩以适应目标 Texture2D 的尺寸,从而导致变形。 如果你希望保持加载的图片的宽高比并避免变形,你可以使用以下代码来调整 Texture2D 的尺寸并在保持原始宽高比的同时适应目标尺寸: ```csharp string filePath = "1"; // 假设图片名为 "1.png" Texture2D srcTexture = Resources.Load(filePath) as Texture2D; int targetWidth = 512; // 目标 Texture2D 的宽度 int targetHeight = 512; // 目标 Texture2D 的高度 float aspectRatio = (float)srcTexture.width / srcTexture.height; float targetAspectRatio = (float)targetWidth / targetHeight; int finalWidth, finalHeight; if (aspectRatio > targetAspectRatio) { finalWidth = targetWidth; finalHeight = (int)(targetWidth / aspectRatio); } else { finalWidth = (int)(targetHeight * aspectRatio); finalHeight = targetHeight; } // 创建一个新的 Texture2D,使用目标尺寸调整加载的纹理 Texture2D newTexture = new Texture2D(finalWidth, finalHeight); Graphics.ConvertTexture(srcTexture, newTexture); // 现在,newTexture 将是适应目标尺寸并保持原始宽高比的加载纹理 ``` 这段代码会根据目标 Texture2D 的宽高比和加载纹理的宽高比来计算调整后的尺寸。然后,使用 `Graphics.ConvertTexture` 方法将加载的纹理复制到新的 Texture2D 中,并适应目标尺寸。这样,新的 Texture2D `newTexture` 将是适应目标尺寸并保持原始宽高比的加载纹理,避免了变形。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值