Skip to content

Instantly share code, notes, and snippets.

@noisecrime
Created November 10, 2016 19:42
Show Gist options
  • Save noisecrime/62251eb5fda4e086460dfbd4e3531775 to your computer and use it in GitHub Desktop.
Save noisecrime/62251eb5fda4e086460dfbd4e3531775 to your computer and use it in GitHub Desktop.
Editor only script that enables adding of bespoke Game resolutions to the GameView dropdown selector. Support to create landscape and portrait resolutions, different resolutions for different platforms etc.
// Based on
// http://answers.unity3d.com/questions/956123/add-and-select-game-view-resolution.html
using System;
using UnityEditor;
using UnityEngine;
namespace NoiseCrime.Editor.Tools
{
public static class GameViewAddResolutions
{
public enum GameViewSizeType { AspectRatio, FixedResolution }
[MenuItem( "Window/NoiseCrimeMethods/GameViewResolutions/RemoveAddAllPlatforms" )]
public static void RemoveThenAddAllPlatformSizes()
{
GameViewResolutionsRemoveAll();
GameViewResolutionsStandalone();
GameViewResolutionsIOS();
GameViewResolutionsAndroid();
}
[MenuItem( "Window/NoiseCrimeMethods/GameViewResolutions/AddStandalone" )]
public static void GameViewResolutionsStandalone()
{
AddDesktopFixedResolutionsToGroup(GameViewSizeGroupType.Standalone);
AddMobileFixedResolutionsToGroup(GameViewSizeGroupType.Standalone);
SaveCustomSizes();
}
[MenuItem( "Window/NoiseCrimeMethods/GameViewResolutions/AddIOS" )]
public static void GameViewResolutionsIOS()
{
AddMobileFixedResolutionsToGroup(GameViewSizeGroupType.iOS);
SaveCustomSizes();
}
[MenuItem( "Window/NoiseCrimeMethods/GameViewResolutions/AddAndroid" )]
public static void GameViewResolutionsAndroid()
{
AddMobileFixedResolutionsToGroup(GameViewSizeGroupType.Android);
SaveCustomSizes();
}
[MenuItem( "Window/NoiseCrimeMethods/GameViewResolutions/RemoveAll" )]
public static void GameViewResolutionsRemoveAll()
{
RemoveCustomSizes(GameViewSizeGroupType.Standalone);
RemoveCustomSizes(GameViewSizeGroupType.iOS);
RemoveCustomSizes(GameViewSizeGroupType.Android);
SaveCustomSizes();
}
static void AddDesktopFixedResolutionsToGroup( GameViewSizeGroupType sizeGroupType )
{
AddFixedResolutionDual( sizeGroupType, 1280, 720, "HDR" );
AddFixedResolutionDual( sizeGroupType, 1920, 1080, "HD" );
AddCustomSize( GameViewSizeType.FixedResolution, sizeGroupType, 3440, 1440, "UWHD Landscape" );
AddCustomSize( GameViewSizeType.FixedResolution, sizeGroupType, 1024, 1024, "Square" );
AddCustomSize( GameViewSizeType.FixedResolution, sizeGroupType, 1201, 1464, "Oculus Rift" );
// Custom Clients
AddCustomSize( GameViewSizeType.FixedResolution, sizeGroupType, 3840, 1200, "CTFull" );
AddCustomSize( GameViewSizeType.FixedResolution, sizeGroupType, 1920, 600, "CTHalf" );
}
static void AddMobileFixedResolutionsToGroup( GameViewSizeGroupType sizeGroupType )
{
AddFixedResolutionDual( sizeGroupType, 1280, 800, "Mobile" );
AddFixedResolutionDual( sizeGroupType, 960, 540, "Lumia 535" );
AddFixedResolutionDual( sizeGroupType, 1920, 1200, "Nexus 7" );
AddFixedResolutionDual( sizeGroupType, 1280, 720, "Moto G" );
AddFixedResolutionDual( sizeGroupType, 1024, 768, "iPad 2" );
AddFixedResolutionDual( sizeGroupType, 2048, 1536, "iPad Pro 9.7" );
AddFixedResolutionDual( sizeGroupType, 2732, 2048, "iPad Pro 12.9" );
AddFixedResolutionDual( sizeGroupType, 480, 320, "iPhone" );
AddFixedResolutionDual( sizeGroupType, 960, 640, "iPhone 4" );
AddFixedResolutionDual( sizeGroupType, 1136, 640, "iPhone 5" );
AddFixedResolutionDual( sizeGroupType, 1334, 750, "iPhone 6" );
AddFixedResolutionDual( sizeGroupType, 1920, 1080, "iPhone 6+" ); // 1242 x 2208 downsampled
AddFixedResolutionDual( sizeGroupType, 2208, 1242, "iPhone 5.5" ); // 1242 x 2208 downsampled
}
static void AddFixedResolutionDual( GameViewSizeGroupType sizeGroupType, int width, int height, string text )
{
AddCustomSize( GameViewSizeType.FixedResolution, sizeGroupType, width, height, text + " Landscape" );
AddCustomSize( GameViewSizeType.FixedResolution, sizeGroupType, height, width, text + " Portrait" );
}
static void SaveCustomSizes()
{
var asm = typeof(UnityEditor.Editor).Assembly;
var sizesType = asm.GetType("UnityEditor.GameViewSizes");
var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
var instanceProp = singleType.GetProperty("instance");
var saveToHDD = sizesType.GetMethod("SaveToHDD");
var instance = instanceProp.GetValue(null, null);
saveToHDD.Invoke(instance, null);
}
static void AddCustomSize( GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, int height, string text )
{
// goal:
// var group = ScriptableSingleton<GameViewSizes>.instance.GetGroup(sizeGroupType);
// group.AddCustomSize(new GameViewSize(viewSizeType, width, height, text);
var asm = typeof(UnityEditor.Editor).Assembly;
var sizesType = asm.GetType("UnityEditor.GameViewSizes");
var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
var instanceProp = singleType.GetProperty("instance");
var getGroup = sizesType.GetMethod("GetGroup");
var instance = instanceProp.GetValue(null, null);
var group = getGroup.Invoke(instance, new object[] { (int)sizeGroupType });
var addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize"); // or group.GetType().
var gvsType = asm.GetType("UnityEditor.GameViewSize");
var ctor = gvsType.GetConstructor(new Type[] { typeof(int), typeof(int), typeof(int), typeof(string) });
var newSize = ctor.Invoke(new object[] { (int)viewSizeType, width, height, text });
addCustomSize.Invoke( group, new object[] { newSize } );
}
static void RemoveCustomSizes( GameViewSizeGroupType sizeGroupType )
{
var asm = typeof(UnityEditor.Editor).Assembly;
var sizesType = asm.GetType("UnityEditor.GameViewSizes");
var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
var instanceProp = singleType.GetProperty("instance");
var getGroup = sizesType.GetMethod("GetGroup");
var instance = instanceProp.GetValue(null, null);
var group = getGroup.Invoke(instance, new object[] { (int)sizeGroupType });
var getCustomCount = getGroup.ReturnType.GetMethod("GetCustomCount"); // or group.GetType().
var getBuiltinCount = getGroup.ReturnType.GetMethod("GetBuiltinCount"); // or group.GetType().
var removeCustomSize = getGroup.ReturnType.GetMethod("RemoveCustomSize"); // or group.GetType().
int customCount = (int) getCustomCount.Invoke(group, null);
int builtinCount = (int) getBuiltinCount.Invoke(group, null);
Debug.Log("RemoveCustomSizes for Group: " + sizeGroupType.ToString() + " Count: " + customCount + " BuiltIn: " + builtinCount);
for ( int i = 0; i < customCount; i++ )
{
removeCustomSize.Invoke( group, new object[] { builtinCount + (customCount - i - 1) } );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment