11
3

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】InputFieldの入力中に判定しない拡張Inputクラス(ExclusiveInput)

Posted at

概要

InputクラスのGetKeyDownなどはuGUIのInputFieldにテキストを入力中でも判定がされてしまいますが、多くの場合は意図しない入力だと思います。そこで、テキスト入力中には入力判定を返さない拡張クラスを作成しました。

gist: ExclusiveInput.cs

使い方

Inputクラスの代わりにExclusiveInput経由でGetKeyDownなどを呼び出してください。

.cs
void Update()
{
    if (ExclusiveInput.GetKeyDown(KeyCode.A))
    {
        // hoge
    }
    if (ExclusiveInput.GetKeyDown("a"))
    {
        // hoge
    }
}

対応メソッド

  • bool anyKeyDown
  • bool anyKey
  • float GetAxis(string axisName)
  • float GetAxisRaw(string axisName)
  • bool GetKey(string name)
  • bool GetKey(KeyCode key)
  • bool GetKeyDown(string name)
  • bool GetKeyDown(KeyCode key)
  • bool GetKeyUp(KeyCode key)
  • bool GetKeyUp(string name)

必要であれば適宜追加してください。

コード

EventSystemクラスで現在フォーカス中のゲームオブジェクトを取得することが出来ます。そこでそのオブジェクトがInputFieldであればtrueを返すIsFocusedOnInputFieldを実装し、各入力メソッドの前で判定を入れています。

public static bool IsFocusedOnInputField
{
    get
    {
        return EventSystem.current?.currentSelectedGameObject?.GetComponent<InputField>() != null;
    }
}

コード全文はこちらです。(gist: ExclusiveInput.cs)

ExclusiveInput.cs
using UnityEngine.EventSystems;
using UnityEngine.UI;

namespace UnityEngine
{
    public class ExclusiveInput
    {
        public static bool IsFocusedOnInputField
        {
            get
            {
                return EventSystem.current?.currentSelectedGameObject?.GetComponent<InputField>() != null;
            }
        }
        //
        // Summary:
        //     Returns true the first frame the user hits any key or mouse button. (Read Only)
        public static bool anyKeyDown
        {
            get
            {
                return !IsFocusedOnInputField && Input.anyKeyDown;
            }
        }
        //
        // Summary:
        //     Is any key or mouse button currently held down? (Read Only)
        public static bool anyKey
        {
            get
            {
                return !IsFocusedOnInputField && Input.anyKey;
            }
        }
        //
        // Summary:
        //     Returns the value of the virtual axis identified by axisName.
        //
        // Parameters:
        //   axisName:
        public static float GetAxis(string axisName)
        {
            return IsFocusedOnInputField ? 0 : Input.GetAxis(axisName);
        }
        //
        // Summary:
        //     Returns the value of the virtual axis identified by axisName with no smoothing
        //     filtering applied.
        //
        // Parameters:
        //   axisName:
        public static float GetAxisRaw(string axisName)
        {
            return IsFocusedOnInputField ? 0 : Input.GetAxisRaw(axisName);
        }
        //
        // Summary:
        //     Returns true while the user holds down the key identified by name.
        //
        // Parameters:
        //   name:
        public static bool GetKey(string name)
        {
            return !IsFocusedOnInputField && Input.GetKey(name);
        }
        //
        // Summary:
        //     Returns true while the user holds down the key identified by the key KeyCode
        //     enum parameter.
        //
        // Parameters:
        //   key:
        public static bool GetKey(KeyCode key)
        {
            return !IsFocusedOnInputField && Input.GetKey(key);
        }
        //
        // Summary:
        //     Returns true during the frame the user starts pressing down the key identified
        //     by name.
        //
        // Parameters:
        //   name:
        public static bool GetKeyDown(string name)
        {
            return !IsFocusedOnInputField && Input.GetKeyDown(name);
        }
        //
        // Summary:
        //     Returns true during the frame the user starts pressing down the key identified
        //     by the key KeyCode enum parameter.
        //
        // Parameters:
        //   key:
        public static bool GetKeyDown(KeyCode key)
        {
            return !IsFocusedOnInputField && Input.GetKeyDown(key);
        }
        //
        // Summary:
        //     Returns true during the frame the user releases the key identified by the key
        //     KeyCode enum parameter.
        //
        // Parameters:
        //   key:
        public static bool GetKeyUp(KeyCode key)
        {
            return !IsFocusedOnInputField && Input.GetKeyUp(key);
        }
        //
        // Summary:
        //     Returns true during the frame the user releases the key identified by name.
        //
        // Parameters:
        //   name:
        public static bool GetKeyUp(string name)
        {
            return !IsFocusedOnInputField && Input.GetKeyUp(name);
        }
    }
}
11
3
2

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
11
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?