Tanoda
InputFocus.cs
Go to the documentation of this file.
1
4
6{
7 [RequireComponent(typeof(InputField))]
8 [AddComponentMenu("UI/Extensions/InputFocus")]
9 public class InputFocus : MonoBehaviour
10 {
11 #region Private Variables
12
13 // The input field we use for chat
14 protected InputField _inputField;
15
16 // When set to true, we will ignore the next time the "Enter" key is released
17 public bool _ignoreNextActivation = false;
18
19 #endregion
20
21 void Start()
22 {
23 _inputField = GetComponent<InputField>();
24 }
25
26 void Update()
27 {
28 // Check if the "Enter" key was just released with the chat input not focused
29 if (Input.GetKeyUp(KeyCode.Return) && !_inputField.isFocused)
30 {
31 // If we need to ignore the keypress, do nothing - otherwise activate the input field
33 {
35 }
36 else
37 {
38 _inputField.Select();
39 _inputField.ActivateInputField();
40 }
41 }
42 }
43
44 public void buttonPressed()
45 {
46 // Do whatever you want with the input field text here
47
48 // Make note of whether the input string was empty, and then clear it out
49 bool wasEmpty = _inputField.text == "";
50 _inputField.text = "";
51
52 // If the string was not empty, we should reactivate the input field
53 if (!wasEmpty)
54 {
55 _inputField.Select();
56 _inputField.ActivateInputField();
57 }
58 }
59
60 public void OnEndEdit(string textString)
61 {
62 // If the edit ended because we clicked away, don't do anything extra
63 if (!Input.GetKeyDown(KeyCode.Return))
64 {
65 return;
66 }
67
68 // Do whatever you want with the input field text here
69
70 // Make note of whether the input string was empty, and then clear it out
71 bool wasEmpty = _inputField.text == "";
72 _inputField.text = "";
73
74 // if the input string was empty, then allow the field to deactivate
75 if (wasEmpty)
76 {
78 }
79 }
80
81
82 }
83}
void OnEndEdit(string textString)
Definition: InputFocus.cs:60
Credit Erdener Gonenc - @PixelEnvision.