前提
- InputSystemパッケージを導入済み
- NewInputSystem-Ver1.0.0(preview)
InputActionアセットとか使わずに直接タッチを取得してスクリプトで処理しました。
色んなデバイスで使える処理なら直接入力を取得せずにInputActionアセットを編集して入力を受け取れるようにしておいた方が良い気もします。
ピンチインアウト処理はタッチ操作だけの処理なので直接タッチ入力を取得しました。
using UnityEngine;
using UnityEngine.InputSystem.EnhancedTouch;
//長いので省略する。
using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
using TouchPhase = UnityEngine.InputSystem.TouchPhase;
#pragma warning disable 0649
public class PinchInOut : MonoBehaviour
{
float lastDistance;
TouchInput _input;
private void Awake()
{
EnhancedTouchSupport.Enable();
}
// Update is called once per frame
void Update()
{
//2か所以上タップされていなければ何もしない
if( Touch.activeTouches.Count < 2) { return; }
var touch0 = Touch.activeTouches[0];
var touch1 = Touch.activeTouches[1];
//最初にタップされた時に距離を取得
if(touch1.phase == TouchPhase.Began)
{
lastDistance = Vector2.Distance(touch0.screenPosition, touch1.screenPosition);
}
//タップ2か所がどちらもドラッグしていない時は何もしない
if(!(touch0.phase == TouchPhase.Moved) && !(touch1.phase == TouchPhase.Moved))
{
return;
}
float distance = Vector2.Distance(touch0.screenPosition, touch1.screenPosition);
//前回のフレームとの差
OnPinch(distance - lastDistance);
lastDistance = distance;
}
void OnPinch(float delta)
{
//deltaがピンチインアウトした量
}
private void OnEnable()
{
EnhancedTouchSupport.Enable();
}
private void OnDisable()
{
//公式には何も書いてなかったけど一応Disableしてます
EnhancedTouchSupport.Disable();
}
}
補足:とりあえず入力を取得したい場合
using UnityEngine.InputSystem; ~~~~~~~~~~~~~~~~~~~~~~~~~~ Keyboard.current.aKey.wasPressedThisFrame; Mouse.current.leftButton.wasPressedThisFrame;
コード補完使えばakeyやleftButtonのところに取得したいボタン名があります。
コメント