using System.Collections.Generic;
using UnityEngine;
namespace Cinemachine
{
///
/// Base class for a Cinemachine Virtual Camera extension module.
/// Hooks into the Cinemachine Pipeline.
///
[DocumentationSorting(23, DocumentationSortingAttribute.Level.API)]
public abstract class CinemachineExtension : MonoBehaviour
{
/// Useful constant for very small floats
protected const float Epsilon = Utility.UnityVectorExtensions.Epsilon;
/// Get the associated CinemachineVirtualCameraBase
public CinemachineVirtualCameraBase VirtualCamera
{
get
{
if (m_vcamOwner == null)
m_vcamOwner = GetComponent();
return m_vcamOwner;
}
}
CinemachineVirtualCameraBase m_vcamOwner;
/// Connect to virtual camera pipeline.
/// Override implementations must call this base implementation
protected virtual void Awake()
{
ConnectToVcam();
}
/// Disconnect from virtual camera pipeline.
/// Override implementations must call this base implementation
protected virtual void OnDestroy()
{
if (VirtualCamera != null)
VirtualCamera.RemovePostPipelineStageHook(PostPipelineStageCallback);
}
void ConnectToVcam()
{
if (VirtualCamera == null)
Debug.LogError("CinemachineExtension requires a Cinemachine Virtual Camera component");
else
VirtualCamera.AddPostPipelineStageHook(PostPipelineStageCallback);
mExtraState = null;
}
///
/// This callback will be called after the virtual camera has implemented
/// each stage in the pipeline. This method may modify the referenced state.
/// If deltaTime less than 0, reset all state info and perform no damping.
///
protected abstract void PostPipelineStageCallback(
CinemachineVirtualCameraBase vcam,
CinemachineCore.Stage stage, ref CameraState state, float deltaTime);
/// Because extensions can be placed on manager cams and will in that
/// case be called for all the vcam children, vcam-specific state information
/// should be stored here. Just define a class to hold your state info
/// and use it exclusively when calling this.
protected T GetExtraState(ICinemachineCamera vcam) where T : class, new()
{
if (mExtraState == null)
mExtraState = new Dictionary();
System.Object extra = null;
if (!mExtraState.TryGetValue(vcam, out extra))
extra = mExtraState[vcam] = new T();
return extra as T;
}
/// Ineffeicient method to get all extra state infor for all vcams.
/// Intended for Editor use only, not runtime!
///
protected List GetAllExtraStates() where T : class, new()
{
var list = new List();
if (mExtraState != null)
foreach (var v in mExtraState)
list.Add(v.Value as T);
return list;
}
private Dictionary mExtraState;
}
}