我有一个将 RGB565 Texture2d 转换为 RGB24Texture2d 的函数。
我使用 Texture2d.ReadPixs() api 并且它已成功实现但是当它在 android 上运行时它会在 Texture2d 上花费 50 毫秒 .ReadPixs() 读取的大小为640*480。
我找到了解决方案 here (使用 glReadPixels)但是当从 Android Studio 编译 so 文件时,它无法统一工作。我不擅长 C++ 和 Android Studio...
有没有其他解决办法?
提前致谢!
public static void textureToTexture2D(Texture texture, Texture2D texture2D)
{
if (texture == null)
throw new ArgumentNullException("texture == null");
if (texture2D == null)
throw new ArgumentNullException("texture2D == null");
if (texture.width != texture2D.width || texture.height != texture2D.height)
throw new ArgumentException("texture and texture2D need to be the same size.");
RenderTexture prevRT = RenderTexture.active;
if (texture is RenderTexture)
{
RenderTexture.active = (RenderTexture)texture;
texture2D.ReadPixels(new UnityEngine.Rect(0f, 0f, texture.width, texture.height), 0, 0, false);
texture2D.Apply(false, false);
}
else
{
RenderTexture tempRT = RenderTexture.GetTemporary(texture.width, texture.height, 0, RenderTextureFormat.ARGB32);
Graphics.Blit(texture, tempRT);
RenderTexture.active = tempRT;
//it takes the most time on the function about 50ms
texture2D.ReadPixels(new UnityEngine.Rect(0f, 0f, texture.width, texture.height), 0, 0, false);
texture2D.Apply(false, false);
RenderTexture.ReleaseTemporary(tempRT);
}
RenderTexture.active = prevRT;
}
最佳答案
您也可以尝试使用 Graphics.CopyTexture
public Texture2D toTexture2D(RenderTexture rTex)
{
Texture2D dest = new Texture2D(rTex.width, rTex.height, TextureFormat.RGBA32, false);
Graphics.CopyTexture(renderTexture, dest);
return dest;
}
Texture formats should be compatible (for example,
TextureFormat.ARGB32
andRenderTextureFormat.ARGB32
are compatible).
您需要检查您的目标设备是否支持此功能。
Some platforms might not have functionality of all sorts of texture copying (e.g. copy from a render texture into a regular texture). See
CopyTextureSupport
, and useSystemInfo.copyTextureSupport
to check.
关于android - Unity Convert Texture to Texture2d 在 android 设备上需要很多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63045900/