From 9d91ece7fbc271364b9d76f4e5ef1567094bdf8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20ICH=C3=89?= Date: Wed, 25 Apr 2018 00:05:11 +0200 Subject: [PATCH] Fixes for Screenshots Needed a bit of refactor --- .../CommandLibrary/ScreenshotCommand.cs | 3 +- Assets/Console/Script/Console.cs | 96 ++++++++++++------- 2 files changed, 66 insertions(+), 33 deletions(-) diff --git a/Assets/Console/CommandLibrary/ScreenshotCommand.cs b/Assets/Console/CommandLibrary/ScreenshotCommand.cs index 8342913..157a738 100644 --- a/Assets/Console/CommandLibrary/ScreenshotCommand.cs +++ b/Assets/Console/CommandLibrary/ScreenshotCommand.cs @@ -21,9 +21,10 @@ public void Execute(string[] args) string datetime = string.Format("{0}{1}{2}-{3}{4}{5}",now.Year, now.Month, now.Day, now.Hour,now.Minute,now.Second); string filename = string.Format("Unity-{0}-{1}-{2}.png", Application.productName, SceneManager.GetActiveScene().name, datetime); Console.Log(GetName(), string.Format("Taking Screenshot at {0}x resolution : {1}",size, filename)); - ScreenCapture.CaptureScreenshot(filename,1); + Console.CaptureScreenshot(filename, size); } + public string GetHelp() { return @"usage: screenshot [supersize 1~5]"; diff --git a/Assets/Console/Script/Console.cs b/Assets/Console/Script/Console.cs index 33e8679..428b0d3 100644 --- a/Assets/Console/Script/Console.cs +++ b/Assets/Console/Script/Console.cs @@ -32,36 +32,32 @@ public class Console : MonoBehaviour [Range(1.0f, 30.0f)] public float ScrollSpeed = 5.0f; - private static ConsoleData s_ConsoleData = new ConsoleData(); + private static ConsoleData s_ConsoleData; + private static Console s_Console; private bool bVisible = false; private int history = -1; void OnEnable() { + if(s_ConsoleData == null) + { + s_ConsoleData = new ConsoleData(); + s_ConsoleData.AutoRegisterConsoleCommands(); + } s_ConsoleData.OnLogUpdated = UpdateLog; + s_Console = this; Application.logMessageReceived += HandleUnityLog; - // Use reflection to add automatically console commands - var autoCommandTypes = new List(); + Log("Console initialized successfully"); + } - foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) - { - foreach (Type type in assembly.GetTypes()) - { - if (type.GetInterfaces().Contains(typeof(IConsoleCommand)) && type.GetCustomAttributes(typeof(AutoRegisterConsoleCommandAttribute), true).Length > 0) - { - autoCommandTypes.Add(type); - } - } - } - foreach(var type in autoCommandTypes) - { - AddCommand(Activator.CreateInstance(type) as IConsoleCommand); - } - - Log("Console initialized successfully"); + void OnDisable() + { + s_ConsoleData.OnLogUpdated = null; + s_Console = null; + Application.logMessageReceived -= HandleUnityLog; } public void UpdateAutoPanel() @@ -104,16 +100,6 @@ public void UpdateAutoPanel() } - void OnDisable() - { - Application.logMessageReceived -= HandleUnityLog; - } - - private void Start() - { - - } - void Update() { if (Input.GetKeyDown(ToggleKey)) @@ -161,9 +147,34 @@ void Update() } } - void ToggleVisibility() + public static void SetVisible(bool visible) + { + s_Console.SetVisibility(visible); + } + + public static void CaptureScreenshot(string filename, int size) + { + s_Console.StartCoroutine(s_Console.Screenshot(filename, size)); + } + + public IEnumerator Screenshot(string filename, int size) + { + Console.SetVisible(false); + yield return new WaitForEndOfFrame(); + ScreenCapture.CaptureScreenshot(filename, 1); + Console.SetVisible(true); + } + + public void ToggleVisibility() + { + SetVisibility(!bVisible); + } + + public void SetVisibility(bool visible) { - bVisible = !bVisible; + if (bVisible == visible) + return; + bVisible = visible; Canvas.gameObject.SetActive(bVisible); if (bVisible) { @@ -172,7 +183,6 @@ void ToggleVisibility() InputField.ActivateInputField(); UpdateLog(); } - } public void ValidateCommand() @@ -339,6 +349,28 @@ public ConsoleData() aliases = new Dictionary(); commandHistory = new List(); } + + public void AutoRegisterConsoleCommands() + { + // Use reflection to add automatically console commands + var autoCommandTypes = new List(); + + foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) + { + foreach (Type type in assembly.GetTypes()) + { + if (type.GetInterfaces().Contains(typeof(IConsoleCommand)) && type.GetCustomAttributes(typeof(AutoRegisterConsoleCommandAttribute), true).Length > 0) + { + autoCommandTypes.Add(type); + } + } + } + foreach (var type in autoCommandTypes) + { + AddCommand(Activator.CreateInstance(type) as IConsoleCommand); + } + + } } [AutoRegisterConsoleCommand]