| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.IO; |
| | 7 | | using UnityEngine; |
| | 8 | |
|
| | 9 | | namespace GDX.Developer.ConsoleCommands |
| | 10 | | { |
| | 11 | | #if UNITY_2022_2_OR_NEWER |
| | 12 | | public class ScreenCaptureConsoleCommand : ConsoleCommandBase |
| | 13 | | { |
| | 14 | | string m_FilePath; |
| 7 | 15 | | int m_SuperSize = 1; |
| | 16 | |
|
| | 17 | | public override bool Evaluate(float deltaTime) |
| 0 | 18 | | { |
| 0 | 19 | | ScreenCapture.CaptureScreenshot(m_FilePath, m_SuperSize); |
| 0 | 20 | | Debug.Log($"Wrote screen capture to '{m_FilePath}'."); |
| 0 | 21 | | return true; |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | public override string GetKeyword() |
| 7 | 25 | | { |
| 7 | 26 | | return "screencapture"; |
| 7 | 27 | | } |
| | 28 | |
|
| | 29 | | public override string GetHelpMessage() |
| 0 | 30 | | { |
| 0 | 31 | | return "Captures a screenshot."; |
| 0 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <inheritdoc /> |
| | 35 | | public override ConsoleCommandBase GetInstance(string context) |
| 0 | 36 | | { |
| | 37 | | // Early out |
| 0 | 38 | | if (string.IsNullOrEmpty(context)) |
| 0 | 39 | | { |
| 0 | 40 | | return new ScreenCaptureConsoleCommand() { m_FilePath = GetPath(GetDefaultName())}; |
| | 41 | | } |
| | 42 | |
|
| | 43 | | // Handle arguments |
| 0 | 44 | | string[] split = context.Split(' ', StringSplitOptions.RemoveEmptyEntries); |
| 0 | 45 | | if (split.Length > 1) |
| 0 | 46 | | { |
| 0 | 47 | | return new ScreenCaptureConsoleCommand() |
| | 48 | | { |
| | 49 | | m_FilePath = GetPath(split[0]), m_SuperSize = int.Parse(split[1]) |
| | 50 | | }; |
| | 51 | | } |
| | 52 | |
|
| 0 | 53 | | if (split[0].IsNumeric()) |
| 0 | 54 | | { |
| 0 | 55 | | return new ScreenCaptureConsoleCommand() |
| | 56 | | { |
| | 57 | | m_FilePath = GetPath(GetDefaultName()), m_SuperSize = int.Parse(split[0]) |
| | 58 | | }; |
| | 59 | | } |
| | 60 | |
|
| 0 | 61 | | return new ScreenCaptureConsoleCommand() |
| | 62 | | { |
| | 63 | | m_FilePath = GetPath(split[0]) |
| | 64 | | }; |
| 0 | 65 | | } |
| | 66 | |
|
| | 67 | | public static string GetPath(string fileName) |
| 0 | 68 | | { |
| 0 | 69 | | return Path.Combine(Platform.GetOutputFolder("Screenshots"), fileName); |
| 0 | 70 | | } |
| | 71 | | static string GetDefaultName() |
| 0 | 72 | | { |
| 0 | 73 | | return $"Screenshot_{DateTime.Now.ToString(Platform.FilenameTimestampFormat)}.png"; |
| 0 | 74 | | } |
| | 75 | | } |
| | 76 | | #endif // UNITY_2022_2_OR_NEWER |
| | 77 | | } |