Camera清除图像
1.前言
Unity清楚某个Camera画面,最简单的方式直接设置SetActive(false)。在此采用CommandBuffer清除图像。
public class EyeSightController : MonoBehaviour
{
public Camera leftCamera;
public Camera rightCamera;
public bool enableSingleEyeMode = false;
private void OnRender(Camera camera)
{
if (camera == leftCamera)
{
ClearCameraRender(camera);
}
}
private void ClearCameraRender(Camera camera)
{
CommandBuffer cmd = new CommandBuffer();
cmd.SetRenderTarget(camera.targetTexture);
cmd.ClearRenderTarget(true, true, Color.black);
Graphics.ExecuteCommandBuffer(cmd);
}
private void Start()
{
Camera.onPostRender += OnRender;
}
}
采用Camera的onPostRender回调时会对所有的camera进行进行相应的处理。如果直接用OnPostRender内置方法时需要挂载在相应camera所在的游戏物体上。