< Summary

Class:GameViewUtils
Assembly:AssetBundlesVisualTestHelpers
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/VisualTests/Editor/GameViewUtils.cs
Covered lines:0
Uncovered lines:60
Coverable lines:60
Total lines:125
Line coverage:0% (0 of 60)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
GameViewUtils()0%2100%
SetSize(...)0%2100%
AddOrGetCustomSize(...)0%6200%
RemoveCustomSize(...)0%2100%
SizeExists(...)0%2100%
FindSize(...)0%20400%
SizeExists(...)0%2100%
FindSize(...)0%20400%
GetGroup(...)0%2100%
GetCurrentGroupType()0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/ABConverter/VisualTests/Editor/GameViewUtils.cs

#LineLine coverage
 1using System;
 2using System.Reflection;
 3using UnityEditor;
 4
 5//NOTE(Brian): Code adapted from this answer https://answers.unity.com/questions/956123/add-and-select-game-view-resolut
 6public static class GameViewUtils
 7{
 8    static object gameViewSizesInstance;
 9    static MethodInfo getGroup;
 10
 11    static GameViewUtils()
 12    {
 013        var sizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
 014        var singleType = typeof(ScriptableSingleton<>).MakeGenericType(sizesType);
 015        var instanceProp = singleType.GetProperty("instance");
 016        getGroup = sizesType.GetMethod("GetGroup");
 017        gameViewSizesInstance = instanceProp.GetValue(null, null);
 018    }
 19
 20    public enum GameViewSizeType
 21    {
 22        AspectRatio, FixedResolution
 23    }
 24
 25    public static void SetSize(int index)
 26    {
 027        var gvWndType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
 28
 029        var selectedSizeIndexProp = gvWndType.GetProperty("selectedSizeIndex",
 30            BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
 31
 032        var gvWnd = EditorWindow.GetWindow(gvWndType);
 33
 034        selectedSizeIndexProp.SetValue(gvWnd, index, null);
 035    }
 36
 37    public static int AddOrGetCustomSize(GameViewSizeType viewSizeType, GameViewSizeGroupType sizeGroupType, int width, 
 38    {
 039        int id = FindSize(sizeGroupType, width, height);
 40
 041        if (id != -1)
 042            return id;
 43
 044        var group = GetGroup(sizeGroupType);
 045        var addCustomSize = getGroup.ReturnType.GetMethod("AddCustomSize"); // or group.GetType().
 046        var gvsType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");
 047        var enumType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizeType");
 048        var ctor = gvsType.GetConstructor(new Type[] { enumType, typeof(int), typeof(int), typeof(string) });
 049        var newSize = ctor.Invoke(new object[] { (int)viewSizeType, width, height, text });
 050        addCustomSize.Invoke(group, new object[] { newSize });
 51
 052        return FindSize(sizeGroupType, width, height);
 53    }
 54
 55    public static void RemoveCustomSize(GameViewSizeGroupType sizeGroupType, int index)
 56    {
 057        var group = GetGroup(sizeGroupType);
 058        var addCustomSize = getGroup.ReturnType.GetMethod("RemoveCustomSize");
 059        addCustomSize.Invoke(group, new object[] { index });
 060    }
 61
 062    public static bool SizeExists(GameViewSizeGroupType sizeGroupType, string text) { return FindSize(sizeGroupType, tex
 63
 64    public static int FindSize(GameViewSizeGroupType sizeGroupType, string text)
 65    {
 066        var group = GetGroup(sizeGroupType);
 067        var getDisplayTexts = group.GetType().GetMethod("GetDisplayTexts");
 068        var displayTexts = getDisplayTexts.Invoke(group, null) as string[];
 069        for (int i = 0; i < displayTexts.Length; i++)
 70        {
 071            string display = displayTexts[i];
 72
 73            // the text we get is "Name (W:H)" if the size has a name, or just "W:H" e.g. 16:9
 74            // so if we're querying a custom size text we substring to only get the name
 75            // You could see the outputs by just logging
 076            int pren = display.IndexOf('(');
 77
 078            if (pren != -1)
 079                display = display.Substring(0, pren - 1); // -1 to remove the space that's before the prens. This is ver
 80
 081            if (display == text)
 082                return i;
 83        }
 84
 085        return -1;
 86    }
 87
 088    public static bool SizeExists(GameViewSizeGroupType sizeGroupType, int width, int height) { return FindSize(sizeGrou
 89
 90    public static int FindSize(GameViewSizeGroupType sizeGroupType, int width, int height)
 91    {
 092        object group = GetGroup(sizeGroupType);
 093        var groupType = group.GetType();
 094        var getBuiltinCount = groupType.GetMethod("GetBuiltinCount");
 095        var getCustomCount = groupType.GetMethod("GetCustomCount");
 096        int sizesCount = (int)getBuiltinCount.Invoke(group, null) + (int)getCustomCount.Invoke(group, null);
 097        var getGameViewSize = groupType.GetMethod("GetGameViewSize");
 098        var gvsType = getGameViewSize.ReturnType;
 099        var widthProp = gvsType.GetProperty("width");
 0100        var heightProp = gvsType.GetProperty("height");
 0101        var indexValue = new object[1];
 102
 0103        for (int i = 0; i < sizesCount; i++)
 104        {
 0105            indexValue[0] = i;
 0106            var size = getGameViewSize.Invoke(group, indexValue);
 0107            int sizeWidth = (int)widthProp.GetValue(size, null);
 0108            int sizeHeight = (int)heightProp.GetValue(size, null);
 109
 0110            if (sizeWidth == width && sizeHeight == height)
 0111                return i;
 112        }
 113
 0114        return -1;
 115    }
 116
 0117    static object GetGroup(GameViewSizeGroupType type) { return getGroup.Invoke(gameViewSizesInstance, new object[] { (i
 118
 119    public static GameViewSizeGroupType GetCurrentGroupType()
 120    {
 0121        var getCurrentGroupTypeProp = gameViewSizesInstance.GetType().GetProperty("currentGroupType");
 122
 0123        return (GameViewSizeGroupType)(int)getCurrentGroupTypeProp.GetValue(gameViewSizesInstance, null);
 124    }
 125}