Simple input system for Unity
Lucid InputはUnityのInputクラスを拡張し、同時押しやフリック、ダブルクリックなどの高度な入力を従来のInputクラスのようにコード一行で取得することを可能にします。
- シンプルで扱いやすいシステム
- 同時押しやフリックなどの高度な入力に対応
- Input Systemに対応、UnityEngine.Inputとの切り替えが可能
- Unity 2019.4 以上
- Window > Package ManagerからPackage Managerを開く
- 「+」ボタン > Add package from git URL
- 以下を入力する
あるいはPackages/manifest.jsonを開き、dependenciesブロックに以下を追記
{
"dependencies": {
"com.annulusgames.lucid-input": "https://github.com/AnnulusGames/LucidInput.git?path=/Assets/LucidInput"
}
}LucidInputを利用する場合は、ファイルの冒頭に以下の一行を追加します。
using AnnulusGames.LucidTools.InputSystem;Input Systemを導入すると、Lucid Inputは自動的にInput SyetemのAPIを利用するように切り替わります。もしLucid Inputがどちらを利用するかを切り替えたい場合は、LucidInput.activeInputHandlingを変更してください。
//InputManager(UnityEngine.Input)を使用
LucidInput.activeInputHandling = InputHandlingMode.InputManager;
//InputSystemを使用
LucidInput.activeInputHandling = InputHandlingMode.InputSystem;ゲームパッドでの入力を行いたい場合は、Input Systemの導入が必須になります。
キーの入力を取得する場合、以下のように記述します。
if (LucidInput.GetKey(Key.Space))
{
Debug.Log("space key is held down");
}LucidInputでは、独自の列挙型「Key」を用いてキーを識別します。 Keyは、以下のようにKeyCodeやUnityEngine.InputSystem.Keyに変換可能です。
Key spaceKey = Key.Space;
//Key -> KeyCodeに変換
if (Input.GetKeyDown(spaceKey.ToKeyCode()))
{
Debug.Log("space key was pressed");
}
//Key -> UnityEngine.InputSystem.Keyに変換
if (Keyboard.current[spaceKey.ToISKey()].wasPressedThisFrame)
{
Debug.Log("space key was pressed");
}押された瞬間や離された瞬間を取得したい場合は、従来のInputクラスと同様にGetKeyDown、GetKeyUpを利用します。
if (LucidInput.GetKeyDown(Key.A))
{
Debug.Log("A key was pressed");
}
if (LucidInput.GetKeyUp(Key.B))
{
Debug.Log("B key was released");
}複数のキーを判別する場合はGetKeyAny、GetKeyAllを利用します。
//A、B、Cのいずれかのキーが押されている
if (LucidInput.GetKeyAny(Key.A, Key.B, Key.C))
{
Debug.Log("A key or B key or C key is held down");
}
//AキーとBキーが同時に押された
if (LucidInput.GetKeyDownAll(Key.A, Key.B))
{
Debug.Log("A key and B key was released");
}同時押しを判定する時間を調整したい場合は、LucidInput.simultaneousPressIntervalの値を変更します。(初期値は0.1[s])
//同時押しを判定する時間を0.15sに変更
LucidInput.simultaneousPressInterval = 0.15f;キーが一定の時間押された後に離された(タップされた)瞬間を取得した場合は、GetKeyTapを利用します。
//Spaceキーが一定の時間押された後に離された
if (LucidInput.GetKeyTap(Key.Space))
{
Debug.Log("tap");
}ダブルタップを取得する場合はGetKeyDoubleTap、それ以上の回数のタップを取得する場合はGetKeyMultiTapを利用します。
//ダブルタップ
if (LucidInput.GetKeyDouleTap(Key.Space))
{
Debug.Log("double tap");
}
//4回のタップ
if (LucidInput.GetKeyMultiTap(Key.Space, 4))
{
Debug.Log("4 tap");
}また、タップされた回数を取得したい場合はGetKeyTapCountを利用します。
int tapCount = LucidInput.GetKeyTapCount(Key.Space);タップを判定する時間を調整したい場合はLucidInput.tapTime、複数回のタップの間隔を変更したい場合はLucidInput.tapIntervalの値を変更します。(初期値はどちらも0.2[s])
LucidInput.tapTime = 0.25f;
LucidInput.tapInterval = 0.15f;キーの長押しを取得する場合は、GetKeyHold、GetKeyHoldEndedを利用します。
//スペースキーが1.0秒押された後、離されるまで毎フレームtrueを返す
if (LucidInput.GetKeyHold(Key.Space, 1.0f))
{
Debug.Log("hold");
}
//スペースキーが1.0秒押された後、離された瞬間にtrueを返す
if (LucidInput.GetKeyHoldEnded(Key.Space, 1.0f))
{
Debug.Log("hold ended");
}また、キーが押されている時間を取得したい場合はGetKeyHoldTimeを利用します。
float time = LucidInput.GetKeyHoldTime(Key.Space);マウスのボタンの入力は以下のように行います。
if (LucidInput.GetMouseButton(0))
{
Debug.Log("Pressed left click.");
}
if (LucidInput.GetMouseButton(1))
{
Debug.Log("Pressed right click.");
}
if (LucidInput.GetMouseButton(2))
{
Debug.Log("Pressed middle click.");
}マウスのボタンもタップやホールドに対応しています。
if (LucidInput.GetMouseButtonDoubleTap(0))
{
Debug.Log("double click.");
}
if (LucidInput.GetMouseButtonHoldEnded(1))
{
Debug.Log("right button hold");
}マウスを利用したフリック操作を取得することも可能です。
//上下左右のいずれかの方向にフリック
if (LucidInput.GetMouseFlick(0))
{
Debug.Log("flick");
}
//指定された方向にフリック
if (LucidInput.GetMouseFlick(0, Direction.Right))
{
Debug.Log("right flick");
}フリック操作を判定する距離や時間については、それぞれLucidInput.flickMinDistance、LucidInput.flickTimeを変更することで調整が可能です。
LucidInput.flickMinDistance = 80;
LucidInput.flickTime = 0.25f;デフォルトではマウス操作をタッチでシミュレートするように設定されているため、GetMouse系のメソッドはタッチスクリーンでも動作します。無効にしたい場合は、LucidInput.simulateMouseWithTouchesをfalseに設定してください。
LucidInput.simulateMouseWithTouches = false;マウスポインターの位置や移動量を取得したい場合は、以下のように記述します。
//マウスポインターの位置をスクリーン座標で取得
Vector2 position = LucidInput.mousePosition;
//マウスポインターの移動量を取得
Vector2 delta = LucidInput.mousePositionDelta;マウスホイールの変化量を取得する場合はmouseScrollDeltaを利用します。
//マウスホイールの変化量を取得
Vector2 mouseWheelDelta = LucidInput.mouseScrollDelta;※Input Systemを使用する場合、タッチ情報の取得にはEnhancedTouchを利用するためEnhancedTouchSupportが有効化されている必要があります。(activeInputHandlingをInputSystemに設定すると自動で有効化されます)
画面のタッチの情報を取得したい場合は以下のように記述します。
//画面上のタッチの数だけ繰り返す
for (int i = 0; i < LucidInput.touchCount; i++)
{
//タッチ情報を取得 (画面上にタッチが存在しない場合はnullを返す)
Touch touch = LucidInput.GetTouch(i);
}また、タッチもホールドやダブルタップなどに対応しています。それらを取得したい場合は、GetTouchButtonを利用します。
if (LucidInput.GetTouchButtonHold(0, 1.0f))
{
Debug.Log("hold");
}
if (LucidInput.GetTouchButtonDoubleTap(0))
{
Debug.Log("double tap");
}フリック操作を取得したい場合はGetTouchFlickを利用します。
if (LucidInput.GetTouchFlick(0, Direction.Left))
{
Debug.Log("left flick");
}ピンチ操作(画面に2本の指をタッチして広げる・狭める動作)にも対応しています。ピンチ操作の変化量を取得したい場合は、pinchDeltaから取得できます。(画面に2本以上の指がない場合は0を返します)
float delta = LucidInput.pinchDelta;※ゲームパッドの入力にはInput Systemの導入が必須です。LucidInput.activeInputHandlingがInputSystemに設定されている場合にのみ動作します。
ゲームパッドのボタンの入力を取得する場合は、GetGamepadButtonを使用します。
if (LucidInput.GetGamepadButtonDown(GamepadButton.A))
{
Debug.Log("A button was pressed");
}ゲームパッドのボタンも他のボタン同様、高度な入力に対応しています。
if (LucidInput.GetGamepadButtonHoldEnded(GamepadButton.RightTrigger, 1f))
{
Debug.Log("hold ended");
}
if (LucidInput.GetGamepadButtonDoubleTap(GamepadButton.DpadDown))
{
Debug.Log("double tap");
}また、GetKeyと同様にAny、Allも利用できます。
if (LucidInput.GetGamepadButtonDownAny(GamepadButton.A, GamepadButton.B))
{
Debug.Log("A button or B button was pressed");
}
if (LucidInput.GetGamepadButtonDownAll(GamepadButton.RightTrigger, GamepadButton.LeftTrigger))
{
Debug.Log("RT and LT was pressed");
}スティックの値やトリガーの値を取得したい場合は、以下のように記述します。
//左スティックの値を取得
Vector2 stickValue = LucidInput.GetGamepadStick(LR.Left);
//右トリガーの値を取得
float triggerValue = LucidInput.GetGamepadTrigger(LR.Right);Lucid Inputでは以下の5つのAxisが利用可能です。
- Horizontal
- Vertical
- MouseX
- MouseY
- MouseScrollWheel
//キー入力の場合、平滑化された値を返す
float value = LucidInput.GetAxis(Axis.Horizontal);
//-1, 0, 1のいずれかを返す
float valueRaw = LucidInput.GetAxisRaw(Axis.Vertical);GetAxisによって取得できる値は独自の計算方式で算出されるため、従来のInput.GetAxisとは値が異なる場合があります。
