Basically the title, I’m trying to focus on an Input Field, when a player presses a key.
I’m using this for a chatbox, so I want it when someone presses ENTER or Y, the chat will pop up, and focus so they can type, then they can press ENTER again to send it. I’ve got the chat box working already, just wondering how I can setup the focusing and what not.
Still looking for help on this.
I figured out a work around on focusing.
Here’s my script.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class Chat : BoltCallbacks
{
public Scrollbar scroll;
public RectTransform content;
public Text chatLine;
public InputField input;
public override bool PersistBetweenStartupAndShutdown ()
{
return true;
}
void Awake ()
{
input.onSubmit.AddListener (SendChat);
}
void Update ()
{
scroll.value = + -0.01f * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Y))
{
input.gameObject.SetActive(true);
EventSystemManager.currentSystem.SetSelectedGameObject(input.gameObject, null);
input.OnPointerClick (null);
}
}
void SendChat (string line)
{
input.value = "";
input.gameObject.SetActive (false);
if (line == "")
{
return;
}
IChat evnt = BoltFactory.NewEvent<IChat>();
evnt.text = PlayerNamePlate.playerName + ": " + line;
BoltNetwork.Raise(evnt);
}
public override void OnEvent (IChat evnt, BoltConnection cn)
{
Text newLine = ((GameObject) Instantiate(chatLine.gameObject)).GetComponent<Text>();
newLine.gameObject.SetActive(true);
newLine.rectTransform.parent = content;
newLine.text = evnt.text;
}
}
The problem now is, whenever I open chat with “Y”, the chatbox actually types Y… I’ve tried a few things, nothing seems to stop from typing the first Y, which is just suppose to open the chat.
1 Like