android - Unity Convert Texture to Texture2d 在 android 设备上需要很多时间

标签 android unity3d textures texture2d

我有一个将 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 and RenderTextureFormat.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 use SystemInfo.copyTextureSupport to check.

关于android - Unity Convert Texture to Texture2d 在 android 设备上需要很多时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63045900/

相关文章:

opengl - 如何在 openGL 纹理上使用 Cuda NPP(NVIDIA Performance Primitives)

c++ - sdl表面到opengl纹理

javascript - THREE.js renderTarget 具有半透明纹理,看起来与黑色背景混合

java - Android:将自定义 View 添加到 XML

java - 变量在函数外丢失了它的值?

android - Crashlytics 连接被拒绝 - 无法连接到无法连接到 api.crashlytics.com

ios - 如何检查用户是否从我的另一个应用程序安装了我的应用程序? iOS(统一)

android - AutoCompleteTextView 在 Android 2.3.5 中不显示建议

c# - 统一: What's the difference between a PlayMode UnityTest and an EditMode UnityTest?

android - 用于 Unity 的 Windows 到 ios 文件?