Render Texture Black on Android Only

2019-08-15 06:46发布

I want to draw view of camera in texture and show texture in canvas, if camera not moving. I run it for android then I got black texuture, but for WebPlayer is Good!

public RawImage rawImage;

private void Start()
{
   texture = new RenderTexture(camera.pixelWidth, camera.pixelHeight, 32, RenderTextureFormat.ARGB32);
    texture.antiAliasing = 8;
    texture.Create();
}

public void ShowTexture()
{
   camera.targetTexture = texture;
   RenderTexture.active = texture2;
   if (!RenderTexture.active.IsCreated())
      RenderTexture.active.Create();

   camera.Render();

   var texture2d = new Texture2D(camera.targetTexture.width, camera.targetTexture.height, TextureFormat.RGB24, true, true);
   texture2d.ReadPixels(new Rect(0, 0, camera.pixelWidth, camera.pixelHeight), 0, 0);
   texture2d.Apply(false);

   RenderTexture.active = null;
   camera.targetTexture = null;

  rawImage.texture = texture2d;
}

2条回答
狗以群分
2楼-- · 2019-08-15 07:47

This is a known issue with Unity. You can find more details at:

https://forum.unity3d.com/threads/rendertexture-not-working-on-ios-and-android-unity-4-2-0f4.192561/

https://forum.unity3d.com/threads/render-texture-not-working-on-device-unity-5-2-1f1.358483/

https://forum.unity3d.com/threads/render-texture-works-in-editor-but-not-on-devices-after-upgrade-to-unity-5.362397/

and a few others where the moderators and staff claim it is fixed in a future release or (with a touch of unnecessary arrogance) that the issue is the user and a bug never existed at all.

BUT

This is going to sound silly, but add an ImageEffect to the main camera. I have made a dummy effect that is attached to my main camera and without any logical explanation, it fixes RenderTexture on mobile.

DummyEffect.cs:

using UnityEngine;

[ExecuteInEditMode]
[AddComponentMenu("Image Effects/Dummy Effect")]
public class DummyEffect : ImageEffectBase {

    // Called by camera to apply image effect
    void OnRenderImage (RenderTexture source, RenderTexture destination) {
        Graphics.Blit (source, destination, material);
    }
}

DummyEffect.shader:

Shader "Hidden/Dummy Effect" {
Properties {
    _MainTex ("Base (RGB)", RECT) = "white" {}
}

SubShader {
    Pass {
        ZTest Always Cull Off ZWrite Off
        Fog { Mode off }

CGPROGRAM
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"

uniform sampler2D _MainTex;

float4 frag (v2f_img i) : COLOR {
    return tex2D(_MainTex, i.uv);
}
ENDCG

    }
}

Fallback off

}
查看更多
smile是对你的礼貌
3楼-- · 2019-08-15 07:48

figure this might help someone...

Replicated same issue by accident... working both on iOS and Droid. Then disabled camera clear, and hey presto... works on iOS but black on Android. changed camera clear back to Z-depth only... and both iOS and Droid work again.

So try changing your camera clear settings.

查看更多
登录 后发表回答