I wanted to make a simple Application Pause Toggle as Unity already implemented it, but with an easier keyboard shortcut (Because sometimes you just really want to quickly pause the editor and maybe have only one finger left). Sadly the shortcut can’t be changed in the preferences, so I went to do my own simple script:
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="PauseApplicationToggle.cs" company="Supyrb">
// Copyright (c) 2017 Supyrb. All rights reserved.
// </copyright>
// <author>
// Johannes Deml
// send@johannesdeml.com
// </author>
// --------------------------------------------------------------------------------------------------------------------
using UnityEditor;
namespace Supyrb
{
using UnityEngine;
using System.Collections;
public static class PauseApplicationToggle
{
[MenuItem("Supyrb/Misc/TogglePauseApplication _p", false, 10000)]
public static void TogglePauseApplication()
{
EditorApplication.isPaused = !EditorApplication.isPaused;
}
[MenuItem("Supyrb/Misc/TogglePauseApplication _p", true)]
public static bool TogglePauseApplicationValidation()
{
return Application.isPlaying;
}
}
}
The script works quite well and I can easily toggle by just hitting the p key. One big problem for me however is, that the menu item consumes the keyboard input event, even if the validator function sends back a false. So, whenever I want to rename something and hit a p, the renaming is stopped, I just can’t type the letter p anymore.
Is there a workaround for this? I imagine I can add a custom script that listens to all events, and uses the p event, but does not consume it. This feels rather dirty, I hope there is some functionality I missed.
There are a few possible solutions for this, but in my opinion, a menu item shouldn’t have a single key shortcut in the first place… Think of other tools or apps you use, most use key combinations (CTRL + key) as shortcuts. I believe you can already achieve the same using ctrl-shift-p, you could simply train your fingers to hit that combination to pause…
Other solutions are not to use menu items, but something else that will pause the game as you hit that key.
Since you only want this to run when Application.isPlaying is true, a runtime script that processes this input sounds like a better fit to me (as opposed to the editor script that is executed even when you’re not in play mode).
Thanks for the answer. Yep, the keyboard shortcut ctrl-shift-p does exactly what I want, but I sadly can’t change the combination in the preferences.
Hm, well when you are thinking about pressing W or E or R or F, there are quite a few shortcuts in unity that work without any modifiers.
I also had the thought of creating a runtime script, but the problem there is, that it won’t be able to resume the pause, because the application is paused. So that is also not the best solution out there… I’m just annoyed that I haven’t found a way to control whether the event is consumed or not.
I actually tried the code you posted (using Unity 5.6 beta 3) and it didn’t work - hitting “P” while in play mode didn’t do anything. When i manually paused, and then hit “P”, it unpaused the game.
There may be other solutions to this issue, i’ll try to have a look later
I think you can avoid than MenuItem consumes your keyboard event.
I use this shortcuts to avoid conflicts with Unity:
F6 = Play
F7 = Pause
F8 = Next Step
(F5 is used in Version Control window to refresh)
Interesting that you mention that, I just set the shortcut to the F keys just before you answered me as a quick fix. I think with this solution we are just shipping around the problem. The event is still consumed, but as long as no other event is interested in an F5 input it does not really matter.
This is still a problem and I wish it wasn’t. There’s a bunch of hotkeys in Unity already, and I put in a simple one that I really like, but it consumes input while the built-in ones work fine (such as for the tools in the bar at the top left, and any entered in the preferences).
Really wish there was a fix for this! I’d like to set up some custom hotkeys so some tasks are similar to working in Maya (would make workflow a bit easier for artists on my team) but this forces me to use some odd key combination that no one will remember. UGH!