Adding Passwords to Player Actions
Last updated
Last updated
There may be times when you want the player to password protect a feature such as a Parental Control, Game time, or perhaps even to protect their inventory in a multiplayer environment.
The Accessibilty Module comes with several Actions to enable you to add this facility to your game. You can encrypt a string and save it encrypted to a variable, you can decrypt the encrypted variable to a string, and you can compare an encrypted variable with a string or another encrypted variable.
This tutorial will show you how to create a simple password screen to protect an Action of some kind. For this example, we will use the Parental Controls feature of pixelating an image.
To start, you need a Canvas with a Panel in your scene hierarchy. This Canvas can be displayed using a trigger of some type, usually a input/on key down, or a GC button to call the following Actions. Your Canvas will also need a Canvas Group if it is to be used with Game Creator. Unity does not always add this, sometimes you need to add it manually.
While you are working with the Canvas, you can have the Alpha set to 1 (so it is visable) and the Interactable and Block Raycast checked. However, later we will reverse these settings so that we can control it with Game Creator Actions.
2. We can create an Action to Display and Hide the Canvas, using the Change CanvasGroup Settings Action from the GC Actions/UI menu. If your target platform is PC/Mac, you may also wish to frezze the player character and stop the camera from orbitting while the player is interacting with the Canvas. This is not generally needed on a Mobile platfrom. Add the following Action to an On Key Down trigger or a GC Button.
4. You can also create the reverse of this to hide the Canvas. Do not forget to drag in the corresponding Canvas into the Canvas Group slot.
Now we have a Canvas (User Interface panel), we can add our UI components to this. We do not have to use a specific Canvas as we have created here, you could add the following to an existing setting panel if you wish. For the purpose of this tutorial, we will use the same Canvas for enabling the feature and setting the password, and for disabling the feature using the password.
5. We can now add UI components to our Panel. I have numbered these as they are different elements, but you do not need to add the number shown in Red.
Heading - This is a Text Element added from the Unity UI menu.
InputField - This is a standard Unity InputField also added from the Unity UI menu.
InputFiled GC - This is added from the Game Creator / UI menu as it adds to a variable.
Button1 - Added from the Game Creator / UI menu as it exectues an Action.
Button2 - Also added from tne Game Creator / UI menu.
Error Message - An empty Text Element current disabled.
The Scene Hierarchy for the Canvas will look something like this, although we have not yet added the Password Conditions and Actions.
6. Before we go any further, we will create two String variables using the Game Creator Preferences panel. Give them meaningful names and remember to click the save check box, especially for the variable where the encrypted password will be stored. We will use these in our Actions and Conditions.
We will deal with each element in turn as we progress. Firstly, the heading (1) does not need changing as we will do this from an Action. The Inputfield (2) also does not need changing as an Action will also deal with thois.
7. The second Inputfield (3) which is a Game Creator Inputfield, should add to the Global Variable labled "Entered-Password". This is done by selecting the correct Variable from the dropdown menu.
Do Not add to a Variable from the first InputField (2) element. The Action will do this during the Encryption process.
8. Both of the Buttons should be Game Creator Buttons and have the ability to add Actions directly to the button scripts in thne Inspector. For Button1 (4), you should add three Actions.
Select the Encrypt to Variable Action from the Accessibility section. Drag the Inputfield (2) into the UI Text InputFiled slot. Select the String Variable you creatored called "Encrypted" for the Target Variable, and change the Encryption Key to something other than the default one. Please Note: The Key must be exactly 32 bytes (characters/numbers) long.
The second Action should be the Action you wish to password protect and in our example this isthe Enable Censor Objects Action.
The third Action and this is not essential, we can change the heading text (1) to reflect the fact that the Enable Censor Objects is turned on.
Now you have a half working function. You can hit play and enter a password into the Inputfield (1) and then view the Variables, and you will see an Encrypted string. On Button2 (5), we will add the Action that Calls a Condition, but first we need to create the Condition.
9. Create a Game Creator Condition from the Toolbar or by seleting Game Creator/Conditions from the right-click menu in the scene hierarchy. We have called this PasswordConditions, but the name is not important. In the Inspector Window, select and add the Accessibility Condition, Variable Encrypted String.
Select the Encrypted Variable for the first parameter, and for the Text String, use the variable where the Game Creator Inputfield added to in step 7 - in our case this is Entered-Password.
In the Decryption Key, enter the identical Key that you added in step 8.1 (must be identical and must be 32 bytes long).
Now we need to add the "Then" (if it matches) and the "Else" (if it does not match) Actions.
10. If the variable matches, we simply reverse the earlier Actions and turn off Censor Objects by using the Disable function, and change the Text Heading to "Off".
11. If the password does not match, we can do nothing, or display an error message. In this example, we set active a pre-defined text object, wait 3 seconds, then set it inactive. You could also use a Change Text Action and then change it to blank.
You can test it by selecting Play and watch the Variable feilds in the GC Preferences Window. When you are happy with it, you can change the Inputfield Content Type to be "Password" and this will display ******* when the text is typed.
And that is alll there is to it. However, you will probably want to save you player profile (variables, etc), and reload them at the start of the game, otherwise the Encrypted Password will not be loaded. Buit that ius a topic for another tutorial.