namespace Zinnia.Visual { using System.Collections.Generic; using UnityEngine; using Zinnia.Data.Collection.List; using Zinnia.Extension; /// /// Attempts to mutate the valid camera background settings and caches the previous settings for later restoration. /// public class CameraBackgroundMutator : MonoBehaviour { public struct CameraSettings { public CameraClearFlags clearFlags; public Color backgroundColor; } [Tooltip("The Camera collection to mutate the background of.")] [SerializeField] private UnityObjectObservableList targetCameras; /// /// The collection to mutate the background of. /// public UnityObjectObservableList TargetCameras { get { return targetCameras; } set { targetCameras = value; } } [Tooltip("The camera clear flags to set the valid cameras to.")] [SerializeField] private CameraClearFlags targetClearFlags; /// /// The camera clear flags to set the valid cameras to. /// public CameraClearFlags TargetClearFlags { get { return targetClearFlags; } set { targetClearFlags = value; } } [Tooltip("The Color to mutate the background to.")] [SerializeField] private Color targetBackgroundColor = Color.black; /// /// The to mutate the background to. /// public Color TargetBackgroundColor { get { return targetBackgroundColor; } set { targetBackgroundColor = value; } } /// /// The existing cached settings before it was changed. /// protected Dictionary cachedCameraSettings = new Dictionary(); /// /// Mutates the collection with the target settings. /// public virtual void Mutate() { if (!this.IsValidState()) { return; } foreach (Object currentCameraObject in TargetCameras.NonSubscribableElements) { Camera currentCamera = (Camera)currentCameraObject; if (currentCamera == null) { continue; } CameraSettings currentSettings = new CameraSettings() { clearFlags = currentCamera.clearFlags, backgroundColor = currentCamera.backgroundColor }; cachedCameraSettings[currentCamera] = currentSettings; if (TargetClearFlags != 0) { currentCamera.clearFlags = TargetClearFlags; } currentCamera.backgroundColor = TargetBackgroundColor; } } /// /// Restores the settings to the cached settings before the mutate occurred. /// public virtual void Restore() { if (!this.IsValidState()) { return; } foreach (KeyValuePair currentCamera in cachedCameraSettings) { currentCamera.Key.clearFlags = currentCamera.Value.clearFlags; currentCamera.Key.backgroundColor = currentCamera.Value.backgroundColor; } cachedCameraSettings.Clear(); } } }