Skip to content

Commit b8ae2d6

Browse files
authored
fix: use Game View size tool in simulate mouse E2E (#1833)
1 parent 6cc54a0 commit b8ae2d6

1 file changed

Lines changed: 20 additions & 159 deletions

File tree

scripts/test-simulate-mouse-demo.sh

Lines changed: 20 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ set -eu
66
SCENE_PATH="Assets/Scenes/SimulateMouseDemoScene.unity"
77
TMP_DIR="${TMPDIR:-/tmp}/unity-cli-loop-simulate-mouse"
88
ELEMENTS_JSON="$TMP_DIR/simulate-mouse-elements.json"
9-
ORIGINAL_GAME_VIEW_SIZE_INDEX=""
9+
ORIGINAL_GAME_VIEW_WIDTH=""
10+
ORIGINAL_GAME_VIEW_HEIGHT=""
1011
PROJECT_PATH=""
1112
ULOOP_PATH="${ULOOP_BIN:-uloop}"
1213

@@ -17,8 +18,8 @@ fail() {
1718

1819
cleanup() {
1920
run_uloop control-play-mode --action Stop >/dev/null 2>&1 || true
20-
if [ -n "${ORIGINAL_GAME_VIEW_SIZE_INDEX:-}" ]; then
21-
restore_game_view_size_index "$ORIGINAL_GAME_VIEW_SIZE_INDEX" >/dev/null 2>&1 || true
21+
if [ -n "${ORIGINAL_GAME_VIEW_WIDTH:-}" ] && [ -n "${ORIGINAL_GAME_VIEW_HEIGHT:-}" ]; then
22+
restore_game_view_size "$ORIGINAL_GAME_VIEW_WIDTH" "$ORIGINAL_GAME_VIEW_HEIGHT" >/dev/null 2>&1 || true
2223
fi
2324
}
2425
trap cleanup EXIT INT TERM
@@ -133,164 +134,25 @@ return SceneManager.GetActiveScene().path;
133134
}
134135

135136
select_full_hd_game_view() {
136-
# Unity exposes no public setter for the Game View resolution dropdown.
137-
# This matches the manual "Full HD (1920x1080)" selection before reading UI coordinates.
138-
code='
139-
using System;
140-
using System.Reflection;
141-
using UnityEditor;
142-
using UnityEngine;
143-
const int Width = 1920;
144-
const int Height = 1080;
145-
EditorApplication.ExecuteMenuItem("Window/General/Game");
146-
Type gameViewType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
147-
Debug.Assert(gameViewType != null, "GameView type must exist.");
148-
UnityEngine.Object[] gameViews = Resources.FindObjectsOfTypeAll(gameViewType);
149-
EditorWindow gameView = null;
150-
for (int i = 0; i < gameViews.Length; i++)
151-
{
152-
EditorWindow candidate = gameViews[i] as EditorWindow;
153-
if (candidate == null)
154-
{
155-
continue;
156-
}
157-
if (gameView == null || candidate.hasFocus)
158-
{
159-
gameView = candidate;
160-
}
161-
}
162-
if (gameView == null)
163-
{
164-
gameView = EditorWindow.GetWindow(gameViewType);
165-
}
166-
gameView.Show();
167-
Type gameViewSizesType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizes");
168-
Debug.Assert(gameViewSizesType != null, "GameViewSizes type must exist.");
169-
Type singletonType = typeof(ScriptableSingleton<>).MakeGenericType(gameViewSizesType);
170-
PropertyInfo instanceProperty = singletonType.GetProperty("instance", BindingFlags.Public | BindingFlags.Static);
171-
Debug.Assert(instanceProperty != null, "GameViewSizes instance property must exist.");
172-
object gameViewSizes = instanceProperty.GetValue(null);
173-
MethodInfo getGroupMethod = gameViewSizesType.GetMethod("GetGroup", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
174-
Debug.Assert(getGroupMethod != null, "GameViewSizes.GetGroup must exist.");
175-
object group = getGroupMethod.Invoke(gameViewSizes, new object[] { GameViewSizeGroupType.Standalone });
176-
Debug.Assert(group != null, "Standalone GameViewSize group must exist.");
177-
Type groupType = group.GetType();
178-
MethodInfo getDisplayTextsMethod = groupType.GetMethod("GetDisplayTexts", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
179-
Debug.Assert(getDisplayTextsMethod != null, "GameViewSizeGroup.GetDisplayTexts must exist.");
180-
string[] displayTexts = (string[])getDisplayTextsMethod.Invoke(group, null);
181-
int selectedIndex = -1;
182-
for (int i = 0; i < displayTexts.Length; i++)
183-
{
184-
if (displayTexts[i].Contains("1920x1080") || displayTexts[i].Contains("Full HD"))
185-
{
186-
selectedIndex = i;
187-
break;
188-
}
189-
}
190-
if (selectedIndex < 0)
191-
{
192-
Type gameViewSizeType = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSize");
193-
Debug.Assert(gameViewSizeType != null, "GameViewSize type must exist.");
194-
Type gameViewSizeTypeEnum = typeof(Editor).Assembly.GetType("UnityEditor.GameViewSizeType");
195-
Debug.Assert(gameViewSizeTypeEnum != null, "GameViewSizeType enum must exist.");
196-
object fixedResolution = Enum.Parse(gameViewSizeTypeEnum, "FixedResolution");
197-
ConstructorInfo constructor = gameViewSizeType.GetConstructor(new Type[] { gameViewSizeTypeEnum, typeof(int), typeof(int), typeof(string) });
198-
Debug.Assert(constructor != null, "GameViewSize constructor must exist.");
199-
object selectedGameViewSize = constructor.Invoke(new object[] { fixedResolution, Width, Height, "Full HD" });
200-
MethodInfo addCustomSizeMethod = groupType.GetMethod("AddCustomSize", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
201-
Debug.Assert(addCustomSizeMethod != null, "GameViewSizeGroup.AddCustomSize must exist.");
202-
addCustomSizeMethod.Invoke(group, new object[] { selectedGameViewSize });
203-
displayTexts = (string[])getDisplayTextsMethod.Invoke(group, null);
204-
selectedIndex = displayTexts.Length - 1;
205-
}
206-
PropertyInfo selectedSizeIndexProperty = gameViewType.GetProperty("selectedSizeIndex", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
207-
Debug.Assert(selectedSizeIndexProperty != null, "GameView.selectedSizeIndex must exist.");
208-
selectedSizeIndexProperty.SetValue(gameView, selectedIndex, null);
209-
Screen.SetResolution(Width, Height, false);
210-
gameView.Repaint();
211-
Vector2 currentGameViewSize = Handles.GetMainGameViewSize();
212-
return displayTexts[selectedIndex] + " / " + currentGameViewSize.x + "x" + currentGameViewSize.y;
213-
'
214-
215-
json=$(run_uloop_json execute-dynamic-code --code "$code")
216-
assert_json_success "$json" "Select Full HD Game View"
217-
printf ' %s\n' "$(printf '%s\n' "$json" | jq -r '.Result')"
137+
json=$(run_uloop_json set-game-view-size --width 1920 --height 1080)
138+
assert_json_success "$json" "Set Full HD Game View"
139+
ORIGINAL_GAME_VIEW_WIDTH=$(printf '%s\n' "$json" | jq -r '.PreviousWidth')
140+
ORIGINAL_GAME_VIEW_HEIGHT=$(printf '%s\n' "$json" | jq -r '.PreviousHeight')
141+
current_width=$(printf '%s\n' "$json" | jq -r '.CurrentWidth')
142+
current_height=$(printf '%s\n' "$json" | jq -r '.CurrentHeight')
143+
assert_text_equals "$current_width" "1920" "Game View width"
144+
assert_text_equals "$current_height" "1080" "Game View height"
145+
printf ' %sx%s (previous: %sx%s)\n' \
146+
"$current_width" "$current_height" \
147+
"$ORIGINAL_GAME_VIEW_WIDTH" "$ORIGINAL_GAME_VIEW_HEIGHT"
218148
sleep 1
219149
}
220150

221-
capture_game_view_size_index() {
222-
code='
223-
using System;
224-
using System.Reflection;
225-
using UnityEditor;
226-
using UnityEngine;
227-
Type gameViewType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
228-
Debug.Assert(gameViewType != null, "GameView type must exist.");
229-
UnityEngine.Object[] gameViews = Resources.FindObjectsOfTypeAll(gameViewType);
230-
EditorWindow gameView = null;
231-
for (int i = 0; i < gameViews.Length; i++)
232-
{
233-
EditorWindow candidate = gameViews[i] as EditorWindow;
234-
if (candidate == null)
235-
{
236-
continue;
237-
}
238-
if (gameView == null || candidate.hasFocus)
239-
{
240-
gameView = candidate;
241-
}
242-
}
243-
if (gameView == null)
244-
{
245-
gameView = EditorWindow.GetWindow(gameViewType);
246-
}
247-
PropertyInfo selectedSizeIndexProperty = gameViewType.GetProperty("selectedSizeIndex", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
248-
Debug.Assert(selectedSizeIndexProperty != null, "GameView.selectedSizeIndex must exist.");
249-
return selectedSizeIndexProperty.GetValue(gameView, null).ToString();
250-
'
251-
252-
json=$(run_uloop_json execute-dynamic-code --code "$code")
253-
assert_json_success "$json" "Capture Game View size index"
254-
printf '%s\n' "$json" | jq -r '.Result'
255-
}
256-
257-
restore_game_view_size_index() {
258-
selected_index=$1
259-
code="
260-
using System;
261-
using System.Reflection;
262-
using UnityEditor;
263-
using UnityEngine;
264-
int selectedIndex = $selected_index;
265-
Debug.Assert(selectedIndex >= 0, \"GameView size index must be non-negative.\");
266-
Type gameViewType = typeof(Editor).Assembly.GetType(\"UnityEditor.GameView\");
267-
Debug.Assert(gameViewType != null, \"GameView type must exist.\");
268-
UnityEngine.Object[] gameViews = Resources.FindObjectsOfTypeAll(gameViewType);
269-
EditorWindow gameView = null;
270-
for (int i = 0; i < gameViews.Length; i++)
271-
{
272-
EditorWindow candidate = gameViews[i] as EditorWindow;
273-
if (candidate == null)
274-
{
275-
continue;
276-
}
277-
if (gameView == null || candidate.hasFocus)
278-
{
279-
gameView = candidate;
280-
}
281-
}
282-
if (gameView == null)
283-
{
284-
gameView = EditorWindow.GetWindow(gameViewType);
285-
}
286-
PropertyInfo selectedSizeIndexProperty = gameViewType.GetProperty(\"selectedSizeIndex\", BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
287-
Debug.Assert(selectedSizeIndexProperty != null, \"GameView.selectedSizeIndex must exist.\");
288-
selectedSizeIndexProperty.SetValue(gameView, selectedIndex, null);
289-
gameView.Repaint();
290-
return selectedIndex.ToString();
291-
"
292-
293-
run_uloop_json execute-dynamic-code --code "$code"
151+
restore_game_view_size() {
152+
original_width=$1
153+
original_height=$2
154+
json=$(run_uloop_json set-game-view-size --width "$original_width" --height "$original_height")
155+
assert_json_success "$json" "Restore Game View resolution"
294156
}
295157

296158
capture_annotated_elements() {
@@ -557,7 +419,6 @@ wait_play_mode
557419
sleep 1
558420

559421
printf '[3/8] Selecting Full HD Game View resolution...\n'
560-
ORIGINAL_GAME_VIEW_SIZE_INDEX=$(capture_game_view_size_index)
561422
select_full_hd_game_view
562423

563424
printf '[4/8] Reading annotated UI coordinates...\n'

0 commit comments

Comments
 (0)