1 using System;
2 using
UnityEngine;
3 using
UnityEngine.EventSystems;
4
5 namespace
UnityStandardAssets.CrossPlatformInput
6 {
7     
public class AxisTouchButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
8     {
9         
// designed to work in a pair with another axis touch button
10         
// (typically with one having -1 and one having 1 axisValues)
11         
public string axisName = "Horizontal"; // The name of the axis
12         
public float axisValue = 1; // The axis that the value has
13         
public float responseSpeed = 3; // The speed at which the axis touch button responds
14         
public float returnToCentreSpeed = 3; // The speed at which the button will return to its centre
15
16         AxisTouchButton m_PairedWith;
// Which button this one is paired with
17         CrossPlatformInputManager.VirtualAxis m_Axis;
// A reference to the virtual axis as it is in the cross platform input
18
19         
void OnEnable()
20         {
21             
if (!CrossPlatformInputManager.AxisExists(axisName))
22             {
23                 
// if the axis doesnt exist create a new one in cross platform input
24                 m_Axis =
new CrossPlatformInputManager.VirtualAxis(axisName);
25                 CrossPlatformInputManager.RegisterVirtualAxis(m_Axis);
26             }
27             
else
28             {
29                 m_Axis = CrossPlatformInputManager.VirtualAxisReference(axisName);
30             }
31             FindPairedButton();
32         }
33
34         
void FindPairedButton()
35         {
36             
// find the other button witch which this button should be paired
37             
// (it should have the same axisName)
38             
var otherAxisButtons = FindObjectsOfType(typeof(AxisTouchButton)) as AxisTouchButton[];
39
40             
if (otherAxisButtons != null)
41             {
42                 
for (int i = 0; i < otherAxisButtons.Length; i++)
43                 {
44                     
if (otherAxisButtons[i].axisName == axisName && otherAxisButtons[i] != this)
45                     {
46                         m_PairedWith = otherAxisButtons[i];
47                     }
48                 }
49             }
50         }
51
52         
void OnDisable()
53         {
54             
// The object is disabled so remove it from the cross platform input system
55             m_Axis.Remove();
56         }
57
58
59         
public void OnPointerDown(PointerEventData data)
60         {
61             
if (m_PairedWith == null)
62             {
63                 FindPairedButton();
64             }
65             
// update the axis and record that the button has been pressed this frame
66             m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue, axisValue, responseSpeed * Time.deltaTime));
67         }
68
69
70         
public void OnPointerUp(PointerEventData data)
71         {
72             m_Axis.Update(Mathf.MoveTowards(m_Axis.GetValue,
0, responseSpeed * Time.deltaTime));
73         }
74     }
75 }


Gõ tìm kiếm nhanh...