Changing Inputs from the Defaults

One of the headlining features of cInput is that it enables you to change key bindings at runtime, a feature sorely missing from Unity

Changing Inputs via Script

For information on how to change key bindings from script, see the ChangeKey() documentation.

Using the Built-In GUI

cInput comes with a built-in GUI to make it simple to change which inputs are bound to which actions. This is great for rapid prototyping since it allows you to adjust the controls at runtime without having to invest a lot of time into a custom GUI.

Due to popular demand, we've separated this built-in GUI from the main cInput class into it's own class called cGUI. Simply call cGUI.ToggleGUI() to show or hide the built-in GUI menu, and assign a GUISkin to cGUI.cSkin to change the way it looks. cInput includes a GUISkin for your convenience. You can also optionally change the color/alpha of the GUI using cGUI.bgColor. For more specific information on how to use cGUI, please see the cGUI Reference Manual included in the .unitypackage.

Making a Custom GUI Menu

Admittedly, even though you can customize the look of the built-in GUI with cGUI.cSkin, it still won't fit the design or theme for every game. In situations like these where you're past the prototyping phase and getting closer to public release, it will likely be necessary (not to mention recommended) to replace the built-in GUI with something that fits the style of the rest of your game. With that in mind, cInput includes functions which will assist in the process of making your own custom GUI menu for changing controls.

A quick note: Explaining how to make a UI in Unity is beyond the scope of this document. To learn more about how to make GUIs in Unity, see the UI System or the Legacy GUI Scripting Guide sections in the online Unity Reference Manual. This section only explains the methods which cInput provides to assist you in making your own UI menu for displaying and changing the key bindings in your game. Also note that if you make your own UI, you can prevent cInput from adding its own included GUI components by using cInput.Init(false) in your setup script.

There are a few methods and properties you will likely want to use when creating a UI to display the current key bindings and allow the players to change them or reset them back to defaults. They are the GetText(), ChangeKey(), ResetInputs() functions and the length property. You may also be interested in the OnKeyChanged event.

The basic idea is to use a for loop to iterate through all of the inputs and create buttons the player can click in order to change the key bindings while in game. Here is an example with Unity's legacy GUI:

// this variable keeps track of the vertical scroll position (if necessary) private Vector2 _scrollPosition = new Vector2(); void OnGUI() { _scrollPosition = GUILayout.BeginScrollView(_scrollPosition); GUILayout.BeginHorizontal(); // this sections lists the names of each action, such as "Move Left" or "Jump" GUILayout.BeginVertical(); GUILayout.Label("Action"); for (int n = 0; n < cInput.length; n++) { GUILayout.Label(cInput.GetText(n, 0)); } GUILayout.EndVertical(); // this sections lists the primary input for the actions, such as "A" or "Space" GUILayout.BeginVertical(); GUILayout.Label("Primary"); for (int n = 0; n < cInput.length; n++) { if (GUILayout.Button(cInput.GetText(n, 1)) && Input.GetMouseButtonUp(0)) { cInput.ChangeKey(n, 1); } } GUILayout.EndVertical(); // this section lists the secondary input for the actions, such as "Left Arrow" GUILayout.BeginVertical(); GUILayout.Label("Secondary"); for (int n = 0; n < cInput.length; n++) { if (GUILayout.Button(cInput.GetText(n, 2)) && Input.GetMouseButtonUp(0)) { cInput.ChangeKey(n, 2); } } GUILayout.EndVertical(); GUILayout.EndHorizontal(); GUILayout.EndScrollView(); // this gives the player a button they can click to reset the inputs to the defaults. GUILayout.BeginHorizontal(); if (GUILayout.Button("Reset to Defaults") && Input.GetMouseButtonUp(0)) { cInput.ResetInputs(); } // this shows a button to close the menu if (GUILayout.Button("Close") && Input.GetMouseButtonUp(0)) { cInput.ShowMenu(false); } GUILayout.EndHorizontal(); }

results matching ""

    No results matching ""