-
-
Save oleghwang/608fc2281cffa069c13b4f70b5d746f2 to your computer and use it in GitHub Desktop.
Fixed Button / Fixed Touch Field (in addition to https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.EventSystems; | |
public class FixedTouchField : MonoBehaviour, IPointerDownHandler, IPointerUpHandler | |
{ | |
[HideInInspector] | |
public Vector2 TouchDist; | |
[HideInInspector] | |
public Vector2 PointerOld; | |
[HideInInspector] | |
protected int PointerId; | |
[HideInInspector] | |
public bool Pressed; | |
// Use this for initialization | |
void Start() | |
{ | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
if (Pressed) | |
{ | |
if (PointerId >= 0 && PointerId < Input.touches.Length) | |
{ | |
TouchDist = Input.touches[PointerId].position - PointerOld; | |
PointerOld = Input.touches[PointerId].position; | |
} | |
else | |
{ | |
TouchDist = new Vector2(Input.mousePosition.x, Input.mousePosition.y) - PointerOld; | |
PointerOld = Input.mousePosition; | |
} | |
} | |
else | |
{ | |
TouchDist = new Vector2(); | |
} | |
} | |
public void OnPointerDown(PointerEventData eventData) | |
{ | |
Pressed = true; | |
PointerId = eventData.pointerId; | |
PointerOld = eventData.position; | |
} | |
public void OnPointerUp(PointerEventData eventData) | |
{ | |
Pressed = false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment