using System;
using UnityEditor;
using UnityEngine;
namespace Unity.Tutorials.Core.Editor
{
///
/// The Scene View camera mode
///
public enum SceneViewCameraMode
{
///
/// 2D view.
///
SceneView2D,
///
/// 3D view.
///
SceneView3D
};
///
/// Determines how the camera position is applied when loaded
///
public enum SceneViewFocusMode
{
///
/// The camera is positioned as specified by the various settings.
///
Manual,
///
/// A specific object is framed automatically.
///
FrameObject
};
///
/// Used to store and apply scene view camera settings
///
[Serializable]
public class SceneViewCameraSettings
{
///
/// The Scene View camera mode
///
public SceneViewCameraMode CameraMode => m_CameraMode;
[SerializeField]
SceneViewCameraMode m_CameraMode = SceneViewCameraMode.SceneView2D;
///
/// Determines how the camera position is applied when loaded
///
public SceneViewFocusMode FocusMode => m_FocusMode;
[SerializeField]
SceneViewFocusMode m_FocusMode = SceneViewFocusMode.Manual;
///
/// Is the camera ortographic?
///
public bool Orthographic => m_Orthographic;
[SerializeField]
bool m_Orthographic = false;
///
/// Ortographic size of the camera
///
public float Size => m_Size;
[SerializeField]
float m_Size = default;
///
/// The point the camera will look at
///
public Vector3 Pivot => m_Pivot;
[SerializeField]
Vector3 m_Pivot = default;
///
/// The rotation of the camera
///
public Quaternion Rotation => m_Rotation;
[SerializeField]
Quaternion m_Rotation = default;
///
/// The object that can be framed by the camera.
/// Applicable if SceneViewFocusMode.FrameObject used as FocusMode.
///
public SceneObjectReference FrameObject => m_FrameObject;
[SerializeField]
SceneObjectReference m_FrameObject = null;
///
/// Are these camera settings going to be used?
///
public bool Enabled => m_Enabled;
[SerializeField]
bool m_Enabled = false;
///
/// Applies the saved camera settings to the current scene camera
///
public void Apply()
{
var sceneView = EditorWindow.GetWindow(null, false);
sceneView.in2DMode = (CameraMode == SceneViewCameraMode.SceneView2D);
switch (FocusMode)
{
case SceneViewFocusMode.FrameObject:
GameObject go = FrameObject.ReferencedObjectAsGameObject;
if (go == null)
throw new InvalidOperationException("Error looking up frame object");
sceneView.Frame(GameObjectProxy.CalculateBounds(go), true);
break;
case SceneViewFocusMode.Manual:
sceneView.LookAt(Pivot, Rotation, Size, Orthographic, false);
break;
default:
throw new NotImplementedException(string.Format("Focus mode {0} not supported", FocusMode));
}
}
}
}