What i want to do is when running the game the main camera is active.
Then i want to display at the bottom right corner a small window with another camera i have.
Then when clicking on the C key to switch between the two cameras i want to turn off the preview camera and it’s on the preview camera is active and then switching back to the main camera to turn on back the preview camera as small window preview.
First what i did so far.
In the editor: Assets > Create > Render Texture
Then in the Hierarchy i have a Capsule.
Under the Capsule as child i have a Camera.
On the Capsule i have two scripts i attached.
The first script is to switch between the cameras:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchCameras : MonoBehaviour {
public Camera[] cameras;
public int currentCamera = 0;
public GameObject objectToHide;
void Awake()
{
if (cameras == null || cameras.Length == 0)
{
Debug.LogError("No cameras assigned..", gameObject);
this.enabled = false;
}
EnableOnlyFirstCamera();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.C))
{
// disable current
cameras[currentCamera].enabled = false;
// increment index and wrap after finished array
currentCamera = (currentCamera + 1) % cameras.Length;
// enable next
cameras[currentCamera].enabled = true;
if (cameras[currentCamera].name == "Camera")
{
objectToHide.GetComponent<Renderer>().enabled = false;
}
else
{
objectToHide.GetComponent<Renderer>().enabled = true;
}
}
}
void EnableOnlyFirstCamera()
{
for (int i = 0; i < cameras.Length; i++)
{
// only 1==0 returns true
cameras[i].enabled = (i == 0);
}
}
}
The second script is to rotate the camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraRotate : MonoBehaviour {
[SerializeField]
private MouseView m_MouseView;
public Camera m_Camera;
// Use this for initialization
void Start ()
{
m_MouseView.Init(transform, m_Camera.transform);
}
// Update is called once per frame
void Update () {
RotateView();
}
private void FixedUpdate()
{
m_MouseView.UpdateCursorLock();
}
private void RotateView()
{
m_MouseView.LookRotation(transform, m_Camera.transform);
}
}
This scripts are working fine and it was all working fine until i tried to add the Render Texture.
In the Hierarchy i also have the Main Camera.
Next thing i did was to add a Canvas as child of the Main Camera.
And then i added a RawImage as child of the Canvas.
Last two things i did:
Dragged the Render Texture to the RawImage in the Inspector to the Texture field.
And also dragged the same Render Texture to the Camera in the Inspector to the Target Texture field.
Now when i’m running the game i don’t see any small preview window of the Camera in the right bottom corner. But if i will click on C to switch to Camera then i will see the small preview window in the right bottom corner but then also i will see a error message in the middle: Display 1 No cameras rendering.
What i want to do is kind simple i think but i’m not sure where is the problem, with the Canvas and Render Texture ? Or maybe with the two scripts ?
What i want to do is: Running the game the Main Camera is active and in the right bottom corner will be a window showing the Camera.
When i click on C it will switch to the Camera will turn off the preview small window in the right bottom corner the Camera now will be active. when i will click on C again to switch back to the Main Camera the Main Camera will be active and the Camera will be a small window again in the right bottom corner.
I don’t want to only turn off close the preview small window when switching to Camera but also make Camera as the active one in the game view.
The idea is to switch and active between the two cameras.