using System;
using UnityEngine;
namespace Framework.Cameras {
/// Helper methods for dealing with cameras
public static class CameraHelper {
/// Makes the specified camera the active camera
/// Camera that will become the active camera
///
/// Whether the game object holding the previously active camera should be
/// disabled, including any other components such as audio sources that it
/// is holding. This is usually a good idea.
///
public static Camera ActivateCamera(Camera camera, bool disablePrevious = true) {
Camera previousCamera = Camera.main;
// First tag the new camera as MainCamera. This should switch
// what UnityEngine.Camera.main returns and leave it clear which
// camera is to become the main camera when the previous one is off.
camera.tag = "MainCamera";
// If another camera was active previously, take away its MainCamera
// tag and disable it completely if so desired by the caller
if(previousCamera != null) {
GameObject previousCameraGameObject = previousCamera.gameObject;
if(!ReferenceEquals(previousCamera, camera)) {
previousCameraGameObject.tag = "Untagged";
if(disablePrevious) {
previousCameraGameObject.SetActive(false);
}
}
}
// With the previous camera disabled, enable this one.
// Doing it in this order ensures that no two cameras + audio listeners
// or such are active at the same time.
camera.enabled = false;
camera.enabled = true; // Force OnEnable() for ICameraManager (does it work?)
camera.gameObject.SetActive(true);
return previousCamera;
}
}
} // namespace Framework.Cameras