1
1

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 3 years have passed since last update.

【MRTK】NonNativeKeyboard の使い方

Last updated at Posted at 2021-04-26

環境

  • HoloLens2
  • Windows 10 pc
  • Unity 2019.4.1f1
  • Mixed Reality Toolkit ver2.5.3

NonNativeKeyboard を使う上での注意点

日本語入力はできません。
InputFieldを使用すると標準のキーボードと二重に表示されてしまうのでInputFieldっぽいものを自作する必要があります。

ソースコード

NonNativeInputField.cs
using Microsoft.MixedReality.Toolkit.Experimental.UI;
using System;
using TMPro;
using UnityEngine;
using UnityEngine.EventSystems;

public class NonNativeInputField : MonoBehaviour, IPointerDownHandler
{
    [SerializeField] private TextMeshProUGUI Text = null;
    [SerializeField] private NonNativeKeyboard keyboard = null;

    private void Awake()
    {
        if (keyboard == null)
        {
            keyboard = GameObject.FindWithTag("NonNativeKeyboard").GetComponent<NonNativeKeyboard>();
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        keyboard.PresentKeyboard(Text.text);
        for (int i = 0; i < Text.text.Length; i++)
        {
            keyboard.MoveCaretRight();
        }
        keyboard.OnClosed += DisableKeyboard;
        keyboard.OnTextSubmitted += DisableKeyboard;
        keyboard.OnTextUpdated += UpdateText;
    }

    private void UpdateText(string text)
    {
        Text.text = text;
    }

    private void DisableKeyboard(object sender, EventArgs e)
    {
        keyboard.OnTextUpdated -= UpdateText;
        keyboard.OnClosed -= DisableKeyboard;
        keyboard.OnTextSubmitted -= DisableKeyboard;
        keyboard.Close();
    }
}

使い方

  1. Hierarchy に NonNativekeyboard を追加する。
    (project > Mixed Reality Toolkit Foundation > SDK > Experimental > NonNativeKeyboard > prefabs)

  2. NonNativeKeyboard に "NonNativeKeyboard" タグをつける。

  3. NonNativeKeyboard子要素の InputField の Interactable をオフにする。
    (Hierarchy > NonNativeKeyboard > keyboard_Background > search > InputField)

  4. InputField の Disabled Color を 255,255,255,255 に設定

  5. Hierarchy に UI > image を追加する。

  6. Source Image を InputFieldBackground に設定

  7. Imageの子要素として UI >Text-TextMeshPro を追加する。

  8. Image に上のスクリプト(NanNativeInputField)をアタッチ。

  9. Inspector から Text 及び NoNnativeKeyboard プロパティを設定。

説明

ポインターで操作できるように IPointerDownHandler インターフェイスを実装しています。

各種イベント
keyboard.OnClosed 閉じるボタンを押したときに呼び出される。
keyboard.OnTextSubmitted Enterey を押したときに呼び出される。
keyboard.OnTextUpdated テキストが更新されたときに呼び出される。

公式ドキュメント : NonNativeKeybaord

1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?