Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Unity editor extension to display a zoom indicator over the 2D scene view. Click it to reset to 100% zoom.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;
// Display a zoom indicator overlay on the 2D scene view. Click it to reset to 100% zoom
public static class SceneViewUIOverlay
static GUIStyle buttonStyle = new GUIStyle();
static SceneViewUIOverlay()
SceneView.onSceneGUIDelegate += OnSceneGUI;
buttonStyle.normal.textColor = Color.white;
static void OnSceneGUI(SceneView sceneView)
if (!sceneView.in2DMode)
// Calculate current zoom level
float zoom = GetSceneViewHeight(sceneView) / ( * 2f);
// Display zoom indicator in top left corner; clicking the indicator resets to 100%
if (GUI.Button(new Rect(5, 5, 50, buttonStyle.lineHeight), $"{zoom * 100:N0}%", buttonStyle))
SetSceneViewZoom(sceneView, 1f);
static float GetSceneViewHeight(SceneView sceneView)
// Don't use sceneView.position.height, as it does not account for the space taken up by
// toolbars.
return sceneView.position.width /;
static void SetSceneViewZoom(SceneView sceneView, float zoom)
float orthoHeight = GetSceneViewHeight(sceneView) / 2f / zoom;
// We can't set camera.orthographicSize directly, because SceneView overrides it
// every frame based on SceneView.size, so set SceneView.size instead.
// See SceneView.GetVerticalOrthoSize for the source of these sqrts.
//sceneView.size = orthoHeight * Mathf.Sqrt(2f) * Mathf.Sqrt(;
float size = orthoHeight * Mathf.Sqrt(2f) * Mathf.Sqrt(;
sceneView.LookAt(sceneView.pivot, sceneView.rotation, size);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment