加载并设置渲染纹理

Load and set Render Texture

我有 CamFeed.rendertexture 和 CamFeed.mat 文件。我使用我的脚本加载它们并将其设置为相机作为目标纹理和 material 分别设置到另一个四边形,但它只显示黑屏。

Material newMat = Resources.Load("CamFeed", typeof(Material)) as Material;
RenderTexture rendertexture = Resources.Load("CamFeed", typeof(Material)) as RenderTexture;

FirstPersonCamera.targetTexture = rendertexture;
quad.GetComponent<Renderer>().material = newMat;

我怎样才能解决这个问题?

嗯,首先,为什么要将 typeof(Material) 转换为 RenderTexture?你应该在你的 Resources 文件夹中各有一个,命名独特,这样你就可以通过以下方式实例化它们:

Material newMat = Resources.Load<Material>("CamFeedMaterial");
RenderTexture rendertexture = Resources.Load<RenderTexture>("CamFeedTexture");

还要确保您的 material 使用 Unlit\Texture 等着色器,因为您需要在编辑器中或代码中将 RenderTexture 附加到它:

newMat.mainTexture = rendertexture;

最后,您的最后一段代码保持不变:

FirstPersonCamera.targetTexture = rendertexture;
quad.GetComponent<Renderer>().material = newMat;