using UnityEngine;
using System;
using UnityEditor;
using System.Collections.Generic;
using UnityEngine.Serialization;
namespace Unity.Cinemachine.Editor
{
///
/// User-definable named presets for lenses. This is a Singleton asset, available in editor only
///
[Serializable]
public sealed class CinemachineLensPalette : ScriptableObject
{
static CinemachineLensPalette s_Instance = null;
static bool s_AlreadySearched = false;
/// Get the singleton instance of this object, or null if it doesn't exist
public static CinemachineLensPalette InstanceIfExists
{
get
{
if (!s_AlreadySearched)
{
s_AlreadySearched = true;
var guids = AssetDatabase.FindAssets("t:CinemachineLensPalette");
for (int i = 0; i < guids.Length && s_Instance == null; ++i)
s_Instance = AssetDatabase.LoadAssetAtPath(
AssetDatabase.GUIDToAssetPath(guids[i]));
}
return s_Instance;
}
}
/// Get the singleton instance of this object. Creates asset if nonexistent
public static CinemachineLensPalette Instance
{
get
{
if (InstanceIfExists == null)
{
var newAssetPath = EditorUtility.SaveFilePanelInProject(
"Create Lens Palette asset", "CinemachineLensPalette", "asset",
"This editor-only file will contain the lens presets for this project");
if (!string.IsNullOrEmpty(newAssetPath))
{
s_Instance = CreateInstance();
// Create some sample presets
s_Instance.Presets = new()
{
new () { Name = "21mm", VerticalFOV = 60f },
new () { Name = "35mm", VerticalFOV = 38f },
new () { Name = "58mm", VerticalFOV = 23f },
new () { Name = "80mm", VerticalFOV = 17f },
new () { Name = "125mm", VerticalFOV = 10f }
};
AssetDatabase.CreateAsset(s_Instance, newAssetPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
return s_Instance;
}
}
/// Lens Preset
[Serializable]
public struct Preset
{
/// The name of the preset
[Tooltip("Lens Name")]
[FormerlySerializedAs("m_Name")]
public string Name;
///
/// This is the camera view in vertical degrees. For cinematic people, a 50mm lens
/// on a super-35mm sensor would equal a 19.6 degree FOV
///
[Range(1f, 179f)]
[Tooltip("This is the camera view in vertical degrees. For cinematic people, "
+ " a 50mm lens on a super-35mm sensor would equal a 19.6 degree FOV")]
[FormerlySerializedAs("m_FieldOfView")]
public float VerticalFOV;
}
/// The array containing Preset definitions for nonphysical cameras
[Tooltip("The array containing Preset definitions, for nonphysical cameras")]
public List Presets = new();
/// Get the index of the preset that matches the lens settings
/// Vertical field of view
/// the preset index, or -1 if no matching preset
public int GetMatchingPreset(float verticalFOV)
=> Presets.FindIndex(x => Mathf.Approximately(x.VerticalFOV, verticalFOV));
/// Get the index of the first preset that matches the preset name
/// Name of the preset
/// the preset index, or -1 if no matching preset
public int GetPresetIndex(string presetName)
=> Presets.FindIndex(x => x.Name == presetName);
}
}