0
\$\begingroup\$

I'm trying to create a falling word typing game like z-type.

I have used the code provided here — but it's is only for keyboards:

public class WordInput : MonoBehaviour {

    public WordManager wordManager;

    void Update () {

        foreach (char letter in Input.inputString)
        {
            wordManager.TypeLetter(letter);   
        }    
    }
}

I want to implement this on mobiles as well. So I have created a mobile keypad using a canvas and buttons for alphabets but I'm unable to link it to words shown on the screen.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It's reading letters from Input.inputString. So all we have to do is provide our own alternate input source.

public class CustomInput : MonoBehaviour {

    string inputThisFrame = "";
    
    // Call this method from your on-screen buttons.
    public void Type(string text) {
        inputThisFrame += text;
    }

    // Expose text the same way Input does.
    public string inputString {
        get {
             if(inputThisFrame != "")
                 return inputThisFrame;

             // Fall back on built-in input.
             return Input.inputString;
        }
    }

    // At the end of the frame, clear the buffer
    // so we start from scratch next frame.
    void LateUpdate() {
        inputThisFrame = "";
    }
}

Now you can:

  1. Place an instance of CustomInput in your scene

  2. Wire up your on-screen buttons to report their typing to the CustomInput instance

  3. Modify WordInput so it takes an instance of CustomInput and asks for its inputString instead of the built-in Input.

    The fallback we added above ensures the regular keyboard still works, when presentz which helps with testing.

\$\endgroup\$
4
  • \$\begingroup\$ Thanks for the response @DMGregory ...I'm still having issues with the keypad. I have created the CustomInput script and added it to the main camera. Then I added the CustomInput script to the button's "On Click" function...Modified the wordinput script as follows : foreach(char letter in customInput.inputString) { wordManager.TypeLetter(letter); } But get a null reference error to the foreach line....Any advise on how to rectify this? \$\endgroup\$ Commented Nov 30, 2018 at 14:03
  • \$\begingroup\$ Did you remember to assign WordInput's customInput instance, or did you leave it as null? \$\endgroup\$
    – DMGregory
    Commented Nov 30, 2018 at 14:30
  • \$\begingroup\$ no @DMGregory, I haven't added...not sure how to do that..this what I have:public WordManager wordManager; public CustomInput customInput; // Update is called once per frame void Update () { foreach(char letter in customInput.inputString) { wordManager.TypeLetter(letter); } } \$\endgroup\$ Commented Nov 30, 2018 at 14:33
  • \$\begingroup\$ Assign the customInput reference in the Inspector. \$\endgroup\$
    – DMGregory
    Commented Nov 30, 2018 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .