i use Input.GetTouch(0).deltaPosition.x in moblie device, now i switch to windows?
how can i convert Input.GetTouch(0).deltaPosition.x to Input.GetMouseButtonDown(0) then drag left and right?
CodePudding user response:
There is no direct conversion between the two. I would create method, what returns the value, and implement the logic per platform. Something like this:
#if UNITY_STANDALONE
private Vector2 _prevPos;
#endif
private void Update()
{
if (IsPointer())
{
Vector2 delta = GetDeltaPos();
// do the work
}
}
private bool IsPointer()
{
#if UNITY_ANDROID
return Input.touchCount > 0;
#elif UNITY_STANDALONE
return Input.GetMouseButton(0);
#endif
}
private Vector2 GetDeltaPos()
{
#if UNITY_ANDROID
return Input.GetTouch(0).deltaPosition;
#elif UNITY_STANDALONE
Vector2 ret = Input.mousePosition - _prevPos;
_prevPos = Input.mousePosition;
return ret;
#endif
}
I know it's not nice, but works on both platforms.