CrossPlatformInputManager.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using UnityEngine;
  3. using UnityStandardAssets.CrossPlatformInput.PlatformSpecific;
  4. namespace UnityStandardAssets.CrossPlatformInput
  5. {
  6. public static class CrossPlatformInputManager
  7. {
  8. public enum ActiveInputMethod
  9. {
  10. Hardware,
  11. Touch
  12. }
  13. private static VirtualInput activeInput;
  14. private static VirtualInput s_TouchInput;
  15. private static VirtualInput s_HardwareInput;
  16. static CrossPlatformInputManager()
  17. {
  18. s_TouchInput = new MobileInput();
  19. s_HardwareInput = new StandaloneInput();
  20. #if MOBILE_INPUT
  21. activeInput = s_TouchInput;
  22. #else
  23. activeInput = s_HardwareInput;
  24. #endif
  25. }
  26. public static void SwitchActiveInputMethod(ActiveInputMethod activeInputMethod)
  27. {
  28. switch (activeInputMethod)
  29. {
  30. case ActiveInputMethod.Hardware:
  31. activeInput = s_HardwareInput;
  32. break;
  33. case ActiveInputMethod.Touch:
  34. activeInput = s_TouchInput;
  35. break;
  36. }
  37. }
  38. public static bool AxisExists(string name)
  39. {
  40. return activeInput.AxisExists(name);
  41. }
  42. public static bool ButtonExists(string name)
  43. {
  44. return activeInput.ButtonExists(name);
  45. }
  46. public static void RegisterVirtualAxis(VirtualAxis axis)
  47. {
  48. activeInput.RegisterVirtualAxis(axis);
  49. }
  50. public static void RegisterVirtualButton(VirtualButton button)
  51. {
  52. activeInput.RegisterVirtualButton(button);
  53. }
  54. public static void UnRegisterVirtualAxis(string name)
  55. {
  56. if (name == null)
  57. {
  58. throw new ArgumentNullException("name");
  59. }
  60. activeInput.UnRegisterVirtualAxis(name);
  61. }
  62. public static void UnRegisterVirtualButton(string name)
  63. {
  64. activeInput.UnRegisterVirtualButton(name);
  65. }
  66. // returns a reference to a named virtual axis if it exists otherwise null
  67. public static VirtualAxis VirtualAxisReference(string name)
  68. {
  69. return activeInput.VirtualAxisReference(name);
  70. }
  71. // returns the platform appropriate axis for the given name
  72. public static float GetAxis(string name)
  73. {
  74. return GetAxis(name, false);
  75. }
  76. public static float GetAxisRaw(string name)
  77. {
  78. return GetAxis(name, true);
  79. }
  80. // private function handles both types of axis (raw and not raw)
  81. private static float GetAxis(string name, bool raw)
  82. {
  83. return activeInput.GetAxis(name, raw);
  84. }
  85. // -- Button handling --
  86. public static bool GetButton(string name)
  87. {
  88. return activeInput.GetButton(name);
  89. }
  90. public static bool GetButtonDown(string name)
  91. {
  92. return activeInput.GetButtonDown(name);
  93. }
  94. public static bool GetButtonUp(string name)
  95. {
  96. return activeInput.GetButtonUp(name);
  97. }
  98. public static void SetButtonDown(string name)
  99. {
  100. activeInput.SetButtonDown(name);
  101. }
  102. public static void SetButtonUp(string name)
  103. {
  104. activeInput.SetButtonUp(name);
  105. }
  106. public static void SetAxisPositive(string name)
  107. {
  108. activeInput.SetAxisPositive(name);
  109. }
  110. public static void SetAxisNegative(string name)
  111. {
  112. activeInput.SetAxisNegative(name);
  113. }
  114. public static void SetAxisZero(string name)
  115. {
  116. activeInput.SetAxisZero(name);
  117. }
  118. public static void SetAxis(string name, float value)
  119. {
  120. activeInput.SetAxis(name, value);
  121. }
  122. public static Vector3 mousePosition
  123. {
  124. get { return activeInput.MousePosition(); }
  125. }
  126. public static void SetVirtualMousePositionX(float f)
  127. {
  128. activeInput.SetVirtualMousePositionX(f);
  129. }
  130. public static void SetVirtualMousePositionY(float f)
  131. {
  132. activeInput.SetVirtualMousePositionY(f);
  133. }
  134. public static void SetVirtualMousePositionZ(float f)
  135. {
  136. activeInput.SetVirtualMousePositionZ(f);
  137. }
  138. // virtual axis and button classes - applies to mobile input
  139. // Can be mapped to touch joysticks, tilt, gyro, etc, depending on desired implementation.
  140. // Could also be implemented by other input devices - kinect, electronic sensors, etc
  141. public class VirtualAxis
  142. {
  143. public string name { get; private set; }
  144. private float m_Value;
  145. public bool matchWithInputManager { get; private set; }
  146. public VirtualAxis(string name)
  147. : this(name, true)
  148. {
  149. }
  150. public VirtualAxis(string name, bool matchToInputSettings)
  151. {
  152. this.name = name;
  153. matchWithInputManager = matchToInputSettings;
  154. }
  155. // removes an axes from the cross platform input system
  156. public void Remove()
  157. {
  158. UnRegisterVirtualAxis(name);
  159. }
  160. // a controller gameobject (eg. a virtual thumbstick) should update this class
  161. public void Update(float value)
  162. {
  163. m_Value = value;
  164. }
  165. public float GetValue
  166. {
  167. get { return m_Value; }
  168. }
  169. public float GetValueRaw
  170. {
  171. get { return m_Value; }
  172. }
  173. }
  174. // a controller gameobject (eg. a virtual GUI button) should call the
  175. // 'pressed' function of this class. Other objects can then read the
  176. // Get/Down/Up state of this button.
  177. public class VirtualButton
  178. {
  179. public string name { get; private set; }
  180. public bool matchWithInputManager { get; private set; }
  181. private int m_LastPressedFrame = -5;
  182. private int m_ReleasedFrame = -5;
  183. private bool m_Pressed;
  184. public VirtualButton(string name)
  185. : this(name, true)
  186. {
  187. }
  188. public VirtualButton(string name, bool matchToInputSettings)
  189. {
  190. this.name = name;
  191. matchWithInputManager = matchToInputSettings;
  192. }
  193. // A controller gameobject should call this function when the button is pressed down
  194. public void Pressed()
  195. {
  196. if (m_Pressed)
  197. {
  198. return;
  199. }
  200. m_Pressed = true;
  201. m_LastPressedFrame = Time.frameCount;
  202. }
  203. // A controller gameobject should call this function when the button is released
  204. public void Released()
  205. {
  206. m_Pressed = false;
  207. m_ReleasedFrame = Time.frameCount;
  208. }
  209. // the controller gameobject should call Remove when the button is destroyed or disabled
  210. public void Remove()
  211. {
  212. UnRegisterVirtualButton(name);
  213. }
  214. // these are the states of the button which can be read via the cross platform input system
  215. public bool GetButton
  216. {
  217. get { return m_Pressed; }
  218. }
  219. public bool GetButtonDown
  220. {
  221. get
  222. {
  223. return m_LastPressedFrame - Time.frameCount == -1;
  224. }
  225. }
  226. public bool GetButtonUp
  227. {
  228. get
  229. {
  230. return (m_ReleasedFrame == Time.frameCount - 1);
  231. }
  232. }
  233. }
  234. }
  235. }