Input Mode
The Input Mode class is a handy way to switch between several modes of input. For example, when the game is paused or we are in an UI inventory we want the cursor to be visible and the cursor's lockstate to be confined. In this case we would set the InputMode to UI. When unpausing or when closing our inventory we once again put our InputMode to Game and the cursor will be hidden and locked.
This also allows us to check our InputMode before handling input. We don't want our character to be able to walk around when we are managing our inventory.
using UnityEngine;
using ExtensionTools;
public class Inventory : MonoBehaviour
{
void OpenInventory()
{
//Enable cursor and unlock
InputMode.SetInputModeUI();
}
void CloseInventory() {
//Hide cursor and lock
InputMode.SetInputModeGame();
}
}
In some cases you might want your cursor to be visible in the Game Mode as well, for example when creating a card game. To do this you have two options.
Option one is to pass a GameModeSettings class everytime you change the inputmode back to Game Mode.
InputMode.SetInputModeGame(new InputMode.GameModeSettings(CursorLockMode.Confined,true));
Option two is to override the default GameModeSettings somewhere in the start of the game. Now you don't have to pass GameModeSettings everytime you change the Inputmode back to Game.
InputMode.SetDefaultGameModeSettings(new InputMode.GameModeSettings(CursorLockMode.Confined, true));
Checking for InputMode
When writing our character movement or input in game we don't want it to be called when we are in the UI Game Mode. It doesn't make sense to be able to move our character when we are in a pause menu or in our inventory. So we just write a simple if statement before our movement code.
if (InputMode.currentInputmode == InputMode.Input.Game)
{
//movement code
}
The Debug Mode
The Debug Mode is a handy mode during development of your game but should only be used for development purposes. By default switching to this Inputmode will enable the Debug Camera and allow you to move around in your scene without disturbing the flow of the game. Think of it as a sort of God Mode for development purposes.
The recommended way to switch to the Debug Mode is by using the Console