LoginSignup
2
1

More than 5 years have passed since last update.

Unity Inputのポインティングディバイス座標系

Last updated at Posted at 2019-04-16

画面左下原点シリーズ

  • CanvasのGlobal座標(rectTranform.postion
  • Input.mousePosition
  • Input.GetTouch(num).postion
  • Mouse.currnt.postion.ReadValue()
  • Touchscreen.current.primalyTouch.postion.ReadValue()

画面左上原点

  • Touchscreen.current.postion.ReadValue()

検証MEMO

因みに、PCではTouchscreenが、スマンホホではMouseがnullが返ってくる。
押していないときにはnullになるとかない(if(Input.touchCount>0)とか要らない)

using System.Text;
using UnityEngine;
using UnityEngine.Experimental.Input;


public class InputTest : MonoBehaviour
{
    void Update()
    {
        var mouse = Mouse.current;
        var mPhase = mouse?.phase?.ReadValue();
        var mButton = mouse?.leftButton?.isPressed;
        var mPos = mouse?.position?.ReadValue();
        var touch = Touchscreen.current;
        var tPhase = touch?.phase?.ReadValue();
        var tPos = touch?.position?.ReadValue();
        var touchP = Touchscreen.current?.primaryTouch;
        var tpPhase = touchP?.phase?.ReadValue();
        var tpPos = touchP?.position?.ReadValue();
        Debug.Log($"{mouse} {mPhase} {mButton} {mPos} / {touch} {tPhase} {tPos} / {touchP} {tpPhase} {tpPos}");
    }
}

実用MEMO

MouseもPhaseが取れればいいだが…一応メンバはある(Mouse.current.phase)が常にNoneなので注意。

PointerPhase phase;
Vector2 pos;
// #if ~ #endif はOmnSharpがたまにバグるのであまり好きじゃない
if(Application.platform == RuntimePlatform.Android
    || Application.platform == RuntimePlatform.IPhonePlayer)
{
    phase = Touchscreen.current.primaryTouch.phase.ReadValue();
    pos = Touchscreen.current.primaryTouch.position.ReadValue();
}
else
{   
    phase = 
        Mouse.current.leftButton.wasPressedThisFrame ? PointerPhase.Began :
        Mouse.current.leftButton.isPressed ? PointerPhase.Stationary :
        Mouse.current.leftButton.wasReleasedThisFrame ? PointerPhase.Ended :
        PointerPhase.None;
    pos = Mouse.current.position.ReadValue();
}

if(phase == PointerPhase.Began)
{
     // 押された瞬間
}
else if(phase == PointerPhase.Stationary || phase == PointerPhase.Moved)
{
     // 押してる間(押された瞬間を除く)
}
else if(phase == PointerPhase.Ended)
{
     // 離した瞬間
}
else // Cancelled || None
{
     // 離されているあいだ(話した瞬間を除く)
}

参考

Namespace UnityEngine.Experimental.Input

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