Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How can I register each camera information script events onenable and ondisable ?

Discussion in 'Scripting' started by Chocolade, Nov 6, 2020.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    916
    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<string> but I don't want to make it loop over the foreach loops nonsotp in the Update so I'm stuck.

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Cinemachine;
    6. using System.Linq;
    7.  
    8. public class CamerasInfo : MonoBehaviour
    9. {
    10.     public List<CinemachineFreeLook> FreeLook;
    11.     public List<CinemachineVirtualCamera> Virtual;
    12.     public List<Camera> AllCameras;
    13.     public List<string> currentActiveCameras;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         Cameras();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.        
    25.     }
    26.  
    27.     public void Cameras()
    28.     {
    29.         FreeLook = FindObjectsOfType<CinemachineFreeLook>().ToList();
    30.         Virtual = FindObjectsOfType<CinemachineVirtualCamera>().ToList();
    31.         for (int i = 0; i < Virtual.Count;)
    32.         {
    33.             if (!Virtual[i].name.StartsWith("CM"))
    34.                 Virtual.RemoveAt(i);
    35.             else
    36.                 i++;
    37.         }
    38.  
    39.         AllCameras = Camera.allCameras.ToList();
    40.        
    41.         foreach (CinemachineFreeLook freelook in FreeLook)
    42.         {
    43.             if (freelook.isActiveAndEnabled)
    44.                 currentActiveCameras.Add(freelook.Name);
    45.         }
    46.  
    47.         foreach (CinemachineVirtualCamera vir in Virtual)
    48.         {
    49.             if (vir.isActiveAndEnabled)
    50.                 currentActiveCameras.Add(vir.Name);
    51.         }
    52.  
    53.         for(int i = 0; i < AllCameras.Count; i++)
    54.         {
    55.             if (AllCameras[i].isActiveAndEnabled)
    56.                 currentActiveCameras.Add(AllCameras[i].name);
    57.         }
    58.     }
    59. }
    60.  
    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 :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CameraInformation : MonoBehaviour
    7. {
    8.     public string cameraState;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.        
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.        
    20.     }
    21.  
    22.     private void OnEnable()
    23.     {
    24.         cameraState =
    25.     }
    26.  
    27.     private void OnDisable()
    28.     {
    29.        
    30.     }
    31. }
    32.  
    And the first script I changed it to something like this :

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Cinemachine;
    6. using System.Linq;
    7.  
    8. public class CamerasInfo : MonoBehaviour
    9. {
    10.     public List<CinemachineFreeLook> FreeLook;
    11.     public List<CinemachineVirtualCamera> Virtual;
    12.     public List<Camera> AllCameras;
    13.     public List<string> currentActiveCameras;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         Cameras();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.        
    25.     }
    26.  
    27.     public void Cameras()
    28.     {
    29.         FreeLook = FindObjectsOfType<CinemachineFreeLook>().ToList();
    30.         Virtual = FindObjectsOfType<CinemachineVirtualCamera>().ToList();
    31.         for (int i = 0; i < Virtual.Count;)
    32.         {
    33.             if (!Virtual[i].name.StartsWith("CM"))
    34.                 Virtual.RemoveAt(i);
    35.             else
    36.                 i++;
    37.         }
    38.  
    39.         AllCameras = Camera.allCameras.ToList();
    40.        
    41.         foreach (CinemachineFreeLook freelook in FreeLook)
    42.         {
    43.             freelook.gameObject.AddComponent<CameraInformation>();
    44.         }
    45.  
    46.         foreach (CinemachineVirtualCamera vir in Virtual)
    47.         {
    48.             vir.gameObject.AddComponent<CameraInformation>();
    49.         }
    50.  
    51.         for(int i = 0; i < AllCameras.Count; i++)
    52.         {
    53.             AllCameras[i].gameObject.AddComponent<CameraInformation>();
    54.         }
    55.     }
    56. }
    57.  
    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.
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    You would need to write a script that attaches to each camera that you can fire the OnEnable and OnDisable events from.

    Code (CSharp):
    1.  
    2. using Cinemachine;
    3. using UnityEngine;
    4. using UnityEngine.Events;
    5.  
    6. [System.Serializable]
    7. public class CinemachineCamEnableChangedEvent : UnityEvent<CinemachineVirtualCameraBase, bool> { }
    8.  
    9. public class CinemachineCamHelper : MonoBehaviour
    10. {
    11.     public CinemachineCamEnableChangedEvent EnableChanged;
    12.  
    13.     protected CinemachineVirtualCameraBase virtualCam;
    14.  
    15.     public CinemachineVirtualCameraBase VirtualCam
    16.     {
    17.         get
    18.         {
    19.             if (virtualCam == null)
    20.             {
    21.                 virtualCam = GetComponent<CinemachineVirtualCameraBase>();
    22.             }
    23.  
    24.             return virtualCam;
    25.         }
    26.     }
    27.  
    28.     protected void OnEnable()
    29.     {
    30.         EnableChanged?.Invoke(VirtualCam, true);
    31.     }
    32.  
    33.     protected void OnDisable()
    34.     {
    35.         EnableChanged?.Invoke(VirtualCam, false);
    36.     }
    37. }
    38.  
    The above class uses UnityEvents that are editor friendly so you can set it in the inspector if you so desire.
     
  3. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    You can make use of a delegate


    For example you have a class which needs to be informed about enable or disabled state of the camera ( or any game object )

    Code (CSharp):
    1. class MyClass
    2. {      
    3.    public MyClass()
    4.    {
    5.        MyCameraClass  cam  = new MyCameraClass();
    6.        cam.OnCameraEnable =  MyHandler  ;  
    7.        cam.OnCameraDisable =  MyHandler  ;  
    8.    }
    9.  
    10.    void MyHandler(object sender, EventArgs args)    
    11.    {
    12.       Debug.Log ( "The camera status has changed ") ;  
    13.    }
    14. }
    15.  
    16. //---------//---------//---------//---------//---------//---------//---------//---------
    17.  
    18. public delegate void MyDelegate(object sender, Eventargs args);
    19. class MyCameraClass
    20. {
    21.     public event MyDelegate OnCameraEnable ;  
    22.     public event MyDelegate OnCameraDisable ;  
    23.     public MyCameraClass()
    24.    {
    25.  
    26.                 OnCameraEnable(this, EventArgs.Empty);
    27.                 OnCameraDisable(this, EventArgs.Empty);  
    28.     }
    29.  
    30. }
    31.  
    32.