The main goal is to show the cameras current state/s.
The main script was before like this. The problem is that it was working fine but only once in the Start and I want it to update if there is any change in any of the cameras state enable/disable and display it in the List but I don’t want to make it loop over the foreach loops nonsotp in the Update so I’m stuck.
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)
{
if (freelook.isActiveAndEnabled)
currentActiveCameras.Add(freelook.Name);
}
foreach (CinemachineVirtualCamera vir in Virtual)
{
if (vir.isActiveAndEnabled)
currentActiveCameras.Add(vir.Name);
}
for(int i = 0; i < AllCameras.Count; i++)
{
if (AllCameras[i].isActiveAndEnabled)
currentActiveCameras.Add(AllCameras[i].name);
}
}
}
Then I thought to do it maybe in another way.
I created a small script not sure yet what and how to do the rest in the script that will be attached to each camera :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraInformation : MonoBehaviour
{
public string cameraState;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnEnable()
{
cameraState =
}
private void OnDisable()
{
}
}
And the first script I changed it to something like this :
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 main goal with this try is to display the current camera state/s in the main script CamerasInfo and also to display the current state of each camera in the small script CameraInfo.
but I’m not sure how to register and get the info from the OnEnable and OnDisable events.
My main goal is to know while the game is running what cameras are currently active and enabled and what are disabled. So if virtual camera 1 is active then at some point in the game virtual camera 1 is disable and main camera is enabled or both enabled or both disabled then update the strings at run time.