1 using System;
2 using
UnityEngine;
3 using
UnityEngine.EventSystems;
4
5 namespace
UnityStandardAssets.CrossPlatformInput
6 {
7     
public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
8     {
9         
public enum AxisOption
10         {
11             
// Options for which axes to use
12             Both,
// Use both
13             OnlyHorizontal,
// Only horizontal
14             OnlyVertical
// Only vertical
15         }
16
17         
public int MovementRange = 100;
18         
public AxisOption axesToUse = AxisOption.Both; // The options for the axes that the still will use
19         
public string horizontalAxisName = "Horizontal"; // The name given to the horizontal axis for the cross platform input
20         
public string verticalAxisName = "Vertical"; // The name given to the vertical axis for the cross platform input
21
22         Vector3 m_StartPos;
23         
bool m_UseX; // Toggle for using the x axis
24         
bool m_UseY; // Toggle for using the Y axis
25         CrossPlatformInputManager.VirtualAxis m_HorizontalVirtualAxis;
// Reference to the joystick in the cross platform input
26         CrossPlatformInputManager.VirtualAxis m_VerticalVirtualAxis;
// Reference to the joystick in the cross platform input
27
28         
void OnEnable()
29         {
30             CreateVirtualAxes();
31         }
32
33         
void Start()
34         {
35             m_StartPos = transform.position;
36         }
37
38         
void UpdateVirtualAxes(Vector3 value)
39         {
40             
var delta = m_StartPos - value;
41             delta.y = -delta.y;
42             delta /= MovementRange;
43             
if (m_UseX)
44             {
45                 m_HorizontalVirtualAxis.Update(-delta.x);
46             }
47
48             
if (m_UseY)
49             {
50                 m_VerticalVirtualAxis.Update(delta.y);
51             }
52         }
53
54         
void CreateVirtualAxes()
55         {
56             
// set axes to use
57             m_UseX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
58             m_UseY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);
59
60             
// create new axes based on axes to use
61             
if (m_UseX)
62             {
63                 m_HorizontalVirtualAxis =
new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
64                 CrossPlatformInputManager.RegisterVirtualAxis(m_HorizontalVirtualAxis);
65             }
66             
if (m_UseY)
67             {
68                 m_VerticalVirtualAxis =
new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
69                 CrossPlatformInputManager.RegisterVirtualAxis(m_VerticalVirtualAxis);
70             }
71         }
72
73
74         
public void OnDrag(PointerEventData data)
75         {
76             Vector3 newPos = Vector3.zero;
77
78             
if (m_UseX)
79             {
80                 
int delta = (int)(data.position.x - m_StartPos.x);
81                 delta = Mathf.Clamp(delta, - MovementRange, MovementRange);
82                 newPos.x = delta;
83             }
84
85             
if (m_UseY)
86             {
87                 
int delta = (int)(data.position.y - m_StartPos.y);
88                 delta = Mathf.Clamp(delta, -MovementRange, MovementRange);
89                 newPos.y = delta;
90             }
91             transform.position =
new Vector3(m_StartPos.x + newPos.x, m_StartPos.y + newPos.y, m_StartPos.z + newPos.z);
92             UpdateVirtualAxes(transform.position);
93         }
94
95
96         
public void OnPointerUp(PointerEventData data)
97         {
98             transform.position = m_StartPos;
99             UpdateVirtualAxes(m_StartPos);
100         }
101
102
103         
public void OnPointerDown(PointerEventData data) { }
104
105         
void OnDisable()
106         {
107             
// remove the joysticks from the cross platform input
108             
if (m_UseX)
109             {
110                 m_HorizontalVirtualAxis.Remove();
111             }
112             
if (m_UseY)
113             {
114                 m_VerticalVirtualAxis.Remove();
115             }
116         }
117     }
118 }