Something that has been going on for some time in Unity since they added the Scaling of the game view is that sometimes after compilation and/or when pressing the play button the scale change for no reason (zoom in the game view). Restarting unity doesn’t fix this. It seems to always go to 0.55 in scale.
Am I the only one that have this problem ? Did anyone managed to understand why it happens and how to solve this ?
Thanks for the help
It seems that changing the layout through the “saved layout” menu (top right of the editor) temporary fix this issue.
Improved code:
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
[InitializeOnLoad]
public static class FixResolutionScale
{
static FixResolutionScale()
{
EditorApplication.playModeStateChanged += OnPlayStateChanged;
SetGameViewScale();
}
private static void OnPlayStateChanged(PlayModeStateChange playModeStateChange)
{
SetGameViewScale();
}
private static void SetGameViewScale()
{
Type gameViewType = GetGameViewType();
EditorWindow gameViewWindow = GetGameViewWindow(gameViewType);
if (gameViewWindow == null)
{
Debug.LogError("GameView is null!");
return;
}
var defScaleField = gameViewType.GetField("m_defaultScale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
//whatever scale you want when you click on play
float defaultScale = 0.1f;
var areaField = gameViewType.GetField("m_ZoomArea", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var areaObj = areaField.GetValue(gameViewWindow);
var scaleField = areaObj.GetType().GetField("m_Scale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
scaleField.SetValue(areaObj, new Vector2(defaultScale, defaultScale));
}
private static Type GetGameViewType()
{
Assembly unityEditorAssembly = typeof(EditorWindow).Assembly;
Type gameViewType = unityEditorAssembly.GetType("UnityEditor.GameView");
return gameViewType;
}
private static EditorWindow GetGameViewWindow(Type gameViewType)
{
Object[] obj = Resources.FindObjectsOfTypeAll(gameViewType);
if (obj.Length > 0)
{
return obj[0] as EditorWindow;
}
return null;
}
}
EDIT: This seems to now have been fixed in 2019.1.8:
Editor: Fixed game view scale in play mode for mobile platforms. (1140742, 1151179)
https://unity3d.com/unity/whats-new/2019.1.8
Also fixed or planned fixed for some other major Unity versions:
An alternative solution, if you don’t need to have the game view set to a certain resolution, is to set it instead to a certain aspect ratio. Then the scaling problem doesn’t seem to occur.
bmulla
March 14, 2019, 7:40pm
4
Just use this code snippet
http://answers.unity.com/answers/1612575/view.html
#if UNITY_EDITOR
void Awake()
{
SetGameViewScale();
}
void SetGameViewScale()
{
System.Reflection.Assembly assembly = typeof(UnityEditor.EditorWindow).Assembly;
System.Type type = assembly.GetType("UnityEditor.GameView");
UnityEditor.EditorWindow v = UnityEditor.EditorWindow.GetWindow(type);
var defScaleField = type.GetField("m_defaultScale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
//whatever scale you want when you click on play
float defaultScale = 0.1f;
var areaField = type.GetField("m_ZoomArea", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var areaObj = areaField.GetValue(v);
var scaleField = areaObj.GetType().GetField("m_Scale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
scaleField.SetValue(areaObj, new Vector2(defaultScale, defaultScale));
}
#endif
A fix to previous solution. In my case if I edit the scripts, unity 2019.1.5 changes the scale of game window. So …
Just add this to your project.
using System;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;
[InitializeOnLoad]
[ExecuteInEditMode]
public class FixGameWindowScaleBug
{
private static bool mUpdate = false;
static FixGameWindowScaleBug()
{
EditorApplication.update += update;
EditorApplication.delayCall += delayCall;
SetGameViewScale();
}
private static void delayCall()
{
mUpdate = true;
}
private static void update()
{
if (mUpdate)
{
SetGameViewScale();
mUpdate = false;
}
}
private static void SetGameViewScale()
{
Type gameViewType = GetGameViewType();
EditorWindow gameViewWindow = GetGameViewWindow(gameViewType);
if (gameViewWindow == null)
{
Debug.LogError("GameView is null!");
return;
}
var defScaleField = gameViewType.GetField("m_defaultScale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
//whatever scale you want when you click on play
float defaultScale = 0.1f;
var areaField = gameViewType.GetField("m_ZoomArea", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
var areaObj = areaField.GetValue(gameViewWindow);
var scaleField = areaObj.GetType().GetField("m_Scale", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
scaleField.SetValue(areaObj, new Vector2(defaultScale, defaultScale));
}
private static Type GetGameViewType()
{
Assembly unityEditorAssembly = typeof(EditorWindow).Assembly;
Type gameViewType = unityEditorAssembly.GetType("UnityEditor.GameView");
return gameViewType;
}
private static EditorWindow GetGameViewWindow(Type gameViewType)
{
Object[] obj = Resources.FindObjectsOfTypeAll(gameViewType);
if (obj.Length > 0)
{
return obj[0] as EditorWindow;
}
return null;
}
}
I noticed this happening when I set a custom aspect ratio.