16
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity 2017.x+InputField+Androidで日本語入力ができない問題への対策

Last updated at Posted at 2017-09-05

Unity 2017.1 において Content Type を Standard 等にしていると、 Android 上で InputField から日本語入力できない(日本語入力に切り替えられない)というケースがあるようです(2017.2も試しましたが現時点では改善なしでした)。
こちらは InputField 経由でなく、自分で TouchScreenKeyboard.Open を用いてキーボードを呼びだすことで回避可能のようでした。
以下サンプルコードです。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

/// <summary>
/// 2017.x を用いたバイナリが Android 上だとキーボードで日本語入力できない問題のワークアラウンド用.
/// InputField の子に Button を置き、その Button にアタッチして使用する.
/// </summary>
public class InputFieldButton : Button {

    private InputField inputField
    {
        get
        {
            if (inputField_ == null) {
                inputField_ = GetComponentInParent<InputField> ();
            }
            return inputField_;
        }
    }
    private InputField inputField_;

    private TouchScreenKeyboard keyboard_ = null;
    private string originalText_ = "";
    private bool wasCanceled_ = false;

    void CreateKeyboard()
    {
        if (keyboard_ != null) return;
        if (!IsInteractable ())    return;

        if (!inputField.textComponent || inputField.textComponent.font == null)
            return;

        originalText_ = inputField.text;
        keyboard_ = TouchScreenKeyboard.Open (inputField.text, TouchScreenKeyboardType.Default, true);
    }

    void DestroyKeyboard()
    {
        if (!inputField.textComponent || !inputField.IsInteractable ())
            return;            

        if (wasCanceled_) {
            inputField.text = originalText_;
            wasCanceled_ = false;
        }
                
        if (keyboard_ != null) {
            keyboard_.active = false;
            keyboard_ = null;
            originalText_ = "";
        }
    }

    void LateUpdate() {
        if (keyboard_ == null) return;

        inputField.text = keyboard_.text;

        if (keyboard_.wasCanceled) {
            wasCanceled_ = true;
            DestroyKeyboard ();
        }

        if (keyboard_.done) {
            DestroyKeyboard ();
        }
    }

    public override void OnPointerDown (UnityEngine.EventSystems.PointerEventData eventData)
    {
        CreateKeyboard ();
        base.OnPointerDown (eventData);
    }

    public override void OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData)
    {
        CreateKeyboard ();
        base.OnPointerClick (eventData);
    }
}
16
10
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?