Skip to content

Instantly share code, notes, and snippets.

@popcron
Created February 9, 2019 21:53
Show Gist options
  • Save popcron/29d2a83f5caedbfe9530dce1f9d62816 to your computer and use it in GitHub Desktop.
Save popcron/29d2a83f5caedbfe9530dce1f9d62816 to your computer and use it in GitHub Desktop.
Variable editor camera FOV
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
namespace Popcron.UnityEditor
{
[InitializeOnLoad]
public class EditorCustomCamera
{
public static float FOV
{
get
{
return EditorPrefs.GetFloat(SystemInfo.deviceUniqueIdentifier + ".Popcron.UnityEditor.FOV", 90f);
}
set
{
EditorPrefs.SetFloat(SystemInfo.deviceUniqueIdentifier + ".Popcron.UnityEditor.FOV", value);
}
}
private static float lastTime;
private static readonly Type typeSceneView = typeof(SceneView);
private static readonly FieldInfo fieldInfo = typeSceneView.GetField("onPreSceneGUIDelegate", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);
private static WeakReference slave = new WeakReference(null);
static EditorCustomCamera()
{
Add(OnCamera);
SceneView.onSceneGUIDelegate += OnSceneGUI;
}
private static SceneView.OnSceneFunc OnPreSceneGUIDelegate
{
get
{
if (fieldInfo == null) return null;
return fieldInfo.GetValue(null) as SceneView.OnSceneFunc;
}
set
{
if (fieldInfo == null) return;
fieldInfo.SetValue(null, value);
}
}
private static bool HasSlave
{
get
{
return slave.IsAlive;
}
}
private static Camera Slave
{
get
{
return slave.Target as Camera;
}
}
private static void Add(SceneView.OnSceneFunc func)
{
OnPreSceneGUIDelegate = Delegate.Combine(func, OnPreSceneGUIDelegate) as SceneView.OnSceneFunc;
}
private static void Remove(SceneView.OnSceneFunc func)
{
OnPreSceneGUIDelegate = Delegate.Remove(func, OnPreSceneGUIDelegate) as SceneView.OnSceneFunc;
}
private static void OnSceneGUI(SceneView sceneView)
{
if (sceneView == null) return;
else if (sceneView.camera == null) return;
else if (sceneView.in2DMode) return;
GUIStyle style = new GUIStyle();
style.normal.textColor = new Color(1f, 1f, 1f, 1f) * 0.8f;
style.fontSize = 10;
Handles.BeginGUI();
Rect rect = new Rect(Screen.width - 110, 110, 110, 50);
if (rect.Contains(Event.current.mousePosition))
{
GUI.depth = int.MinValue;
GUI.Label(new Rect(Screen.width - 95, 124, 200, 200), "FOV: " + FOV.ToString("0"), style);
GUI.color = Color.white;
FOV = GUI.HorizontalSlider(new Rect(Screen.width - 95, 135, 80, 15), FOV, 60f, 120f);
}
Handles.EndGUI();
}
private static void OnCamera(SceneView sceneView)
{
if (sceneView == null) return;
else if (sceneView.camera == null) return;
else if (sceneView.in2DMode) return;
Handles.BeginGUI();
Camera camera = sceneView.camera;
camera.fieldOfView = FOV;
if (HasSlave)
{
Copy(camera, Slave);
}
Handles.EndGUI();
}
private static void Copy(Camera camera, Camera slave)
{
if (camera == null || slave == null) return;
slave.fieldOfView = camera.fieldOfView;
slave.transform.position = camera.transform.position;
slave.transform.rotation = camera.transform.rotation;
}
}
}
@popcron
Copy link
Author

popcron commented Feb 9, 2019

Tired of low field of view in the editor?

Add this piece of code to any folder named Editor.
Then hover over the corner under perspective tool to change field of view.

Preview of what it looks like

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment