Created
October 11, 2021 07:43
-
-
Save reitowo/bc0e9d6e31cbd26bb6f3ec79911eb6ce to your computer and use it in GitHub Desktop.
Unity Editor Change UI Scale Factor With Game View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Reflection; | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditor.SceneManagement; | |
using UnityEngine.UI; | |
[InitializeOnLoad] | |
public class EditorUIScaler { | |
static EditorUIScaler() { | |
var assembly = typeof(UnityEditor.EditorWindow).Assembly; | |
var gameViewType = assembly.GetType("UnityEditor.GameView"); | |
var playModeViewType = assembly.GetType("UnityEditor.PlayModeView"); | |
var targetSizeField = playModeViewType.GetField("m_TargetSize", BindingFlags.NonPublic | BindingFlags.Instance); | |
EditorWindow gameView = null; | |
CanvasScaler[] canvases = null; | |
EditorApplication.delayCall += () => { | |
gameView = EditorWindow.GetWindow(gameViewType); | |
canvases = EditorSceneManager.GetActiveScene().GetRootGameObjects().Where((g) => g.name.EndsWith("Canvas")) | |
.Select((g) => g.GetComponent<CanvasScaler>()).ToArray(); | |
}; | |
EditorApplication.update += () => { | |
if (gameView && canvases != null && !EditorApplication.isPlaying) { | |
var size = (Vector2)targetSizeField.GetValue(gameView); | |
foreach (var canvas in canvases) { | |
canvas.scaleFactor = size.x * 0.7f / 1920; | |
} | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment