Skip to content

Instantly share code, notes, and snippets.

@20chan
Created August 21, 2019 02:58
Show Gist options
  • Select an option

  • Save 20chan/5c192bcfe8ba2ce57e2b393af59136ae to your computer and use it in GitHub Desktop.

Select an option

Save 20chan/5c192bcfe8ba2ce57e2b393af59136ae to your computer and use it in GitHub Desktop.
Unity take screenshots for each resolutions editor script
using System;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
public static class GameViewResolution {
static object gameViewSizesInstance;
static MethodInfo getGroup;
static GameViewResolution() {
// gameViewSizesInstance = ScriptableSingleton<GameViewSizes>.instance;
var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
var instanceProp = singleType.GetProperty("instance");
getGroup = sizesType.GetMethod("GetGroup");
gameViewSizesInstance = instanceProp.GetValue(null, null);
}
public enum GameViewSizeType {
AspectRatio,
FixedResolution
}
public static void SetSize(int index) {
var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var gvWnd = EditorWindow.GetWindow(gvWndType);
selectedSizeIndexProp.SetValue(gvWnd, index, null);
}
public static void AddCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width,
int height, string text) {
// GameViewSizes group = gameViewSizesInstance.GetGroup(sizeGroupTyge);
// group.AddCustomSize(new GameViewSize(viewSizeType, width, height, text);
var group = GetGroup(sizeGroupType);
var addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize"); // or group.GetType().
var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");
var ctor = gvsType.GetConstructors()[0];
var newSize = ctor.Invoke(new object[] { (int)viewSizeType, width, height, text });
addCustomSize.Invoke(group, new object[] { newSize });
}
public static int FindSize(GameViewSizeGroupType sizeGroupType, string text) {
var group = GetGroup(sizeGroupType);
var getDisplayTexts = group.GetType().GetMethod("GetDisplayTexts");
var displayTexts = getDisplayTexts.Invoke(group, null) as string[];
for (int i = 0; i < displayTexts.Length; i++) {
string display = displayTexts[i];
int pren = display.IndexOf('(');
if (pren != -1)
display = display.Substring(0,
pren - 1); // -1 to remove the space that's before the prens. This is very implementation-depdenent
if (display == text)
return i;
}
return -1;
}
public static bool SizeExists(GameViewSizeGroupType sizeGroupType, int width, int height) {
return FindSize(sizeGroupType, width, height) != -1;
}
public static int FindSize(GameViewSizeGroupType sizeGroupType, int width, int height) {
var group = GetGroup(sizeGroupType);
var groupType = group.GetType();
var getBuiltinCount = groupType.GetMethod("GetBuiltinCount");
var getCustomCount = groupType.GetMethod("GetCustomCount");
int sizesCount = (int)getBuiltinCount.Invoke(group, null) + (int)getCustomCount.Invoke(group, null);
var getGameViewSize = groupType.GetMethod("GetGameViewSize");
var gvsType = getGameViewSize.ReturnType;
var widthProp = gvsType.GetProperty("width");
var heightProp = gvsType.GetProperty("height");
var indexValue = new object[1];
for (int i = 0; i < sizesCount; i++) {
indexValue[0] = i;
var size = getGameViewSize.Invoke(group, indexValue);
int sizeWidth = (int)widthProp.GetValue(size, null);
int sizeHeight = (int)heightProp.GetValue(size, null);
if (sizeWidth == width && sizeHeight == height)
return i;
}
return -1;
}
static object GetGroup(GameViewSizeGroupType type) {
return getGroup.Invoke(gameViewSizesInstance, new object[] { (int)type });
}
public static GameViewSizeGroupType GetCurrentGroupType() {
var getCurrentGroupTypeProp = gameViewSizesInstance.GetType().GetProperty("currentGroupType");
return (GameViewSizeGroupType)(int)getCurrentGroupTypeProp.GetValue(gameViewSizesInstance, null);
}
public static void SetGamePause(bool pause) {
var editor = typeof(Editor).Assembly.GetType("UnityEditor.EditorApplication");
editor.GetProperty("isPaused").SetValue(null, pause, null);
}
private static int[][] storeResolutions = {
new[] { 1242, 2688 },
new[] { 1242, 2208 },
new[] { 2048, 2732 },
};
private static string[] resolutionNames = {
"6.5",
"5.5",
"12.9",
};
[MenuItem("ScreenShot/Take Screenshots %F12")]
public static void TakeScreenshots() {
var name = DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
var group = GetCurrentGroupType();
SetGamePause(false);
int resIdx = 0;
bool sizeChanged = false;
EditorApplication.update += TakeShot;
void TakeShot() {
var res = storeResolutions[resIdx];
if (!sizeChanged) {
if (FindSize(group, res[0], res[1]) == -1) {
AddCustomSize(GameViewSizeType.FixedResolution, group, res[0], res[1], "");
}
var i = FindSize(group, res[0], res[1]);
SetSize(i);
sizeChanged = true;
}
else {
sizeChanged = false;
var directory = Path.Combine(Directory.GetParent(Application.dataPath).FullName, "Temp", "Screenshots", resolutionNames[resIdx]);
if (!Directory.Exists(directory)) {
Directory.CreateDirectory(directory);
}
var path = Path.Combine(directory, $"{name}.png");
resIdx++;
ScreenCapture.CaptureScreenshot(path);
Debug.Log(path);
if (resIdx == storeResolutions.Length) {
EditorApplication.update -= TakeShot;
}
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment