Firstborn/Library/PackageCache/com.unity.cinemachine@2.8.9/Samples~/Cinemachine Example Scenes/Scenes/2D/MouseScrollZoom2D.cs

56 lines
1.6 KiB
C#
Raw Normal View History

2023-03-28 13:24:16 -04:00
using System;
using UnityEngine;
namespace Cinemachine.Examples
{
[RequireComponent(typeof(CinemachineVirtualCamera))]
[SaveDuringPlay] // Enable SaveDuringPlay for this class
public class MouseScrollZoom2D : MonoBehaviour
{
[Range(0, 10)]
public float ZoomMultiplier = 1f;
[Range(0, 100)]
public float MinZoom = 1f;
[Range(0, 100)]
public float MaxZoom = 50f;
CinemachineVirtualCamera m_VirtualCamera;
float m_OriginalOrthoSize;
void Awake()
{
m_VirtualCamera = GetComponent<CinemachineVirtualCamera>();
m_OriginalOrthoSize = m_VirtualCamera.m_Lens.OrthographicSize;
#if UNITY_EDITOR
// This code shows how to play nicely with the VirtualCamera's SaveDuringPlay functionality
SaveDuringPlay.SaveDuringPlay.OnHotSave -= RestoreOriginalOrthographicSize;
SaveDuringPlay.SaveDuringPlay.OnHotSave += RestoreOriginalOrthographicSize;
#endif
}
#if UNITY_EDITOR
void OnDestroy()
{
SaveDuringPlay.SaveDuringPlay.OnHotSave -= RestoreOriginalOrthographicSize;
}
void RestoreOriginalOrthographicSize()
{
m_VirtualCamera.m_Lens.OrthographicSize = m_OriginalOrthoSize;
}
#endif
void OnValidate()
{
MaxZoom = Mathf.Max(MinZoom, MaxZoom);
}
void Update()
{
float zoom = m_VirtualCamera.m_Lens.OrthographicSize + Input.mouseScrollDelta.y * ZoomMultiplier;
m_VirtualCamera.m_Lens.OrthographicSize = Mathf.Clamp(zoom, MinZoom, MaxZoom);
}
}
}