From 50dc5e45d1b2f84b6df14d2bb61e0f93f6f5917d Mon Sep 17 00:00:00 2001 From: Kuniwak Date: Wed, 8 Nov 2023 09:53:56 +0900 Subject: [PATCH] Screenshot feature --- Editor/Localization/ja.po | 66 ++++++++ Editor/UI/Agents/UGUIMonkeyAgentEditor.cs | 184 ++++++++++++++++++++++ Runtime/Agents/UGUIMonkeyAgent.cs | 38 ++++- 3 files changed, 287 insertions(+), 1 deletion(-) diff --git a/Editor/Localization/ja.po b/Editor/Localization/ja.po index 75233f7..43bbbed 100644 --- a/Editor/Localization/ja.po +++ b/Editor/Localization/ja.po @@ -311,6 +311,72 @@ msgstr "最小文字列長" msgid "Maximum Length" msgstr "最大文字列長" +# screenshot options +msgid "Screenshot Options" +msgstr "スクリーンショット設定" + +# screenshot options enabled +msgid "Enabled" +msgstr "有効" + +# screenshot options enabled tooltip +msgid "Whether screenshot is enabled or not" +msgstr "スクリーンショットが有効かどうか" + +# screenshot directory +msgid "Directory" +msgstr "ディレクトリ" + +# screenshot directory path +msgid "Path" +msgstr "パス" + +# screenshot directory path tooltip +msgid "Directory path to save screenshots" +msgstr "スクリーンショットの保存先ディレクトリのパス" + +# screenshot directory use default +msgid "Use Default" +msgstr "デフォルト値を使用" + +# screenshot directory use default tooltip +msgid "Whether using a default directory path to save screenshots or specifying it manually. Default value is Application.persistentDataPath + \"/TestHelper.Monkey/Screenshots/\"" +msgstr "スクリーンショットの保存先のディレクトリ名にデフォルト値を使うかどうか。デフォルト値は Application.persistentDataPath + \"/TestHelper.Monkey/Screenshots/\"" + +# screenshot filename +msgid "Filename" +msgstr "ファイル名" + +# screenshot filename prefix +msgid "Prefix" +msgstr "プレフィックス" + +# screenshot filename prefix tooltip +msgid "Prefix of screenshots filename" +msgstr "スクリーンショットのファイル名のプレフィックス" + +# screenshot filename prefix use default +msgid "Use Default" +msgstr "デフォルト値を使用" + +# screenshot filename prefix use default tooltip +msgid "Whether using a default prefix of screenshots filename or specifying it manually. Default value is CurrentTest.Name if run in test-framework context. Otherwise, a caller method name" +msgstr "スクリーンショットのファイル名のプレフィックスにデフォルト値を使うかどうか。デフォルト値はもしテストフレームワークのからの実行であれば CurrentTest.Name、そうでなければ呼び出し元の関数名" + +# screenshot super size +msgid "Super Size" +msgstr "拡大係数" + +msgid "The factor to increase resolution with. Neither this nor Stereo Capture Mode can be specified" +msgstr "解像度をあげるための係数。ステレオキャプチャモードと同時には設定できません" + +# screenshot stereo capture mode +msgid "Stereo Capture Mode" +msgstr "ステレオキャプチャモード" + +msgid "The eye texture to capture when stereo rendering is enabled. Neither this nor Resolution Factor can be specified" +msgstr "ステレオレンダリングが有効な場合は指定した眼のテクスチャ。拡大係数と同時には設定できません" + #: Editor/UI/Agents/UGUIPlaybackAgentEditor.cs # recordedJson diff --git a/Editor/UI/Agents/UGUIMonkeyAgentEditor.cs b/Editor/UI/Agents/UGUIMonkeyAgentEditor.cs index dc723c3..8465b7e 100644 --- a/Editor/UI/Agents/UGUIMonkeyAgentEditor.cs +++ b/Editor/UI/Agents/UGUIMonkeyAgentEditor.cs @@ -75,6 +75,66 @@ public class UGUIMonkeyAgentEditor : UnityEditor.Editor private GUIContent _randomStringParamsEntryMinLengthGUIContent; private GUIContent _randomStringParamsEntryMaxLengthGUIContent; + private static readonly string s_screenshotOptions = L10n.Tr("Screenshot Options"); + private static readonly string s_screenshotEnabled = L10n.Tr("Enabled"); + private static readonly string s_screenshotEnabledTooltip = L10n.Tr("Whether screenshot is enabled or not"); + private SerializedProperty _screenshotEnabledProp; + private GUIContent _screenshotEnabledGUIContent; + + private static readonly string s_screenshotDirectory = L10n.Tr("Directory"); + private static readonly string s_screenshotDefaultDirectory = L10n.Tr("Use Default"); + + private static readonly string s_screenshotDefaultDirectoryTooltip = L10n.Tr( + @"Whether using a default directory path to save screenshots or specifying it manually. Default value is Application.persistentDataPath + ""/TestHelper.Monkey/Screenshots/""" + ); + + private static readonly string s_screenshotDirectoryPath = L10n.Tr("Path"); + private static readonly string s_screenshotDirectoryPathTooltip = L10n.Tr("Directory path to save screenshots"); + private SerializedProperty _screenshotDefaultDirectoryProp; + private GUIContent _screenshotDefaultDirectoryGUIContent; + private SerializedProperty _screenshotDirectoryProp; + private GUIContent _screenshotDirectoryGUIContent; + + private static readonly string s_screenshotFilename = L10n.Tr("Filename"); + private static readonly string s_screenshotDefaultFilenamePrefix = L10n.Tr("Use Default"); + + private static readonly string s_screenshotDefaultFilenamePrefixTooltip = L10n.Tr( + "Whether using a default prefix of screenshots filename or specifying it manually. Default value is CurrentTest.Name if run in test-framework context. Otherwise, a caller method name" + ); + + private static readonly string s_screenshotFilenamePrefix = L10n.Tr("Prefix"); + private static readonly string s_screenshotFilenamePrefixTooltip = L10n.Tr("Prefix of screenshots filename"); + private SerializedProperty _screenshotDefaultFilenamePrefixProp; + private GUIContent _screenshotDefaultFilenamePrefixGUIContent; + private SerializedProperty _screenshotFilenamePrefixProp; + private GUIContent _screenshotFilenamePrefixGUIContent; + + private static readonly string s_screenshotSuperSize = L10n.Tr("Super Size"); + + private static readonly string s_screenshotSuperSizeTooltip = L10n.Tr( + "The factor to increase resolution with. Neither this nor Stereo Capture Mode can be specified" + ); + + private SerializedProperty _screenshotSuperSizeProp; + private GUIContent _screenshotSuperSizeGUIContent; + private GUIContent _screenshotSuperSizeGUIContentDisabled; + + private static readonly string s_screenshotStereoCaptureMode = L10n.Tr("Stereo Capture Mode"); + + private static readonly string s_screenshotStereoCaptureModeTooltip = + L10n.Tr( + "The eye texture to capture when stereo rendering is enabled. Neither this nor Resolution Factor can be specified" + ); + + private SerializedProperty _screenshotStereoCaptureModeProp; + private GUIContent _screenshotStereoCaptureModeGUIContent; + private GUIContent _screenshotStereoCaptureModeGUIContentDisabled; + + private const int DefaultSuperSize = 1; + + private const ScreenCapture.StereoScreenCaptureMode DefaultStereoScreenCaptureMode = + ScreenCapture.StereoScreenCaptureMode.LeftEye; + private void OnEnable() { @@ -162,6 +222,49 @@ private void Initialize() ); } }; + + _screenshotEnabledProp = serializedObject.FindProperty(nameof(UGUIMonkeyAgent.screenshotEnabled)); + _screenshotEnabledGUIContent = new GUIContent(s_screenshotEnabled, s_screenshotEnabledTooltip); + + _screenshotDefaultDirectoryProp = + serializedObject.FindProperty(nameof(UGUIMonkeyAgent.defaultScreenshotDirectory)); + _screenshotDefaultDirectoryGUIContent = new GUIContent( + s_screenshotDefaultDirectory, + s_screenshotDefaultDirectoryTooltip + ); + _screenshotDirectoryProp = serializedObject.FindProperty(nameof(UGUIMonkeyAgent.screenshotDirectory)); + _screenshotDirectoryGUIContent = new GUIContent( + s_screenshotDirectoryPath, + s_screenshotDirectoryPathTooltip + ); + + _screenshotDefaultFilenamePrefixProp = + serializedObject.FindProperty(nameof(UGUIMonkeyAgent.defaultScreenshotFilenamePrefix)); + _screenshotDefaultFilenamePrefixGUIContent = new GUIContent( + s_screenshotDefaultFilenamePrefix, + s_screenshotDefaultFilenamePrefixTooltip + ); + _screenshotFilenamePrefixProp = + serializedObject.FindProperty(nameof(UGUIMonkeyAgent.screenshotFilenamePrefix)); + _screenshotFilenamePrefixGUIContent = new GUIContent( + s_screenshotFilenamePrefix, + s_screenshotFilenamePrefixTooltip + ); + + _screenshotSuperSizeProp = serializedObject.FindProperty(nameof(UGUIMonkeyAgent.screenshotSuperSize)); + _screenshotSuperSizeGUIContent = new GUIContent(s_screenshotSuperSize, s_screenshotSuperSizeTooltip); + _screenshotSuperSizeGUIContentDisabled = new GUIContent(DefaultSuperSize.ToString()); + + _screenshotStereoCaptureModeProp = + serializedObject.FindProperty(nameof(UGUIMonkeyAgent.screenshotStereoCaptureMode)); + _screenshotStereoCaptureModeGUIContent = new GUIContent( + s_screenshotStereoCaptureMode, + s_screenshotStereoCaptureModeTooltip + ); + _screenshotStereoCaptureModeGUIContentDisabled = new GUIContent( + DefaultStereoScreenCaptureMode.ToString(), + s_screenshotStereoCaptureModeTooltip + ); } @@ -177,8 +280,89 @@ public override void OnInspectorGUI() EditorGUILayout.PropertyField(_touchAndHoldDelayMillisProp, _touchAndHoldDelayMillisGUIContent); EditorGUILayout.PropertyField(_gizmosProp, _gizmosGUIContent); _randomStringParamsMapList.DoLayoutList(); + EditorGUILayout.LabelField(s_screenshotOptions, EditorStyles.boldLabel); + using (new EditorGUI.IndentLevelScope()) + { + EditorGUILayout.PropertyField(_screenshotEnabledProp, _screenshotEnabledGUIContent); + if (_screenshotEnabledProp.boolValue) + { + EditorGUILayout.LabelField(s_screenshotDirectory, EditorStyles.boldLabel); + using (new EditorGUI.IndentLevelScope()) + { + EditorGUILayout.PropertyField( + _screenshotDefaultDirectoryProp, + _screenshotDefaultDirectoryGUIContent + ); + if (!_screenshotDefaultDirectoryProp.boolValue) + { + EditorGUILayout.PropertyField(_screenshotDirectoryProp, _screenshotDirectoryGUIContent); + } + } + + EditorGUILayout.LabelField(s_screenshotFilename, EditorStyles.boldLabel); + using (new EditorGUI.IndentLevelScope()) + { + EditorGUILayout.PropertyField( + _screenshotDefaultFilenamePrefixProp, + _screenshotDefaultFilenamePrefixGUIContent + ); + if (!_screenshotDefaultFilenamePrefixProp.boolValue) + { + EditorGUILayout.PropertyField( + _screenshotFilenamePrefixProp, + _screenshotFilenamePrefixGUIContent + ); + } + } + + var screenCaptureMode = + (ScreenCapture.StereoScreenCaptureMode)_screenshotStereoCaptureModeProp.enumValueIndex; + if (screenCaptureMode == DefaultStereoScreenCaptureMode) + { + _screenshotSuperSizeProp.intValue = EditorGUILayout.IntSlider( + _screenshotSuperSizeGUIContent, + _screenshotSuperSizeProp.intValue, + 1, + 100 + ); + } + else + { + EditorGUILayout.LabelField( + _screenshotSuperSizeGUIContent, + _screenshotSuperSizeGUIContentDisabled + ); + } + + if (_screenshotSuperSizeProp.intValue == DefaultSuperSize) + { + var screenshotStereoCaptureMode = + (ScreenCapture.StereoScreenCaptureMode)EditorGUILayout.EnumPopup( + _screenshotStereoCaptureModeGUIContent, + (ScreenCapture.StereoScreenCaptureMode)_screenshotStereoCaptureModeProp + .enumValueIndex + ); + _screenshotStereoCaptureModeProp.enumValueIndex = Clamp( + (int)screenshotStereoCaptureMode, + 1, + 3 + ); + } + else + { + EditorGUILayout.LabelField( + _screenshotStereoCaptureModeGUIContent, + _screenshotStereoCaptureModeGUIContentDisabled + ); + } + } + } serializedObject.ApplyModifiedProperties(); } + + + // XXX: Fallback for Unity 2019.x + private static int Clamp(int value, int min, int max) => value < min ? min : max < value ? max : value; } } diff --git a/Runtime/Agents/UGUIMonkeyAgent.cs b/Runtime/Agents/UGUIMonkeyAgent.cs index 9e57ad7..61242e3 100644 --- a/Runtime/Agents/UGUIMonkeyAgent.cs +++ b/Runtime/Agents/UGUIMonkeyAgent.cs @@ -56,6 +56,33 @@ public class UGUIMonkeyAgent : AbstractAgent public List randomStringParametersMap = new List(); + /// + /// Whether screenshot is enabled + /// + public bool screenshotEnabled; + + /// + /// Whether using a default directory or specifying manually + /// + public bool defaultScreenshotDirectory = true; + + /// + public string screenshotDirectory; + + /// + /// Whether using a default file name prefix or specifying manually + /// + public bool defaultScreenshotFilenamePrefix = true; + + /// + public string screenshotFilenamePrefix; + + /// + public int screenshotSuperSize = 1; + + /// + public ScreenCapture.StereoScreenCaptureMode screenshotStereoCaptureMode = + ScreenCapture.StereoScreenCaptureMode.LeftEye; /// public override async UniTask Run(CancellationToken token) @@ -72,7 +99,16 @@ public override async UniTask Run(CancellationToken token) RandomStringParametersStrategy = GetRandomStringParameters, SecondsToErrorForNoInteractiveComponent = secondsToErrorForNoInteractiveComponent, TouchAndHoldDelayMillis = touchAndHoldDelayMillis, - Gizmos = gizmos + Gizmos = gizmos, + Screenshots = screenshotEnabled + ? new ScreenshotOptions + { + Directory = defaultScreenshotDirectory ? null : screenshotDirectory, + FilenamePrefix = defaultScreenshotFilenamePrefix ? null : screenshotFilenamePrefix, + SuperSize = screenshotSuperSize, + StereoCaptureMode = screenshotStereoCaptureMode + } + : null }; await Monkey.Run(config, token);