画面左下原点シリーズ
- 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
{
// 離されているあいだ(話した瞬間を除く)
}