1 using System;
2 using
UnityEngine;
3 #
if UNITY_EDITOR
4 using
UnityEditor;
5 #endif

6
7 namespace
UnityStandardAssets.CrossPlatformInput
8 {
9     
// helps with managing tilt input on mobile devices
10     
public class TiltInput : MonoBehaviour
11     {
12         
// options for the various orientations
13         
public enum AxisOptions
14         {
15             ForwardAxis,
16             SidewaysAxis,
17         }
18
19
20         
[Serializable]
21         
public class AxisMapping
22         {
23             
public enum MappingType
24             {
25                 NamedAxis,
26                 MousePositionX,
27                 MousePositionY,
28                 MousePositionZ
29             };
30
31
32             
public MappingType type;
33             
public string axisName;
34         }
35
36
37         
public AxisMapping mapping;
38         
public AxisOptions tiltAroundAxis = AxisOptions.ForwardAxis;
39         
public float fullTiltAngle = 25;
40         
public float centreAngleOffset = 0;
41
42
43         
private CrossPlatformInputManager.VirtualAxis m_SteerAxis;
44
45
46         
private void OnEnable()
47         {
48             
if (mapping.type == AxisMapping.MappingType.NamedAxis)
49             {
50                 m_SteerAxis =
new CrossPlatformInputManager.VirtualAxis(mapping.axisName);
51                 CrossPlatformInputManager.RegisterVirtualAxis(m_SteerAxis);
52             }
53         }
54
55
56         
private void Update()
57         {
58             
float angle = 0;
59             
if (Input.acceleration != Vector3.zero)
60             {
61                 
switch (tiltAroundAxis)
62                 {
63                     
case AxisOptions.ForwardAxis:
64                         angle = Mathf.Atan2(Input.acceleration.x, -Input.acceleration.y)*Mathf.Rad2Deg +
65                                 centreAngleOffset;
66                         
break;
67                     
case AxisOptions.SidewaysAxis:
68                         angle = Mathf.Atan2(Input.acceleration.z, -Input.acceleration.y)*Mathf.Rad2Deg +
69                                 centreAngleOffset;
70                         
break;
71                 }
72             }
73
74             
float axisValue = Mathf.InverseLerp(-fullTiltAngle, fullTiltAngle, angle)*2 - 1;
75             
switch (mapping.type)
76             {
77                 
case AxisMapping.MappingType.NamedAxis:
78                     m_SteerAxis.Update(axisValue);
79                     
break;
80                 
case AxisMapping.MappingType.MousePositionX:
81                     CrossPlatformInputManager.SetVirtualMousePositionX(axisValue*Screen.width);
82                     
break;
83                 
case AxisMapping.MappingType.MousePositionY:
84                     CrossPlatformInputManager.SetVirtualMousePositionY(axisValue*Screen.width);
85                     
break;
86                 
case AxisMapping.MappingType.MousePositionZ:
87                     CrossPlatformInputManager.SetVirtualMousePositionZ(axisValue*Screen.width);
88                     
break;
89             }
90         }
91
92
93         
private void OnDisable()
94         {
95             m_SteerAxis.Remove();
96         }
97     }
98 }

99
100
101 namespace
UnityStandardAssets.CrossPlatformInput.Inspector
102 {
103 #
if UNITY_EDITOR
104     
[CustomPropertyDrawer(typeof (TiltInput.AxisMapping))]
105     
public class TiltInputAxisStylePropertyDrawer : PropertyDrawer
106     {
107         
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
108         {
109             EditorGUI.BeginProperty(position, label, property);
110
111             
float x = position.x;
112             
float y = position.y;
113             
float inspectorWidth = position.width;
114
115             
// Don't make child fields be indented
116             
var indent = EditorGUI.indentLevel;
117             EditorGUI.indentLevel =
0;
118
119             
var props = new[] {"type", "axisName"};
120             
var widths = new[] {.4f, .6f};
121             
if (property.FindPropertyRelative("type").enumValueIndex > 0)
122             {
123                 
// hide name if not a named axis
124                 props =
new[] {"type"};
125                 widths =
new[] {1f};
126             }
127             
const float lineHeight = 18;
128             
for (int n = 0; n < props.Length; ++n)
129             {
130                 
float w = widths[n]*inspectorWidth;
131
132                 
// Calculate rects
133                 Rect rect =
new Rect(x, y, w, lineHeight);
134                 x += w;
135
136                 EditorGUI.PropertyField(rect, property.FindPropertyRelative(props[n]), GUIContent.none);
137             }
138
139             
// Set indent back to what it was
140             EditorGUI.indentLevel = indent;
141             EditorGUI.EndProperty();
142         }
143     }
144 #endif
145 }


Tìm kết quả nhanh