【发布时间】:2016-02-04 09:02:30
【问题描述】:
如果相机不移动,我想在纹理中绘制相机视图并在画布中显示纹理。我为 android 运行它,然后我得到黑色纹理,但对于 WebPlayer 来说很好!
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;
}
【问题讨论】:
-
我确定这是因为您从 Internet 上的某个地方复制粘贴了此代码。 Update()(和所有其他单一行为)函数在应用程序编译时随机排序,其他平台上的顺序可能不同。您可能需要使用 Unity 执行顺序 明确地对它们进行排序。总之 - 确保在主相机渲染时 camera.targetTexture 不为空。 其他提示:你不能有 32 位纹理Source。另外,试着把抗锯齿调低一点,8 对任何手机来说都太多了,要求很高。
-
@NikaKasradze 虽然在 SO 上人们通常会立即假设代码是在不知情的情况下粘贴的,但这是 Unity 中的错误的结果。 Unity 中一个非常知名的错误。自 4.X 以来,RenderTexture 一直存在问题,并持续到今天的当前版本中。
-
@LoungeKatt 我的错,我不知道是什么击中了我,编辑我的评论,谢谢,编辑:我无法编辑我之前的评论,哈哈,它太旧了¯_(ツ)_/¯我承认我有点粗鲁,对此我深表歉意