Created
December 30, 2015 20:34
-
-
Save valbeat/69f3bf0ccf190594e945 to your computer and use it in GitHub Desktop.
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 System.Collections; | |
using UnityEngine.EventSystems; | |
using UnityStandardAssets.CrossPlatformInput; | |
using UnityEngine.UI; | |
public class PlayerJoystick : Joystick, InputGesture { | |
Image joystickImage; | |
EventSystem es; | |
public struct Range { | |
public int x; | |
public int y; | |
} | |
public Range movementRange; | |
protected override void UpdateVirtualAxes(Vector3 value) | |
{ | |
var delta = m_StartPos - value; | |
delta.y = -delta.y; | |
delta.x /= movementRange.x; | |
delta.y /= movementRange.y; | |
if (m_UseX) | |
{ | |
m_HorizontalVirtualAxis.Update(-delta.x); | |
} | |
if (m_UseY) | |
{ | |
m_VerticalVirtualAxis.Update(delta.y); | |
} | |
} | |
public override void Start() { | |
base.Start(); | |
SetupMovementRange(); | |
} | |
public override void OnEnable() { | |
base.OnEnable(); | |
InputGestureManager.Instance.RegisterGesture (this); | |
ShowImage(false); | |
} | |
public override void OnDisable() { | |
base.OnDisable(); | |
InputGestureManager.Instance.UnregisterGesture (this); | |
} | |
public override void OnPointerDown (PointerEventData data) { | |
base.OnPointerDown(data); | |
m_StartPos = transform.position = data.position; | |
ShowImage(true); | |
UpdateVirtualAxes(m_StartPos); | |
} | |
public override void OnPointerUp (PointerEventData data) { | |
base.OnPointerUp(data); | |
ShowImage(false); | |
} | |
public override void OnDrag (PointerEventData data) { | |
Vector3 newPos = Vector3.zero; | |
if (m_UseX) | |
{ | |
int delta = (int)(data.position.x - m_StartPos.x); | |
delta = Mathf.Clamp(delta, - movementRange.x, movementRange.x); | |
newPos.x = delta; | |
} | |
if (m_UseY) | |
{ | |
int delta = (int)(data.position.y - m_StartPos.y); | |
delta = Mathf.Clamp(delta, - movementRange.y, movementRange.y); | |
newPos.y = delta; | |
} | |
transform.position = new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z); | |
UpdateVirtualAxes(transform.position); | |
} | |
public int Order { | |
get { return 9999; } | |
} | |
public bool IsGestureProcess (GestureInfo info) { | |
return true; | |
} | |
public void OnGestureDown (GestureInfo info) { | |
OnPointerDown(GestureToPointer(info)); | |
} | |
public void OnGestureUp (GestureInfo info) { | |
OnPointerUp(GestureToPointer(info)); | |
} | |
public void OnGestureDrag (GestureInfo info) { | |
OnDrag(GestureToPointer(info)); | |
} | |
public void OnGestureFlick (GestureInfo info) { | |
} | |
public void DoMove (Vector3 world_delta) { | |
} | |
public void ShowImage (bool isShow) { | |
joystickImage = GetComponent<Image>(); | |
joystickImage.enabled = isShow; | |
} | |
private void SetupMovementRange () { | |
movementRange.x = Screen.width / 5; | |
movementRange.y = Screen.height / 10; | |
} | |
private PointerEventData GestureToPointer (GestureInfo info) { | |
if (es == null) | |
es = GameObject.FindObjectOfType<UnityEngine.EventSystems.EventSystem>(); | |
PointerEventData data = new PointerEventData(es); | |
data.position = info.ScreenPosition; | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment