Button to target input field linked to I2Loc?

More
6 years 4 months ago #3130 by jimmyt3d
Hi Frank,
I hope you are well.
A couple of years ago you assisted with a piece of code to make a password Input Field look to the I2Loc Language Source to check for the password (to enable the password to be updated remotely via the spreadsheet). Here is the script (currently in the 'On End Edit (String)' field of the Input Field.
using UnityEngine;
using System.Collections;

public class PasswordInput2 : MonoBehaviour {

		public GameObject myObject; //Wrong password message
	public GameObject myObject2; //Correct password message

	public void PassWordCheck(string incomingPassword)
	{
		var RealPassword = I2.Loc.ScriptLocalization.Get("MapPW", false);


		if (incomingPassword == RealPassword)
		{
			myObject.SetActive(false);
			myObject2.SetActive(true);
								}
		else
		{
			myObject.SetActive(true);
			myObject2.SetActive(false);
	}
}
}

This has always worked perfectly, however I wish to make a slight change to how it works.
Now I want to have a 'LOGIN' button that the user can click once they have put their password in, then when this button is clicked I want it to check the Input Field for the password and then respond in the same way it does now. Essentially I want the current behaviour migrated to a button instead of the Input Field itself (too many issues with the password being 'entered' if you happen to click away from the input field due to the way Unity handles focus, therefore I want my 'Incorrect Password' messages only to appear after a 'Login' button has been pressed).
I assume this script needs to be added to the OnClick of the button instead of the Input field, but I am unsure how to make it look to the Input Field from the button.

Any help would be great, I'm not a coder so if you could update the script for me that would be much appreciated!

Unity 5.6.4f1 - I2Loc 2.7.0f1

Thanks

Please Log in or Create an account to join the conversation.

More
6 years 4 months ago #3131 by Frank
Hi,
The code is mostly the same, you just need to add a reference to the InputField and OnClick just grabs its text and calls the PassWordCheck function:
using UnityEngine;
using System.Collections;

public class PasswordInput2 : MonoBehaviour
{
    public GameObject myObject; //Wrong password message
    public GameObject myObject2; //Correct password message
    public UnityEngine.UI.InputField myPasswordInputField;

    public void OnClick()
    {
        PassWordCheck(myPasswordInputField.text);
    }

    public void PassWordCheck(string incomingPassword)
    {
        var RealPassword = I2.Loc.ScriptLocalization.Get("MapPW", false);


        if (incomingPassword == RealPassword)
        {
            myObject.SetActive(false);
            myObject2.SetActive(true);
        }
        else
        {
            myObject.SetActive(true);
            myObject2.SetActive(false);
        }
    }
}

Assign that script to the gameobject and drag the inputField to the myPasswordInputField property.
Then, in the button, add an OnClick and assign the OnClick function.

Hope that helps,
Frank

Are you :-) Give I2L 5 stars!
Are you :-( Please lets us know how to improve it!
To get the betas as soon as they are ready, check this out

Please Log in or Create an account to join the conversation.

More
6 years 4 months ago #3132 by jimmyt3d
That works a treat - thanks a lot Frank.

Please Log in or Create an account to join the conversation.

Time to create page: 0.150 seconds
Template by JoomlaShine