-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameViewResolutionAttribute.cs
110 lines (98 loc) · 3.42 KB
/
GameViewResolutionAttribute.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) 2023 Koji Hasegawa.
// This software is released under the MIT License.
using System;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using UnityEngine;
#if UNITY_EDITOR
using TestHelper.Wrappers.UnityEditor;
using UnityEditor;
#endif
namespace TestHelper.Attributes
{
/// <summary>
/// Set <c>GameView</c> resolution before SetUp test.
/// </summary>
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
public class GameViewResolutionAttribute : NUnitAttribute, IApplyToContext
{
private readonly uint _width;
private readonly uint _height;
private readonly string _name;
/// <summary>
/// Set <c>GameView</c> resolution before SetUp test.
/// </summary>
/// <param name="width">GameView width [px]</param>
/// <param name="height">GameView height [px]</param>
/// <param name="name">GameViewSize name</param>
public GameViewResolutionAttribute(uint width, uint height, string name)
{
_width = width;
_height = height;
_name = name;
}
/// <summary>
/// Set <c>GameView</c> resolution before SetUp test.
/// </summary>
/// <param name="resolution">GameView resolutions enum</param>
public GameViewResolutionAttribute(GameViewResolution resolution)
{
(_width, _height, _name) = resolution.GetParameter();
}
/// <inheritdoc />
public void ApplyToContext(ITestExecutionContext context)
{
#if UNITY_2022_2_OR_NEWER
SetResolutionUsingPlayModeWindow();
#else
SetResolution();
#endif
}
// ReSharper disable once UnusedMember.Local
private void SetResolutionUsingPlayModeWindow()
{
#if UNITY_EDITOR && UNITY_2022_2_OR_NEWER
PlayModeWindow.SetViewType(PlayModeWindow.PlayModeViewTypes.GameView);
PlayModeWindow.SetCustomRenderingResolution(_width, _height, _name);
#endif
}
// ReSharper disable once UnusedMember.Local
private void SetResolution()
{
#if UNITY_EDITOR
var gameViewSizes = GameViewSizesWrapper.CreateInstance();
if (gameViewSizes == null)
{
Debug.LogError("GameViewSizes instance creation failed.");
return;
}
var gameViewSizeGroup = gameViewSizes.CurrentGroup();
if (gameViewSizeGroup == null)
{
Debug.LogError("GameViewSizeGroup instance creation failed.");
return;
}
var gameViewSize = GameViewSizeWrapper.CreateInstance((int)_width, (int)_height, _name);
if (gameViewSize == null)
{
Debug.LogError("GameViewSize instance creation failed.");
return;
}
var index = gameViewSizeGroup.IndexOf(gameViewSize);
if (index == -1)
{
gameViewSizeGroup.AddCustomSize(gameViewSize);
index = gameViewSizeGroup.IndexOf(gameViewSize);
}
var gameView = GameViewWrapper.GetWindow();
if (gameView == null)
{
Debug.LogError("GameView instance creation failed.");
return;
}
gameView.SelectedSizeIndex(index);
#endif
}
}
}