/// <summary> /// Update the text and the label by grabbing it from the iOS/Android keyboard. /// </summary> void Update() { if (mKeyboard != null) { string text = mKeyboard.text; if (mText != text) { mText = ""; for (int i = 0; i < text.Length; ++i) { char ch = text[i]; if (validator != null) { ch = validator(mText, ch); } if (ch != 0) { mText += ch; } } if (maxChars > 0 && mText.Length > maxChars) { mText = mText.Substring(0, maxChars); } if (mText != text) { mKeyboard.text = mText; } UpdateLabel(); } if (mKeyboard.done) { mKeyboard = null; current = this; if (onSubmit != null) { onSubmit(mText); } if (eventReceiver == null) { eventReceiver = gameObject; } eventReceiver.SendMessage(functionName, mText, SendMessageOptions.DontRequireReceiver); current = null; selected = false; } } }
/// <summary> /// Input field creation function. /// </summary> void CreateInput(GameObject go) { if (NGUISettings.atlas != null) { NGUIEditorTools.SpriteField("Background", "Sliced Sprite for the background", NGUISettings.atlas, mInputBG, OnInputBG); } if (ShouldCreate(go, NGUISettings.atlas != null && NGUISettings.font != null)) { int depth = NGUITools.CalculateNextDepth(go); go = NGUITools.AddChild(go); go.name = "Input"; float padding = 3f; NGUISprite bg = NGUITools.AddWidget <NGUISprite>(go); bg.type = NGUISprite.Type.Sliced; bg.name = "Background"; bg.depth = depth; bg.atlas = NGUISettings.atlas; bg.spriteName = mInputBG; bg.pivot = NGUIWidget.Pivot.Left; bg.transform.localScale = new Vector3(400f, NGUISettings.font.size + padding * 2f, 1f); bg.transform.localPosition = Vector3.zero; bg.MakePixelPerfect(); NGUILabel lbl = NGUITools.AddWidget <NGUILabel>(go); lbl.font = NGUISettings.font; lbl.pivot = NGUIWidget.Pivot.Left; lbl.transform.localPosition = new Vector3(padding, 0f, 0f); lbl.multiLine = false; lbl.supportEncoding = false; lbl.lineWidth = Mathf.RoundToInt(400f - padding * 2f); lbl.text = "You can type here"; lbl.MakePixelPerfect(); // Add a collider to the background NGUITools.AddWidgetCollider(go); // Add an input script to the background and have it point to the label NGUIInput input = go.AddComponent <NGUIInput>(); input.label = lbl; // Update the selection Selection.activeGameObject = go; } }
/// <summary> /// Force-localize the widget. /// </summary> public void Localize() { Localization loc = Localization.instance; NGUIWidget w = GetComponent <NGUIWidget>(); NGUILabel lbl = w as NGUILabel; NGUISprite sp = w as NGUISprite; // If no localization key has been specified, use the label's text as the key if (string.IsNullOrEmpty(mLanguage) && string.IsNullOrEmpty(key) && lbl != null) { key = lbl.text; } // If we still don't have a key, leave the value as blank string val = string.IsNullOrEmpty(key) ? "" : loc.Get(key); if (lbl != null) { // If this is a label used by input, we should localize its default value instead NGUIInput input = NGUITools.FindInParents <NGUIInput>(lbl.gameObject); if (input != null && input.label == lbl) { input.defaultText = val; } else { lbl.text = val; } } else if (sp != null) { sp.spriteName = val; sp.MakePixelPerfect(); } mLanguage = loc.currentLanguage; }
/// <summary> /// Append the specified text to the end of the current. /// </summary> void Append(string input) { for (int i = 0, imax = input.Length; i < imax; ++i) { char c = input[i]; if (c == '\b') { // Backspace if (mText.Length > 0) { mText = mText.Substring(0, mText.Length - 1); SendMessage("OnInputChanged", this, SendMessageOptions.DontRequireReceiver); } } else if (c == '\r' || c == '\n') { if (NGUICamera.current.submitKey0 == KeyCode.Return || NGUICamera.current.submitKey1 == KeyCode.Return) { // Not multi-line input, or control isn't held if (!label.multiLine || (!Input.GetKey(KeyCode.LeftControl) && !Input.GetKey(KeyCode.RightControl))) { // Enter current = this; if (onSubmit != null) { onSubmit(mText); } if (eventReceiver == null) { eventReceiver = gameObject; } eventReceiver.SendMessage(functionName, mText, SendMessageOptions.DontRequireReceiver); current = null; selected = false; return; } } // If we have an input validator, validate the input first if (validator != null) { c = validator(mText, c); } // If the input is invalid, skip it if (c == 0) { continue; } // Append the character if (c == '\n' || c == '\r') { if (label.multiLine) { mText += "\n"; } } else { mText += c; } // Notify the listeners SendMessage("OnInputChanged", this, SendMessageOptions.DontRequireReceiver); } else if (c >= ' ') { // If we have an input validator, validate the input first if (validator != null) { c = validator(mText, c); } // If the input is invalid, skip it if (c == 0) { continue; } // Append the character and notify the "input changed" listeners. mText += c; SendMessage("OnInputChanged", this, SendMessageOptions.DontRequireReceiver); } } // Ensure that we don't exceed the maximum length UpdateLabel(); }