How can I get all cameras enabled true false states ?
The first script is attached to empty gameobject and here I'm getting all the cameras I'm using and add to them another script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using System.Linq;
public class CamerasInfo : MonoBehaviour
{
public List<CinemachineFreeLook> FreeLook;
public List<CinemachineVirtualCamera> Virtual;
public List<Camera> AllCameras;
public List<string> currentActiveCameras;
// Start is called before the first frame update
void Start()
{
Cameras();
}
// Update is called once per frame
void Update()
{
}
public void Cameras()
{
FreeLook = FindObjectsOfType<CinemachineFreeLook>().ToList();
Virtual = FindObjectsOfType<CinemachineVirtualCamera>().ToList();
for (int i = 0; i < Virtual.Count;)
{
if (!Virtual[i].name.StartsWith("CM"))
Virtual.RemoveAt(i);
else
i++;
}
AllCameras = Camera.allCameras.ToList();
foreach (CinemachineFreeLook freelook in FreeLook)
{
freelook.gameObject.AddComponent<CameraInformation>();
}
foreach (CinemachineVirtualCamera vir in Virtual)
{
vir.gameObject.AddComponent<CameraInformation>();
}
for (int i = 0; i < AllCameras.Count; i++)
{
AllCameras[i].gameObject.AddComponent<CameraInformation>();
}
}
}
The second script that I added is attached to each gameobject with the cameras. In this script I want to check in real time while the game is running the cameras state/s if each camera is enabled true or false and show it in the string.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraInformation : MonoBehaviour
{
public string currentCameraState;
private bool IsFirstTime = false;
// Start is called before the first frame update
void Start()
{
GetCameraInfo();
}
// Update is called once per frame
void Update()
{
GetCameraInfo();
}
private void GetCameraInfo()
{
MonoBehaviour[] scripts = gameObject.GetComponents<MonoBehaviour>();
foreach (MonoBehaviour script in scripts)
{
if (script.GetType().FullName.StartsWith("Cinemachine"))
{
if(IsFirstTime == false)
{
currentCameraState = script.isActiveAndEnabled.ToString();
IsFirstTime = true;
}
if (script != script.isActiveAndEnabled)
currentCameraState = script.isActiveAndEnabled.ToString();
}
}
}
}
But I'm facing two problems with this script the CameraInformation.
For some reason it's working on the first camera the camera start as enabled true then it's changing to false and it's showing it in the string field. but I have two cinemachine cameras and on the second one it's not showing the changes. The second camera start as enabled false then change to true but it's still showing in the string that it's false. Why it's not working on the second camera either ?
Second problem it's not a problem it's more a question. Is that a bad idea to call the GetCameraInfro method from inside the Update so it's doing the foreach nonstop ? If it does what else can I do to monitor the cameras enabled true false state in real time while the game is running ?