Skip to main content
Commonmark migration
Source Link

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

    Place an instance of CustomInput in your scene

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

    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.

    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.

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

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.

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.

Source Link
DMGregory
  • 141k
  • 23
  • 258
  • 401

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.