Files
project-reset/Assets/Scripts/Units/CameraSettingsProcessor.cs

115 lines
4.5 KiB
C#

using System;
using Reset.Core;
using Sirenix.OdinInspector;
using Unity.Cinemachine;
using UnityEngine;
public class CameraSettingsProcessor : MonoBehaviour{
public static CameraSettingsProcessor Instance{ get; private set; }
[ShowInInspector, FoldoutGroup("Camera Data", expanded: true), InlineProperty, HideLabel]
public CameraSettingData data;
public static GameObject mainCamera;
private CinemachineCamera main;
private CinemachineOrbitalFollow orbit;
private CinemachineRotationComposer rotComp;
private CinemachineCameraOffset offset;
private CinemachineInputAxisController axisCont;
public void Awake(){
// Singleton management
if (Instance != null && Instance != this) {
Destroy(this);
} else {
Instance = this;
}
// Set references for camera object and cinemachine components
mainCamera = gameObject;
main = mainCamera.GetComponent<CinemachineCamera>();
orbit = mainCamera.GetComponent<CinemachineOrbitalFollow>();
rotComp = mainCamera.GetComponent<CinemachineRotationComposer>();
offset = mainCamera.GetComponent<CinemachineCameraOffset>();
axisCont = mainCamera.GetComponent<CinemachineInputAxisController>();
// Quick check for a camera settings
if (data == null) {
Debug.LogWarning("No Camera Settings Data was found on processing. One will be created. Is this intentional? This will have strong effects on camera movement");
data = new CameraSettingData();
}
// Initialize camera settings values from current values
InitializeAllSettings();
}
void Update(){
SmoothCameraSettings();
ApplyCameraSettings();
}
void SmoothCameraSettings(){
var settings = data.GetAllSettings();
for (int i = 0; i < settings.Count; i++) {
settings[i].SmoothAndEase();
}
}
// Responsible for actively applying the settings to the Cinemachine components
void ApplyCameraSettings(){
main.Lens.FieldOfView = data.mainFieldOfView.Value;
axisCont.Controllers[0].Enabled = data.axisLookEnabledX.Value;
axisCont.Controllers[1].Enabled = data.axisLookEnabledY.Value;
axisCont.Controllers[0].Input.Gain = data.axisLookGainX.Value;
axisCont.Controllers[1].Input.Gain = data.axisLookGainY.Value;
orbit.TrackerSettings.PositionDamping = data.orbitPositionDamping.Value;
orbit.TargetOffset = data.orbitTargetOffset.Value;
orbit.Orbits.Top.Height = data.orbitFollowTopHeight.Value;
orbit.Orbits.Top.Radius = data.orbitFollowTopRadius.Value;
orbit.Orbits.Center.Height = data.orbitFollowCenterHeight.Value;
orbit.Orbits.Center.Radius = data.orbitFollowCenterRadius.Value;
orbit.Orbits.Bottom.Height = data.orbitFollowBottomHeight.Value;
orbit.Orbits.Bottom.Radius = data.orbitFollowBottomRadius.Value;
rotComp.Composition.ScreenPosition = data.rotationComposerScreenPos.Value;
offset.Offset = data.cameraOffsetOffset.Value;
}
[Button]
void InitializeAllSettings(){
data.mainFieldOfView.targetValue = main.Lens.FieldOfView;
data.orbitPositionDamping.targetValue = orbit.TrackerSettings.PositionDamping;
data.orbitTargetOffset.targetValue = orbit.TargetOffset;
data.axisLookEnabledX.targetValue = axisCont.Controllers[0].Enabled;
data.axisLookEnabledY.targetValue = axisCont.Controllers[1].Enabled;
data.axisLookGainX.targetValue = axisCont.Controllers[0].Input.Gain;
data.axisLookGainY.targetValue = axisCont.Controllers[1].Input.Gain;
data.orbitFollowTopHeight.targetValue = orbit.Orbits.Top.Height;
data.orbitFollowTopRadius.targetValue = orbit.Orbits.Top.Radius;
data.orbitFollowCenterHeight.targetValue = orbit.Orbits.Center.Height;
data.orbitFollowCenterRadius.targetValue = orbit.Orbits.Center.Radius;
data.orbitFollowBottomHeight.targetValue = orbit.Orbits.Bottom.Height;
data.orbitFollowBottomRadius.targetValue = orbit.Orbits.Bottom.Radius;
data.rotationComposerScreenPos.targetValue = rotComp.Composition.ScreenPosition;
data.cameraOffsetOffset.targetValue = offset.Offset;
var allSettings = data.GetAllSettings();
for (int i = 0; i < allSettings.Count; i++) {
allSettings[i].Initialize();
allSettings[i].Verify();
}
}
}