Forum rules - please read before posting.

Text buttons for input field

edited July 2021 in Technical Q&A

Hey there!
So I have a naming character screen, my specific style is having the keyboard keys on screen, they are all individual text objects (so 26 text objects),

I'm wondering if it's possible for the user to be able to click this and it adds the letter to the input field?

I thought about making empty image buttons for them all and adding "menu set input box text" but it looks like I can't add individual letters onto the input, only fixed letters? (if that makes sense? so if I click another letter it just erases the last one since it sets a new entire input)

I'm kind of stuck so was wondering if anyone else had any ideas or done something similar?

Thank you! o:)

Comments

  • You'll need to extract the Input element's current text, add on the character, and then send it back to the element.

    This can be done when a Button is clicked by hooking into the OnMenuElementClick event. If you adopt a clear naming convention for your Button elements, you can automate this based on the Button names:

    using UnityEngine;
    using AC;
    
    public class KeyboardButtons : MonoBehaviour
    {
    
        private void OnEnable () { EventManager.OnMenuElementClick += OnMenuElementClick; }
        private void OnDisable () { EventManager.OnMenuElementClick -= OnMenuElementClick; }
    
        private void OnMenuElementClick (AC.Menu menu, MenuElement element, int slot, int buttonPressed)
        {
            if (menu.title == "KeyboardMenu" && element.title == "Key_")
            {
                string key = element.title.Substring (4);
    
                MenuInput inputElement = PlayerMenus.GetElementWithName ("InputMenu", "Input") as MenuInput;
                string currentInputContents = inputElement.GetContents ();
                string newInputContents = currentInputContents + key;
                inputElement.SetLabel (newInputContents);
            }
        }
    
    }
    

    Replace "KeyboardMenu" and "InputMenu" with the names of the Menus used for the keyboard buttons and input box, and replace "Input" with the name of the input box element.

    In the menu with the keyboard buttons, rename the key button element titles to start with "Key_", followed by the key they represent (e.g. "Key_a", "Key_4" etc). The script will then extract the key from their titles and add them to the input box.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.