Search Unity

Question Add Game View Resolution Programatically (old solution doesn't work)

Discussion in 'Editor & General Support' started by stalkercatkuz, Apr 4, 2020.

  1. stalkercatkuz

    stalkercatkuz

    Joined:
    Jun 26, 2018
    Posts:
    2
    Hello! I found this solution : https://answers.unity.com/questions...96.915528479.1585746941-2112408147.1582032419
    It works when i change resolution in playmode, but it throws NullReferenceException when trying to add a new resolution in playmode.
    An error occurs when the GameViewSize constructor is called, although the constructor is present.

    Code :

    Code (CSharp):
    1.  public static void AddCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, int height, string text)
    2.     {
    3.         // goal:
    4.         // var group = ScriptableSingleton<GameViewSizes>.instance.GetGroup(sizeGroupType);
    5.         // group.AddCustomSize(new GameViewSize(viewSizeType, width, height, text);
    6.  
    7.         var asm = typeof(Editor).Assembly;
    8.         var sizesType = asm.GetType("UnityEditor.GameViewSizes");
    9.         var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
    10.         var instanceProp = singleType.GetProperty("instance");
    11.         var getGroup = sizesType.GetMethod("GetGroup");
    12.         var instance = instanceProp.GetValue(null, null);
    13.         var group = getGroup.Invoke(instance, new object[] { (int)sizeGroupType });
    14.         var addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize"); // or group.GetType().
    15.         var gvsType = asm.GetType("UnityEditor.GameViewSize");
    16.  
    17.         var types = new Type[] { typeof(GameViewSizeType), typeof(int), typeof(int), typeof(string) };
    18.         // in original
    19.         // var types = new Type[]{typeof(int), typeof(int), typeof(int), typeof(string)};
    20.         // throws exception too
    21.  
    22.         var ctor = gvsType.GetConstructor(types);
    23.  
    24.         // throws exception here
    25.         var newSize = ctor.Invoke(new object[] { viewSizeType, width, height, text });
    26.         // in original
    27.         // var newSize = ctor.Invoke(new object[]{(int)viewSizeType, width, height, text});
    28.         // throws exception too
    29.  
    30.         addCustomSize.Invoke(group, new object[] { newSize });
    31.  
    32.         sizesType.GetMethod("SaveToHDD").Invoke(gameViewSizesInstance, null);
    33.     }

    GameViewSize source code : https://github.com/Unity-Technologi...b/master/Editor/Mono/GameView/GameViewSize.cs
     
  2. kinoshita_natsuko

    kinoshita_natsuko

    Joined:
    May 29, 2020
    Posts:
    2
    This code worked for Unity 2018!
    https://github.com/Syy9/GameViewSizeChanger

    public static void AddCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, int height, string text)
    {
    var group = GetGroup(sizeGroupType);
    var addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize"); // or group.GetType().
    var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");
    string assemblyName = "UnityEditor.dll";
    Assembly assembly = Assembly.Load(assemblyName);
    Type gameViewSize = assembly.GetType("UnityEditor.GameViewSize");
    Type gameViewSizeType = assembly.GetType("UnityEditor.GameViewSizeType");
    ConstructorInfo ctor = gameViewSize.GetConstructor(new Type[]
    {
    gameViewSizeType,
    typeof(int),
    typeof(int),
    typeof(string)
    });
    var newSize = ctor.Invoke(new object[] { (int)viewSizeType, width, height, text });
    addCustomSize.Invoke(group, new object[] { newSize });
    }
     
    Last edited: Sep 16, 2020
    SimonMV likes this.
  3. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Reflection;
    4. using UnityEditor;
    5. using UnityEngine;
    6. using UnityEditorInternal;
    7.  
    8. public class ResolutionManager : EditorWindow
    9. {
    10.     [MenuItem("Assets/Resolution Manager")]
    11.     static void ShowWindow ()
    12.     {
    13.         EditorWindow.GetWindow (typeof(ResolutionManager));
    14.     }
    15.  
    16.     void OnGUI()
    17.     {
    18.         if ( GUILayout.Button( "Add and set new resolution" ) )
    19.         {
    20.             AddResolution(256, 256, "Screenshots");
    21.             SetResolution(GetCount() - 1);
    22.         }
    23.         if ( GUILayout.Button( "Restore to default resolution" ) )
    24.         {
    25.             SetResolution(0);
    26.             RemoveResolution(GetCount() - 1);
    27.         }
    28.     }
    29.  
    30.     void AddResolution(int width, int height, string label)
    31.     {
    32.         Type gameViewSize = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");
    33.         Type gameViewSizes = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
    34.         Type gameViewSizeType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizeType");
    35.         Type generic = typeof(ScriptableSingleton<>).MakeGenericType(gameViewSizes);
    36.         MethodInfo getGroup = gameViewSizes.GetMethod("GetGroup");
    37.         object instance = generic.GetProperty("instance").GetValue(null, null);        
    38.         object group = getGroup.Invoke(instance, new object[] { (int)GameViewSizeGroupType.Standalone });      
    39.         Type[] types = new Type[] { gameViewSizeType, typeof(int), typeof(int), typeof(string)};
    40.         ConstructorInfo constructorInfo = gameViewSize.GetConstructor(types);
    41.         object entry = constructorInfo.Invoke(new object[] { 1, width, height, label });
    42.         MethodInfo addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize");
    43.         addCustomSize.Invoke(group, new object[] { entry });
    44.     }
    45.  
    46.     void RemoveResolution(int index)
    47.     {
    48.         Type gameViewSizes = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
    49.         Type generic = typeof(ScriptableSingleton<>).MakeGenericType(gameViewSizes);
    50.         MethodInfo getGroup = gameViewSizes.GetMethod("GetGroup");
    51.         object instance = generic.GetProperty("instance").GetValue(null, null);
    52.         object group = getGroup.Invoke(instance, new object[] { (int)GameViewSizeGroupType.Standalone });
    53.         MethodInfo removeCustomSize = getGroup.ReturnType.GetMethod("RemoveCustomSize");
    54.         removeCustomSize.Invoke(group, new object[] { index });
    55.     }
    56.  
    57.     int GetCount()
    58.     {
    59.         Type gameViewSizes = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
    60.         Type generic = typeof(ScriptableSingleton<>).MakeGenericType(gameViewSizes);
    61.         MethodInfo getGroup = gameViewSizes.GetMethod("GetGroup");
    62.         object instance = generic.GetProperty("instance").GetValue(null, null);
    63.         PropertyInfo currentGroupType = instance.GetType().GetProperty("currentGroupType");
    64.         GameViewSizeGroupType groupType = (GameViewSizeGroupType)(int)currentGroupType.GetValue(instance, null);
    65.         object group = getGroup.Invoke(instance, new object[] { (int)groupType });
    66.         MethodInfo getBuiltinCount = group.GetType().GetMethod("GetBuiltinCount");
    67.         MethodInfo getCustomCount = group.GetType().GetMethod("GetCustomCount");
    68.         return (int)getBuiltinCount.Invoke(group, null) + (int)getCustomCount.Invoke(group, null);  
    69.     }
    70.  
    71.     void SetResolution(int index)
    72.     {
    73.         Type gameView = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
    74.         PropertyInfo selectedSizeIndex = gameView.GetProperty("selectedSizeIndex", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
    75.         EditorWindow window = EditorWindow.GetWindow(gameView);
    76.         selectedSizeIndex.SetValue(window, index, null);
    77.     }
    78. }
     
    Joness, dhat610, fleity and 2 others like this.
  4. dhat610

    dhat610

    Joined:
    Jun 28, 2022
    Posts:
    1
    thank you for your sharing. Follow you code, I can add new resolution by fixed resolution (width and height) but I can not find the way to add new resolution by aspect ratio. Can you help me? Thanks
     
  5. vazabsky

    vazabsky

    Joined:
    Apr 14, 2022
    Posts:
    1
    instead
    object entry = constructorInfo.Invoke(new object[] { 1, width, height, label });
    put
    object entry = constructorInfo.Invoke(new object[] { 0, width, height, label });