Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
using System;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace PlayerCtrl
{
// helps with managing tilt input on mobile devices
public class TiltInput : MonoBehaviour
{
// options for the various orientations
public enum AxisOptions
{
ForwardAxis,
SidewaysAxis,
}
[Serializable]
public class AxisMapping
{
public enum MappingType
{
NamedAxis,
MousePositionX,
MousePositionY,
MousePositionZ
};
public MappingType type;
public string axisName;
}
public AxisMapping mapping;
public AxisOptions tiltAroundAxis = AxisOptions.ForwardAxis;
public float fullTiltAngle = 25;
public float centreAngleOffset = 0;
private CrossPlatformInputManager.VirtualAxis m_SteerAxis;
private void OnEnable()
{
if (mapping.type == AxisMapping.MappingType.NamedAxis)
{
m_SteerAxis = new CrossPlatformInputManager.VirtualAxis(mapping.axisName);
CrossPlatformInputManager.RegisterVirtualAxis(m_SteerAxis);
}
}
private void Update()
{
float angle = 0;
if (Input.acceleration != Vector3.zero)
{
switch (tiltAroundAxis)
{
case AxisOptions.ForwardAxis:
angle = Mathf.Atan2(Input.acceleration.x, -Input.acceleration.y) * Mathf.Rad2Deg +
centreAngleOffset;
break;
case AxisOptions.SidewaysAxis:
angle = Mathf.Atan2(Input.acceleration.z, -Input.acceleration.y) * Mathf.Rad2Deg +
centreAngleOffset;
break;
}
}
float axisValue = Mathf.InverseLerp(-fullTiltAngle, fullTiltAngle, angle) * 2 - 1;
switch (mapping.type)
{
case AxisMapping.MappingType.NamedAxis:
m_SteerAxis.Update(axisValue);
break;
case AxisMapping.MappingType.MousePositionX:
CrossPlatformInputManager.SetVirtualMousePositionX(axisValue * Screen.width);
break;
case AxisMapping.MappingType.MousePositionY:
CrossPlatformInputManager.SetVirtualMousePositionY(axisValue * Screen.width);
break;
case AxisMapping.MappingType.MousePositionZ:
CrossPlatformInputManager.SetVirtualMousePositionZ(axisValue * Screen.width);
break;
}
}
private void OnDisable()
{
m_SteerAxis.Remove();
}
}
}
namespace UnityStandardAssets.CrossPlatformInput.Inspector
{
#if UNITY_EDITOR
[CustomPropertyDrawer(typeof(TiltInput.AxisMapping))]
public class TiltInputAxisStylePropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.BeginProperty(position, label, property);
float x = position.x;
float y = position.y;
float inspectorWidth = position.width;
// Don't make child fields be indented
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
var props = new[] { "type", "axisName" };
var widths = new[] { .4f, .6f };
if (property.FindPropertyRelative("type").enumValueIndex > 0)
{
// hide name if not a named axis
props = new[] { "type" };
widths = new[] { 1f };
}
const float lineHeight = 18;
for (int n = 0; n < props.Length; ++n)
{
float w = widths[n] * inspectorWidth;
// Calculate rects
Rect rect = new Rect(x, y, w, lineHeight);
x += w;
EditorGUI.PropertyField(rect, property.FindPropertyRelative(props[n]), GUIContent.none);
}
// Set indent back to what it was
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}
}
#endif
}