TiltInput.cs 4.4 KB

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