LoginSignup
2
0

More than 5 years have passed since last update.

【Unity】文字列範囲選択時にソフトウェアキーボードを非表示にする

Posted at

Unityで文字列を範囲選択したい場合、uGUIのInputFieldを使うと文字列を選択できるようになりますが

Android実機だとソフトウェアキーボードが表示されてしまいます

こいつをどうにかして表示されないようにしたい

Hide Mobile Input

InputFieldのインスペクタ上にHide Mobile InputとうチェックボックスがあるのでONにしてみました

しかし、相変わらずキーボードが表示される・・・・

なんだこのプロパティということで公式のドキュメントを見ると

Hide Mobile Input (iOS only)

Androidテメーはダメだ

image.png

ソースコード修正してみようとコード眺めてみましたが、これは無理だと思い他の方法を模索しました

Kindleアプリはそういや出来てるよな

であれば、Androidネイティブの機能でやればいいのかと思い、色々調べてTextViewで実現出来そうだったのでさっそくUnityから呼び出せるAndroidのプラグインを作ってみました

Plugin

Repository: https://github.com/satooon/unity_text_selectable

package com.unity.plugin.ui;

import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.unity3d.player.UnityPlayer;

public class TextSelectable {

    private TextView textView;

    public static TextSelectable Make(final int width, final int height, final int left, final int top) {
        final TextSelectable textSelectable = new TextSelectable();

        UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
            public void run() {
                RelativeLayout layout = new RelativeLayout(UnityPlayer.currentActivity);
                UnityPlayer.currentActivity.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                layout.setGravity(Gravity.TOP);

                textSelectable.textView = new TextView(UnityPlayer.currentActivity);
                textSelectable.SetTextIsSelectable(true);
                RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
                params.leftMargin = left;
                params.topMargin = top;
                layout.addView(textSelectable.textView, params);
            }
        });

        return textSelectable;
    }

    public void SetText(String text) {
        this.textView.setText(text);
    }

    public void SetTextIsSelectable(boolean selectable) {
        this.textView.setTextIsSelectable(selectable);
    }
}

表示中のアクティビティ上に直接配置させてます

あとはUnity側で呼び出す

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sample : MonoBehaviour
{
    void Start()
    {
#if UNITY_ANDROID && !UNITY_EDITOR
        this.ShowTextView();
#endif
    }

    void ShowTextView()
    {
        using (var cls = new AndroidJavaClass("com.unity.plugin.ui.TextSelectable")) {
            using (var textSelectable = cls.CallStatic<AndroidJavaObject>("Make", 500, 500, 0, 0)) {
                textSelectable.Call("SetText", "test test test test test test test");
            }
        }
    }
}

キーボードでてない…!

コンテキストメニューいじりたいなとなったら、それもプラグインでやらないといけないですが


開発中、UnityからAndroidのプラグインのパッケージ名が見つからない問題で数日悩みました

TextSelectableがTextViewを継承していたのが原因ぽかったです

何も継承してないクラスじゃないとUnityから呼べないのかなぁ

2
0
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
2
0