XRDeviceSimulator.cs 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Assertions;
  4. using UnityEngine.InputSystem;
  5. using UnityEngine.InputSystem.Controls;
  6. using UnityEngine.InputSystem.LowLevel;
  7. using UnityEngine.XR;
  8. namespace SC.XR.Unity.Simulation {
  9. /// <summary>
  10. /// A component which handles mouse and keyboard input from the user and uses it to
  11. /// drive simulated XR controllers and an XR head mounted display (HMD).
  12. /// </summary>
  13. /// <remarks>
  14. /// This class does not directly manipulate the camera or controllers which are part of
  15. /// the XR Origin, but rather drives them indirectly through simulated input devices.
  16. /// <br /><br />
  17. /// Use the Package Manager window to install the <i>XR Device Simulator</i> sample into
  18. /// your project to get sample mouse and keyboard bindings for Input System actions that
  19. /// this component expects. The sample also includes a prefab of a <see cref="GameObject"/>
  20. /// with this component attached that has references to those sample actions already set.
  21. /// To make use of this simulator, add the prefab to your scene (the prefab makes use
  22. /// of <see cref="InputActionManager"/> to ensure the Input System actions are enabled).
  23. /// <br /><br />
  24. /// Note that the XR Origin must read the position and rotation of the HMD and controllers
  25. /// by using Input System actions (such as by using <see cref="ActionBasedController"/>
  26. /// and <see cref="TrackedPoseDriver"/>) for this simulator to work as expected.
  27. /// Attempting to use XR input subsystem device methods (such as by using <see cref="XRController"/>
  28. /// and <see cref="SpatialTracking.TrackedPoseDriver"/>) will not work as expected
  29. /// since this simulator depends on the Input System to drive the simulated devices.
  30. /// </remarks>
  31. /// <seealso cref="XRSimulatedController"/>
  32. /// <seealso cref="XRSimulatedHMD"/>
  33. /// <seealso cref="SimulatedInputLayoutLoader"/>
  34. [AddComponentMenu("XR/Debug/XR Device Simulator", 11)]
  35. // [DefaultExecutionOrder(XRInteractionUpdateOrder.k_DeviceSimulator)]
  36. public class XRDeviceSimulator : MonoBehaviour
  37. {
  38. /// <summary>
  39. /// The coordinate space in which to operate.
  40. /// </summary>
  41. /// <seealso cref="keyboardTranslateSpace"/>
  42. /// <seealso cref="mouseTranslateSpace"/>
  43. public enum Space
  44. {
  45. /// <summary>
  46. /// Applies translations of a controller or HMD relative to its own coordinate space, considering its own rotations.
  47. /// Will translate a controller relative to itself, independent of the camera.
  48. /// </summary>
  49. Local,
  50. /// <summary>
  51. /// Applies translations of a controller or HMD relative to its parent. If the object does not have a parent, meaning
  52. /// it is a root object, the parent coordinate space is the same as the world coordinate space. This is the same
  53. /// as <see cref="Local"/> but without considering its own rotations.
  54. /// </summary>
  55. Parent,
  56. /// <summary>
  57. /// Applies translations of a controller or HMD relative to the screen.
  58. /// Will translate a controller relative to the camera, independent of the controller's orientation.
  59. /// </summary>
  60. Screen,
  61. }
  62. /// <summary>
  63. /// The transformation mode in which to operate.
  64. /// </summary>
  65. /// <seealso cref="mouseTransformationMode"/>
  66. public enum TransformationMode
  67. {
  68. /// <summary>
  69. /// Applies translations from input.
  70. /// </summary>
  71. Translate,
  72. /// <summary>
  73. /// Applies rotations from input.
  74. /// </summary>
  75. Rotate,
  76. }
  77. /// <summary>
  78. /// The target device control(s) to update from input.
  79. /// </summary>
  80. /// <remarks>
  81. /// <see cref="FlagsAttribute"/> to support updating multiple controls from input
  82. /// (e.g. to drive the primary and secondary 2D axis on a controller from the same input).
  83. /// </remarks>
  84. /// <seealso cref="axis2DTargets"/>
  85. [Flags]
  86. public enum Axis2DTargets
  87. {
  88. /// <summary>
  89. /// Do not update device state from input.
  90. /// </summary>
  91. None = 0,
  92. /// <summary>
  93. /// Update device position from input.
  94. /// </summary>
  95. Position = 1 << 0,
  96. /// <summary>
  97. /// Update the primary touchpad or joystick on a controller device from input.
  98. /// </summary>
  99. Primary2DAxis = 1 << 1,
  100. /// <summary>
  101. /// Update the secondary touchpad or joystick on a controller device from input.
  102. /// </summary>
  103. Secondary2DAxis = 1 << 2,
  104. }
  105. [SerializeField]
  106. [Tooltip("The Input System Action used to translate in the x-axis (left/right) while held. Must be a Value Axis Control.")]
  107. InputActionReference m_KeyboardXTranslateAction;
  108. /// <summary>
  109. /// The Input System Action used to translate in the x-axis (left/right) while held.
  110. /// Must be a <see cref="InputActionType.Value"/> <see cref="AxisControl"/>.
  111. /// </summary>
  112. public InputActionReference keyboardXTranslateAction
  113. {
  114. get => m_KeyboardXTranslateAction;
  115. set
  116. {
  117. UnsubscribeKeyboardXTranslateAction();
  118. m_KeyboardXTranslateAction = value;
  119. SubscribeKeyboardXTranslateAction();
  120. }
  121. }
  122. [SerializeField]
  123. [Tooltip("The Input System Action used to translate in the y-axis (up/down) while held. Must be a Value Axis Control.")]
  124. InputActionReference m_KeyboardYTranslateAction;
  125. /// <summary>
  126. /// The Input System Action used to translate in the y-axis (up/down) while held.
  127. /// Must be a <see cref="InputActionType.Value"/> <see cref="AxisControl"/>.
  128. /// </summary>
  129. public InputActionReference keyboardYTranslateAction
  130. {
  131. get => m_KeyboardYTranslateAction;
  132. set
  133. {
  134. UnsubscribeKeyboardYTranslateAction();
  135. m_KeyboardYTranslateAction = value;
  136. SubscribeKeyboardYTranslateAction();
  137. }
  138. }
  139. [SerializeField]
  140. [Tooltip("The Input System Action used to translate in the z-axis (forward/back) while held. Must be a Value Axis Control.")]
  141. InputActionReference m_KeyboardZTranslateAction;
  142. /// <summary>
  143. /// The Input System Action used to translate in the z-axis (forward/back) while held.
  144. /// Must be a <see cref="InputActionType.Value"/> <see cref="AxisControl"/>.
  145. /// </summary>
  146. public InputActionReference keyboardZTranslateAction
  147. {
  148. get => m_KeyboardZTranslateAction;
  149. set
  150. {
  151. UnsubscribeKeyboardZTranslateAction();
  152. m_KeyboardZTranslateAction = value;
  153. SubscribeKeyboardZTranslateAction();
  154. }
  155. }
  156. [SerializeField]
  157. [Tooltip("The Input System Action used to enable manipulation of the left-hand controller while held. Must be a Button Control.")]
  158. InputActionReference m_ManipulateLeftAction;
  159. /// <summary>
  160. /// The Input System Action used to enable manipulation of the left-hand controller while held.
  161. /// Must be a <see cref="ButtonControl"/>.
  162. /// </summary>
  163. /// <remarks>
  164. /// Note that if controls on the left-hand controller are actuated when this action is released,
  165. /// those controls will continue to remain actuated. This is to allow for multi-hand interactions
  166. /// without needing to have dedicated bindings for manipulating each controller separately and concurrently.
  167. /// </remarks>
  168. /// <seealso cref="manipulateRightAction"/>
  169. /// <seealso cref="toggleManipulateLeftAction"/>
  170. public InputActionReference manipulateLeftAction
  171. {
  172. get => m_ManipulateLeftAction;
  173. set
  174. {
  175. UnsubscribeManipulateLeftAction();
  176. m_ManipulateLeftAction = value;
  177. SubscribeManipulateLeftAction();
  178. }
  179. }
  180. [SerializeField]
  181. [Tooltip("The Input System Action used to enable manipulation of the right-hand controller while held. Must be a Button Control.")]
  182. InputActionReference m_ManipulateRightAction;
  183. /// <summary>
  184. /// The Input System Action used to enable manipulation of the right-hand controller while held.
  185. /// Must be a <see cref="ButtonControl"/>.
  186. /// </summary>
  187. /// <remarks>
  188. /// Note that if controls on the right-hand controller are actuated when this action is released,
  189. /// those controls will continue to remain actuated. This is to allow for multi-hand interactions
  190. /// without needing to have dedicated bindings for manipulating each controller separately and concurrently.
  191. /// </remarks>
  192. /// <seealso cref="manipulateLeftAction"/>
  193. public InputActionReference manipulateRightAction
  194. {
  195. get => m_ManipulateRightAction;
  196. set
  197. {
  198. UnsubscribeManipulateRightAction();
  199. m_ManipulateRightAction = value;
  200. SubscribeManipulateRightAction();
  201. }
  202. }
  203. [SerializeField]
  204. [Tooltip("The Input System Action used to toggle enable manipulation of the left-hand controller when pressed. Must be a Button Control.")]
  205. InputActionReference m_ToggleManipulateLeftAction;
  206. /// <summary>
  207. /// The Input System Action used to toggle enable manipulation of the left-hand controller when pressed.
  208. /// Must be a <see cref="ButtonControl"/>.
  209. /// </summary>
  210. /// <seealso cref="manipulateLeftAction"/>
  211. /// <seealso cref="toggleManipulateRightAction"/>
  212. public InputActionReference toggleManipulateLeftAction
  213. {
  214. get => m_ToggleManipulateLeftAction;
  215. set
  216. {
  217. UnsubscribeToggleManipulateLeftAction();
  218. m_ToggleManipulateLeftAction = value;
  219. SubscribeToggleManipulateLeftAction();
  220. }
  221. }
  222. [SerializeField]
  223. [Tooltip("The Input System Action used to toggle enable manipulation of the right-hand controller when pressed. Must be a Button Control.")]
  224. InputActionReference m_ToggleManipulateRightAction;
  225. /// <summary>
  226. /// The Input System Action used to toggle enable manipulation of the right-hand controller when pressed.
  227. /// Must be a <see cref="ButtonControl"/>.
  228. /// </summary>
  229. /// <seealso cref="manipulateRightAction"/>
  230. /// <seealso cref="toggleManipulateLeftAction"/>
  231. public InputActionReference toggleManipulateRightAction
  232. {
  233. get => m_ToggleManipulateRightAction;
  234. set
  235. {
  236. UnsubscribeToggleManipulateRightAction();
  237. m_ToggleManipulateRightAction = value;
  238. SubscribeToggleManipulateRightAction();
  239. }
  240. }
  241. [SerializeField]
  242. [Tooltip("The Input System Action used to enable manipulation of the HMD while held. Must be a Button Control.")]
  243. InputActionReference m_ManipulateHeadAction;
  244. /// <summary>
  245. /// The Input System Action used to enable manipulation of the HMD while held.
  246. /// Must be a <see cref="ButtonControl"/>.
  247. /// </summary>
  248. public InputActionReference manipulateHeadAction
  249. {
  250. get => m_ManipulateHeadAction;
  251. set
  252. {
  253. UnsubscribeManipulateHeadAction();
  254. m_ManipulateHeadAction = value;
  255. SubscribeManipulateHeadAction();
  256. }
  257. }
  258. [SerializeField]
  259. [Tooltip("The Input System Action used to translate or rotate by a scaled amount along or about the x- and y-axes. Must be a Value Vector2 Control.")]
  260. InputActionReference m_MouseDeltaAction;
  261. /// <summary>
  262. /// The Input System Action used to translate or rotate by a scaled amount along or about the x- and y-axes.
  263. /// Must be a <see cref="InputActionType.Value"/> <see cref="Vector2Control"/>.
  264. /// </summary>
  265. /// <remarks>
  266. /// Typically bound to the screen-space motion delta of the mouse in pixels.
  267. /// </remarks>
  268. /// <seealso cref="mouseScrollAction"/>
  269. public InputActionReference mouseDeltaAction
  270. {
  271. get => m_MouseDeltaAction;
  272. set
  273. {
  274. UnsubscribeMouseDeltaAction();
  275. m_MouseDeltaAction = value;
  276. SubscribeMouseDeltaAction();
  277. }
  278. }
  279. [SerializeField]
  280. [Tooltip("The Input System Action used to translate or rotate by a scaled amount along or about the z-axis. Must be a Value Vector2 Control.")]
  281. InputActionReference m_MouseScrollAction;
  282. /// <summary>
  283. /// The Input System Action used to translate or rotate by a scaled amount along or about the z-axis.
  284. /// Must be a <see cref="InputActionType.Value"/> <see cref="Vector2Control"/>.
  285. /// </summary>
  286. /// <remarks>
  287. /// Typically bound to the horizontal and vertical scroll wheels, though only the vertical is used.
  288. /// </remarks>
  289. /// <seealso cref="mouseDeltaAction"/>
  290. public InputActionReference mouseScrollAction
  291. {
  292. get => m_MouseScrollAction;
  293. set
  294. {
  295. UnsubscribeMouseScrollAction();
  296. m_MouseScrollAction = value;
  297. SubscribeMouseScrollAction();
  298. }
  299. }
  300. [SerializeField]
  301. [Tooltip("The Input System Action used to cause the manipulated device(s) to rotate when moving the mouse when held. Must be a Button Control.")]
  302. InputActionReference m_RotateModeOverrideAction;
  303. /// <summary>
  304. /// The Input System Action used to cause the manipulated device(s) to rotate when moving the mouse when held.
  305. /// Must be a <see cref="ButtonControl"/>.
  306. /// </summary>
  307. /// <remarks>
  308. /// Forces rotation mode when held, no matter what the current mouse transformation mode is.
  309. /// </remarks>
  310. /// <seealso cref="negateModeAction"/>
  311. public InputActionReference rotateModeOverrideAction
  312. {
  313. get => m_RotateModeOverrideAction;
  314. set
  315. {
  316. UnsubscribeRotateModeOverrideAction();
  317. m_RotateModeOverrideAction = value;
  318. SubscribeRotateModeOverrideAction();
  319. }
  320. }
  321. [SerializeField]
  322. [Tooltip("The Input System Action used to toggle between translating or rotating the manipulated device(s) when moving the mouse when pressed. Must be a Button Control.")]
  323. InputActionReference m_ToggleMouseTransformationModeAction;
  324. /// <summary>
  325. /// The Input System Action used to toggle between translating or rotating the manipulated device(s)
  326. /// when moving the mouse when pressed.
  327. /// Must be a <see cref="ButtonControl"/>.
  328. /// </summary>
  329. public InputActionReference toggleMouseTransformationModeAction
  330. {
  331. get => m_ToggleMouseTransformationModeAction;
  332. set
  333. {
  334. UnsubscribeToggleMouseTransformationModeAction();
  335. m_ToggleMouseTransformationModeAction = value;
  336. SubscribeToggleMouseTransformationModeAction();
  337. }
  338. }
  339. [SerializeField]
  340. [Tooltip("The Input System Action used to cause the manipulated device(s) to rotate when moving the mouse while held when it would normally translate, and vice-versa. Must be a Button Control.")]
  341. InputActionReference m_NegateModeAction;
  342. /// <summary>
  343. /// The Input System Action used to cause the manipulated device(s) to rotate when moving the mouse
  344. /// while held when it would normally translate, and vice-versa.
  345. /// Must be a <see cref="ButtonControl"/>.
  346. /// </summary>
  347. /// <remarks>
  348. /// Can be used to temporarily change the mouse transformation mode to the other mode while held
  349. /// for making quick adjustments.
  350. /// </remarks>
  351. /// <seealso cref="toggleMouseTransformationModeAction"/>
  352. public InputActionReference negateModeAction
  353. {
  354. get => m_NegateModeAction;
  355. set
  356. {
  357. UnsubscribeNegateModeAction();
  358. m_NegateModeAction = value;
  359. SubscribeNegateModeAction();
  360. }
  361. }
  362. [SerializeField]
  363. [Tooltip("The Input System Action used to constrain the translation or rotation to the x-axis when moving the mouse or resetting. May be combined with another axis constraint to constrain to a plane. Must be a Button Control.")]
  364. InputActionReference m_XConstraintAction;
  365. /// <summary>
  366. /// The Input System Action used to constrain the translation or rotation to the x-axis when moving the mouse or resetting.
  367. /// May be combined with another axis constraint to constrain to a plane.
  368. /// Must be a <see cref="ButtonControl"/>.
  369. /// </summary>
  370. /// <seealso cref="yConstraintAction"/>
  371. /// <seealso cref="zConstraintAction"/>
  372. public InputActionReference xConstraintAction
  373. {
  374. get => m_XConstraintAction;
  375. set
  376. {
  377. UnsubscribeXConstraintAction();
  378. m_XConstraintAction = value;
  379. SubscribeXConstraintAction();
  380. }
  381. }
  382. [SerializeField]
  383. [Tooltip("The Input System Action used to constrain the translation or rotation to the y-axis when moving the mouse or resetting. May be combined with another axis constraint to constrain to a plane. Must be a Button Control.")]
  384. InputActionReference m_YConstraintAction;
  385. /// <summary>
  386. /// The Input System Action used to constrain the translation or rotation to the y-axis when moving the mouse or resetting.
  387. /// May be combined with another axis constraint to constrain to a plane.
  388. /// Must be a <see cref="ButtonControl"/>.
  389. /// </summary>
  390. /// <seealso cref="xConstraintAction"/>
  391. /// <seealso cref="zConstraintAction"/>
  392. public InputActionReference yConstraintAction
  393. {
  394. get => m_YConstraintAction;
  395. set
  396. {
  397. UnsubscribeYConstraintAction();
  398. m_YConstraintAction = value;
  399. SubscribeYConstraintAction();
  400. }
  401. }
  402. [SerializeField]
  403. [Tooltip("The Input System Action used to constrain the translation or rotation to the z-axis when moving the mouse or resetting. May be combined with another axis constraint to constrain to a plane. Must be a Button Control.")]
  404. InputActionReference m_ZConstraintAction;
  405. /// <summary>
  406. /// The Input System Action used to constrain the translation or rotation to the z-axis when moving the mouse or resetting.
  407. /// May be combined with another axis constraint to constrain to a plane.
  408. /// Must be a <see cref="ButtonControl"/>.
  409. /// </summary>
  410. /// <seealso cref="xConstraintAction"/>
  411. /// <seealso cref="yConstraintAction"/>
  412. public InputActionReference zConstraintAction
  413. {
  414. get => m_ZConstraintAction;
  415. set
  416. {
  417. UnsubscribeZConstraintAction();
  418. m_ZConstraintAction = value;
  419. SubscribeZConstraintAction();
  420. }
  421. }
  422. [SerializeField]
  423. [Tooltip("The Input System Action used to cause the manipulated device(s) to reset position or rotation (depending on the effective manipulation mode). Must be a Button Control.")]
  424. InputActionReference m_ResetAction;
  425. /// <summary>
  426. /// The Input System Action used to cause the manipulated device(s) to reset position or rotation
  427. /// (depending on the effective manipulation mode).
  428. /// Must be a <see cref="ButtonControl"/>.
  429. /// </summary>
  430. /// <remarks>
  431. /// Resets position to <see cref="Vector3.zero"/> and rotation to <see cref="Quaternion.identity"/>.
  432. /// May be combined with axis constraints (<see cref="xConstraintAction"/>, <see cref="yConstraintAction"/>, and <see cref="zConstraintAction"/>).
  433. /// </remarks>
  434. public InputActionReference resetAction
  435. {
  436. get => m_ResetAction;
  437. set
  438. {
  439. UnsubscribeResetAction();
  440. m_ResetAction = value;
  441. SubscribeResetAction();
  442. }
  443. }
  444. [SerializeField]
  445. [Tooltip("The Input System Action used to toggle the cursor lock mode for the game window when pressed. Must be a Button Control.")]
  446. InputActionReference m_ToggleCursorLockAction;
  447. /// <summary>
  448. /// The Input System Action used to toggle the cursor lock mode for the game window when pressed.
  449. /// Must be a <see cref="ButtonControl"/>.
  450. /// </summary>
  451. /// <seealso cref="Cursor.lockState"/>
  452. /// <seealso cref="desiredCursorLockMode"/>
  453. public InputActionReference toggleCursorLockAction
  454. {
  455. get => m_ToggleCursorLockAction;
  456. set
  457. {
  458. UnsubscribeToggleCursorLockAction();
  459. m_ToggleCursorLockAction = value;
  460. SubscribeToggleCursorLockAction();
  461. }
  462. }
  463. [SerializeField]
  464. [Tooltip("The Input System Action used to toggle enable translation from keyboard inputs when pressed. Must be a Button Control.")]
  465. InputActionReference m_ToggleDevicePositionTargetAction;
  466. /// <summary>
  467. /// The Input System Action used to toggle enable translation from keyboard inputs when pressed.
  468. /// Must be a <see cref="ButtonControl"/>.
  469. /// </summary>
  470. /// <seealso cref="keyboardXTranslateAction"/>
  471. /// <seealso cref="keyboardYTranslateAction"/>
  472. /// <seealso cref="keyboardZTranslateAction"/>
  473. public InputActionReference toggleDevicePositionTargetAction
  474. {
  475. get => m_ToggleDevicePositionTargetAction;
  476. set
  477. {
  478. UnsubscribeToggleDevicePositionTargetAction();
  479. m_ToggleDevicePositionTargetAction = value;
  480. SubscribeToggleDevicePositionTargetAction();
  481. }
  482. }
  483. [SerializeField]
  484. [Tooltip("The Input System Action used to toggle enable manipulation of the Primary2DAxis of the controllers when pressed. Must be a Button Control.")]
  485. InputActionReference m_TogglePrimary2DAxisTargetAction;
  486. /// <summary>
  487. /// The Input System action used to toggle enable manipulation of the <see cref="Axis2DTargets.Primary2DAxis"/> of the controllers when pressed.
  488. /// Must be a <see cref="ButtonControl"/>.
  489. /// </summary>
  490. /// <seealso cref="toggleSecondary2DAxisTargetAction"/>
  491. /// <seealso cref="toggleDevicePositionTargetAction"/>
  492. /// <seealso cref="axis2DAction"/>
  493. public InputActionReference togglePrimary2DAxisTargetAction
  494. {
  495. get => m_TogglePrimary2DAxisTargetAction;
  496. set
  497. {
  498. UnsubscribeTogglePrimary2DAxisTargetAction();
  499. m_TogglePrimary2DAxisTargetAction = value;
  500. SubscribeTogglePrimary2DAxisTargetAction();
  501. }
  502. }
  503. [SerializeField]
  504. [Tooltip("The Input System Action used to toggle enable manipulation of the Secondary2DAxis of the controllers when pressed. Must be a Button Control.")]
  505. InputActionReference m_ToggleSecondary2DAxisTargetAction;
  506. /// <summary>
  507. /// The Input System action used to toggle enable manipulation of the <see cref="Axis2DTargets.Secondary2DAxis"/> of the controllers when pressed.
  508. /// Must be a <see cref="ButtonControl"/>.
  509. /// </summary>
  510. /// <seealso cref="togglePrimary2DAxisTargetAction"/>
  511. /// <seealso cref="toggleDevicePositionTargetAction"/>
  512. /// <seealso cref="axis2DAction"/>
  513. public InputActionReference toggleSecondary2DAxisTargetAction
  514. {
  515. get => m_ToggleSecondary2DAxisTargetAction;
  516. set
  517. {
  518. UnsubscribeToggleSecondary2DAxisTargetAction();
  519. m_ToggleSecondary2DAxisTargetAction = value;
  520. SubscribeToggleSecondary2DAxisTargetAction();
  521. }
  522. }
  523. [SerializeField]
  524. [Tooltip("The Input System Action used to control the value of one or more 2D Axis controls on the manipulated controller device(s). Must be a Value Vector2 Control.")]
  525. InputActionReference m_Axis2DAction;
  526. /// <summary>
  527. /// The Input System Action used to control the value of one or more 2D Axis controls on the manipulated controller device(s).
  528. /// Must be a <see cref="InputActionType.Value"/> <see cref="Vector2Control"/>.
  529. /// </summary>
  530. /// <remarks>
  531. /// <see cref="togglePrimary2DAxisTargetAction"/> and <see cref="toggleSecondary2DAxisTargetAction"/> toggle enables
  532. /// the ability to manipulate 2D Axis controls on the simulated controllers, and this <see cref="axis2DAction"/>
  533. /// actually controls the value of them while those controller devices are being manipulated.
  534. /// <br />
  535. /// Typically bound to WASD on a keyboard, and controls the primary and/or secondary 2D Axis controls on them.
  536. /// </remarks>
  537. public InputActionReference axis2DAction
  538. {
  539. get => m_Axis2DAction;
  540. set
  541. {
  542. UnsubscribeAxis2DAction();
  543. m_Axis2DAction = value;
  544. SubscribeAxis2DAction();
  545. }
  546. }
  547. [SerializeField]
  548. [Tooltip("The Input System Action used to control one or more 2D Axis controls on the opposite hand of the exclusively manipulated controller device. Must be a Value Vector2 Control.")]
  549. InputActionReference m_RestingHandAxis2DAction;
  550. /// <summary>
  551. /// The Input System Action used to control one or more 2D Axis controls on the opposite hand
  552. /// of the exclusively manipulated controller device.
  553. /// Must be a <see cref="InputActionType.Value"/> <see cref="Vector2Control"/>.
  554. /// </summary>
  555. /// <remarks>
  556. /// Typically bound to Q and E on a keyboard for the horizontal component, and controls the opposite hand's
  557. /// 2D Axis controls when manipulating one (and only one) controller. Can be used to quickly and simultaneously
  558. /// control the 2D Axis on the other hand's controller. In a typical setup of continuous movement bound on the left-hand
  559. /// controller stick, and turning bound on the right-hand controller stick, while exclusively manipulating the left-hand
  560. /// controller to move, this action can be used to trigger turning.
  561. /// </remarks>
  562. /// <seealso cref="axis2DAction"/>
  563. public InputActionReference restingHandAxis2DAction
  564. {
  565. get => m_RestingHandAxis2DAction;
  566. set
  567. {
  568. UnsubscribeRestingHandAxis2DAction();
  569. m_RestingHandAxis2DAction = value;
  570. SubscribeRestingHandAxis2DAction();
  571. }
  572. }
  573. [SerializeField]
  574. [Tooltip("The Input System Action used to control the Grip control of the manipulated controller device(s). Must be a Button Control.")]
  575. InputActionReference m_GripAction;
  576. /// <summary>
  577. /// The Input System Action used to control the Grip control of the manipulated controller device(s).
  578. /// Must be a <see cref="ButtonControl"/>.
  579. /// </summary>
  580. public InputActionReference gripAction
  581. {
  582. get => m_GripAction;
  583. set
  584. {
  585. UnsubscribeGripAction();
  586. m_GripAction = value;
  587. SubscribeGripAction();
  588. }
  589. }
  590. [SerializeField]
  591. [Tooltip("The Input System Action used to control the Trigger control of the manipulated controller device(s). Must be a Button Control.")]
  592. InputActionReference m_TriggerAction;
  593. /// <summary>
  594. /// The Input System Action used to control the Trigger control of the manipulated controller device(s).
  595. /// Must be a <see cref="ButtonControl"/>.
  596. /// </summary>
  597. public InputActionReference triggerAction
  598. {
  599. get => m_TriggerAction;
  600. set
  601. {
  602. UnsubscribeTriggerAction();
  603. m_TriggerAction = value;
  604. SubscribeTriggerAction();
  605. }
  606. }
  607. [SerializeField]
  608. [Tooltip("The Input System Action used to control the PrimaryButton control of the manipulated controller device(s). Must be a Button Control.")]
  609. InputActionReference m_PrimaryButtonAction;
  610. /// <summary>
  611. /// The Input System Action used to control the PrimaryButton control of the manipulated controller device(s).
  612. /// Must be a <see cref="ButtonControl"/>.
  613. /// </summary>
  614. public InputActionReference primaryButtonAction
  615. {
  616. get => m_PrimaryButtonAction;
  617. set
  618. {
  619. UnsubscribePrimaryButtonAction();
  620. m_PrimaryButtonAction = value;
  621. SubscribePrimaryButtonAction();
  622. }
  623. }
  624. [SerializeField]
  625. [Tooltip("The Input System Action used to control the SecondaryButton control of the manipulated controller device(s). Must be a Button Control.")]
  626. InputActionReference m_SecondaryButtonAction;
  627. /// <summary>
  628. /// The Input System Action used to control the SecondaryButton control of the manipulated controller device(s).
  629. /// Must be a <see cref="ButtonControl"/>.
  630. /// </summary>
  631. public InputActionReference secondaryButtonAction
  632. {
  633. get => m_SecondaryButtonAction;
  634. set
  635. {
  636. UnsubscribeSecondaryButtonAction();
  637. m_SecondaryButtonAction = value;
  638. SubscribeSecondaryButtonAction();
  639. }
  640. }
  641. [SerializeField]
  642. [Tooltip("The Input System Action used to control the Menu control of the manipulated controller device(s). Must be a Button Control.")]
  643. InputActionReference m_MenuAction;
  644. /// <summary>
  645. /// The Input System Action used to control the Menu control of the manipulated controller device(s).
  646. /// Must be a <see cref="ButtonControl"/>.
  647. /// </summary>
  648. public InputActionReference menuAction
  649. {
  650. get => m_MenuAction;
  651. set
  652. {
  653. UnsubscribeMenuAction();
  654. m_MenuAction = value;
  655. SubscribeMenuAction();
  656. }
  657. }
  658. [SerializeField]
  659. [Tooltip("The Input System Action used to control the Primary2DAxisClick control of the manipulated controller device(s). Must be a Button Control.")]
  660. InputActionReference m_Primary2DAxisClickAction;
  661. /// <summary>
  662. /// The Input System Action used to control the Primary2DAxisClick control of the manipulated controller device(s).
  663. /// Must be a <see cref="ButtonControl"/>.
  664. /// </summary>
  665. public InputActionReference primary2DAxisClickAction
  666. {
  667. get => m_Primary2DAxisClickAction;
  668. set
  669. {
  670. UnsubscribePrimary2DAxisClickAction();
  671. m_Primary2DAxisClickAction = value;
  672. SubscribePrimary2DAxisClickAction();
  673. }
  674. }
  675. [SerializeField]
  676. [Tooltip("The Input System Action used to control the Secondary2DAxisClick control of the manipulated controller device(s). Must be a Button Control.")]
  677. InputActionReference m_Secondary2DAxisClickAction;
  678. /// <summary>
  679. /// The Input System Action used to control the Secondary2DAxisClick control of the manipulated controller device(s).
  680. /// Must be a <see cref="ButtonControl"/>.
  681. /// </summary>
  682. public InputActionReference secondary2DAxisClickAction
  683. {
  684. get => m_Secondary2DAxisClickAction;
  685. set
  686. {
  687. UnsubscribeSecondary2DAxisClickAction();
  688. m_Secondary2DAxisClickAction = value;
  689. SubscribeSecondary2DAxisClickAction();
  690. }
  691. }
  692. [SerializeField]
  693. [Tooltip("The Input System Action used to control the Primary2DAxisTouch control of the manipulated controller device(s). Must be a Button Control.")]
  694. InputActionReference m_Primary2DAxisTouchAction;
  695. /// <summary>
  696. /// The Input System Action used to control the Primary2DAxisTouch control of the manipulated controller device(s).
  697. /// Must be a <see cref="ButtonControl"/>.
  698. /// </summary>
  699. public InputActionReference primary2DAxisTouchAction
  700. {
  701. get => m_Primary2DAxisTouchAction;
  702. set
  703. {
  704. UnsubscribePrimary2DAxisTouchAction();
  705. m_Primary2DAxisTouchAction = value;
  706. SubscribePrimary2DAxisTouchAction();
  707. }
  708. }
  709. [SerializeField]
  710. [Tooltip("The Input System Action used to control the Secondary2DAxisTouch control of the manipulated controller device(s). Must be a Button Control.")]
  711. InputActionReference m_Secondary2DAxisTouchAction;
  712. /// <summary>
  713. /// The Input System Action used to control the Secondary2DAxisTouch control of the manipulated controller device(s).
  714. /// Must be a <see cref="ButtonControl"/>.
  715. /// </summary>
  716. public InputActionReference secondary2DAxisTouchAction
  717. {
  718. get => m_Secondary2DAxisTouchAction;
  719. set
  720. {
  721. UnsubscribeSecondary2DAxisTouchAction();
  722. m_Secondary2DAxisTouchAction = value;
  723. SubscribeSecondary2DAxisTouchAction();
  724. }
  725. }
  726. [SerializeField]
  727. [Tooltip("The Input System Action used to control the PrimaryTouch control of the manipulated controller device(s). Must be a Button Control.")]
  728. InputActionReference m_PrimaryTouchAction;
  729. /// <summary>
  730. /// The Input System Action used to control the PrimaryTouch control of the manipulated controller device(s).
  731. /// Must be a <see cref="ButtonControl"/>.
  732. /// </summary>
  733. public InputActionReference primaryTouchAction
  734. {
  735. get => m_PrimaryTouchAction;
  736. set
  737. {
  738. UnsubscribePrimaryTouchAction();
  739. m_PrimaryTouchAction = value;
  740. SubscribePrimaryTouchAction();
  741. }
  742. }
  743. [SerializeField]
  744. [Tooltip("The Input System Action used to control the SecondaryTouch control of the manipulated controller device(s). Must be a Button Control.")]
  745. InputActionReference m_SecondaryTouchAction;
  746. /// <summary>
  747. /// The Input System Action used to control the SecondaryTouch control of the manipulated controller device(s).
  748. /// Must be a <see cref="ButtonControl"/>.
  749. /// </summary>
  750. public InputActionReference secondaryTouchAction
  751. {
  752. get => m_SecondaryTouchAction;
  753. set
  754. {
  755. UnsubscribeSecondaryTouchAction();
  756. m_SecondaryTouchAction = value;
  757. SubscribeSecondaryTouchAction();
  758. }
  759. }
  760. [SerializeField]
  761. [Tooltip("The Transform that contains the Camera. This is usually the \"Head\" of XR Origins. Automatically set to the first enabled camera tagged MainCamera if unset.")]
  762. Transform m_CameraTransform;
  763. /// <summary>
  764. /// The <see cref="Transform"/> that contains the <see cref="Camera"/>. This is usually the "Head" of XR Origins.
  765. /// Automatically set to <see cref="Camera.main"/> if unset.
  766. /// </summary>
  767. public Transform cameraTransform
  768. {
  769. get => m_CameraTransform;
  770. set => m_CameraTransform = value;
  771. }
  772. [SerializeField]
  773. [Tooltip("The coordinate space in which keyboard translation should operate.")]
  774. Space m_KeyboardTranslateSpace = Space.Local;
  775. /// <summary>
  776. /// The coordinate space in which keyboard translation should operate.
  777. /// </summary>
  778. /// <seealso cref="Space"/>
  779. /// <seealso cref="mouseTranslateSpace"/>
  780. /// <seealso cref="keyboardXTranslateAction"/>
  781. /// <seealso cref="keyboardYTranslateAction"/>
  782. /// <seealso cref="keyboardZTranslateAction"/>
  783. public Space keyboardTranslateSpace
  784. {
  785. get => m_KeyboardTranslateSpace;
  786. set => m_KeyboardTranslateSpace = value;
  787. }
  788. [SerializeField]
  789. [Tooltip("The coordinate space in which mouse translation should operate.")]
  790. Space m_MouseTranslateSpace = Space.Screen;
  791. /// <summary>
  792. /// The coordinate space in which mouse translation should operate.
  793. /// </summary>
  794. /// <seealso cref="Space"/>
  795. /// <seealso cref="keyboardTranslateSpace"/>
  796. public Space mouseTranslateSpace
  797. {
  798. get => m_MouseTranslateSpace;
  799. set => m_MouseTranslateSpace = value;
  800. }
  801. [SerializeField]
  802. [Tooltip("Speed of translation in the x-axis (left/right) when triggered by keyboard input.")]
  803. float m_KeyboardXTranslateSpeed = 0.2f;
  804. /// <summary>
  805. /// Speed of translation in the x-axis (left/right) when triggered by keyboard input.
  806. /// </summary>
  807. /// <seealso cref="keyboardXTranslateAction"/>
  808. /// <seealso cref="keyboardYTranslateSpeed"/>
  809. /// <seealso cref="keyboardZTranslateSpeed"/>
  810. public float keyboardXTranslateSpeed
  811. {
  812. get => m_KeyboardXTranslateSpeed;
  813. set => m_KeyboardXTranslateSpeed = value;
  814. }
  815. [SerializeField]
  816. [Tooltip("Speed of translation in the y-axis (up/down) when triggered by keyboard input.")]
  817. float m_KeyboardYTranslateSpeed = 0.2f;
  818. /// <summary>
  819. /// Speed of translation in the y-axis (up/down) when triggered by keyboard input.
  820. /// </summary>
  821. /// <seealso cref="keyboardYTranslateAction"/>
  822. /// <seealso cref="keyboardXTranslateSpeed"/>
  823. /// <seealso cref="keyboardZTranslateSpeed"/>
  824. public float keyboardYTranslateSpeed
  825. {
  826. get => m_KeyboardYTranslateSpeed;
  827. set => m_KeyboardYTranslateSpeed = value;
  828. }
  829. [SerializeField]
  830. [Tooltip("Speed of translation in the z-axis (forward/back) when triggered by keyboard input.")]
  831. float m_KeyboardZTranslateSpeed = 0.2f;
  832. /// <summary>
  833. /// Speed of translation in the z-axis (forward/back) when triggered by keyboard input.
  834. /// </summary>
  835. /// <seealso cref="keyboardZTranslateAction"/>
  836. /// <seealso cref="keyboardXTranslateSpeed"/>
  837. /// <seealso cref="keyboardYTranslateSpeed"/>
  838. public float keyboardZTranslateSpeed
  839. {
  840. get => m_KeyboardZTranslateSpeed;
  841. set => m_KeyboardZTranslateSpeed = value;
  842. }
  843. [SerializeField]
  844. [Tooltip("Sensitivity of translation in the x-axis (left/right) when triggered by mouse input.")]
  845. float m_MouseXTranslateSensitivity = 0.0004f;
  846. /// <summary>
  847. /// Sensitivity of translation in the x-axis (left/right) when triggered by mouse input.
  848. /// </summary>
  849. /// <seealso cref="mouseDeltaAction"/>
  850. /// <seealso cref="mouseYTranslateSensitivity"/>
  851. /// <seealso cref="mouseScrollTranslateSensitivity"/>
  852. public float mouseXTranslateSensitivity
  853. {
  854. get => m_MouseXTranslateSensitivity;
  855. set => m_MouseXTranslateSensitivity = value;
  856. }
  857. [SerializeField]
  858. [Tooltip("Sensitivity of translation in the y-axis (up/down) when triggered by mouse input.")]
  859. float m_MouseYTranslateSensitivity = 0.0004f;
  860. /// <summary>
  861. /// Sensitivity of translation in the y-axis (up/down) when triggered by mouse input.
  862. /// </summary>
  863. /// <seealso cref="mouseDeltaAction"/>
  864. /// <seealso cref="mouseXTranslateSensitivity"/>
  865. /// <seealso cref="mouseScrollTranslateSensitivity"/>
  866. public float mouseYTranslateSensitivity
  867. {
  868. get => m_MouseYTranslateSensitivity;
  869. set => m_MouseYTranslateSensitivity = value;
  870. }
  871. [SerializeField]
  872. [Tooltip("Sensitivity of translation in the z-axis (forward/back) when triggered by mouse scroll input.")]
  873. float m_MouseScrollTranslateSensitivity = 0.0002f;
  874. /// <summary>
  875. /// Sensitivity of translation in the z-axis (forward/back) when triggered by mouse scroll input.
  876. /// </summary>
  877. /// <seealso cref="mouseScrollAction"/>
  878. /// <seealso cref="mouseXTranslateSensitivity"/>
  879. /// <seealso cref="mouseYTranslateSensitivity"/>
  880. public float mouseScrollTranslateSensitivity
  881. {
  882. get => m_MouseScrollTranslateSensitivity;
  883. set => m_MouseScrollTranslateSensitivity = value;
  884. }
  885. [SerializeField]
  886. [Tooltip("Sensitivity of rotation along the x-axis (pitch) when triggered by mouse input.")]
  887. float m_MouseXRotateSensitivity = 0.1f;
  888. /// <summary>
  889. /// Sensitivity of rotation along the x-axis (pitch) when triggered by mouse input.
  890. /// </summary>
  891. /// <seealso cref="mouseDeltaAction"/>
  892. /// <seealso cref="mouseYRotateSensitivity"/>
  893. /// <seealso cref="mouseScrollRotateSensitivity"/>
  894. public float mouseXRotateSensitivity
  895. {
  896. get => m_MouseXRotateSensitivity;
  897. set => m_MouseXRotateSensitivity = value;
  898. }
  899. [SerializeField]
  900. [Tooltip("Sensitivity of rotation along the y-axis (yaw) when triggered by mouse input.")]
  901. float m_MouseYRotateSensitivity = 0.1f;
  902. /// <summary>
  903. /// Sensitivity of rotation along the y-axis (yaw) when triggered by mouse input.
  904. /// </summary>
  905. /// <seealso cref="mouseDeltaAction"/>
  906. /// <seealso cref="mouseXRotateSensitivity"/>
  907. /// <seealso cref="mouseScrollRotateSensitivity"/>
  908. public float mouseYRotateSensitivity
  909. {
  910. get => m_MouseYRotateSensitivity;
  911. set => m_MouseYRotateSensitivity = value;
  912. }
  913. [SerializeField]
  914. [Tooltip("Sensitivity of rotation along the z-axis (roll) when triggered by mouse scroll input.")]
  915. float m_MouseScrollRotateSensitivity = 0.05f;
  916. /// <summary>
  917. /// Sensitivity of rotation along the z-axis (roll) when triggered by mouse scroll input.
  918. /// </summary>
  919. /// <seealso cref="mouseScrollAction"/>
  920. /// <seealso cref="mouseXRotateSensitivity"/>
  921. /// <seealso cref="mouseYRotateSensitivity"/>
  922. public float mouseScrollRotateSensitivity
  923. {
  924. get => m_MouseScrollRotateSensitivity;
  925. set => m_MouseScrollRotateSensitivity = value;
  926. }
  927. [SerializeField]
  928. [Tooltip("A boolean value of whether to invert the y-axis of mouse input when rotating by mouse input." +
  929. "\nA false value (default) means typical FPS style where moving the mouse up/down pitches up/down." +
  930. "\nA true value means flight control style where moving the mouse up/down pitches down/up.")]
  931. bool m_MouseYRotateInvert;
  932. /// <summary>
  933. /// A boolean value of whether to invert the y-axis of mouse input when rotating by mouse input.
  934. /// A <see langword="false"/> value (default) means typical FPS style where moving the mouse up/down pitches up/down.
  935. /// A <see langword="true"/> value means flight control style where moving the mouse up/down pitches down/up.
  936. /// </summary>
  937. public bool mouseYRotateInvert
  938. {
  939. get => m_MouseYRotateInvert;
  940. set => m_MouseYRotateInvert = value;
  941. }
  942. [SerializeField]
  943. [Tooltip("The desired cursor lock mode to toggle to from None (either Locked or Confined).")]
  944. CursorLockMode m_DesiredCursorLockMode = CursorLockMode.Locked;
  945. /// <summary>
  946. /// The desired cursor lock mode to toggle to from <see cref="CursorLockMode.None"/>
  947. /// (either <see cref="CursorLockMode.Locked"/> or <see cref="CursorLockMode.Confined"/>).
  948. /// </summary>
  949. /// <seealso cref="toggleCursorLockAction"/>
  950. public CursorLockMode desiredCursorLockMode
  951. {
  952. get => m_DesiredCursorLockMode;
  953. set => m_DesiredCursorLockMode = value;
  954. }
  955. /// <summary>
  956. /// The transformation mode in which the mouse should operate.
  957. /// </summary>
  958. public TransformationMode mouseTransformationMode { get; set; } = TransformationMode.Translate;
  959. /// <summary>
  960. /// One or more 2D Axis controls that keyboard input should apply to (or none).
  961. /// </summary>
  962. /// <remarks>
  963. /// Used to control a combination of the position (<see cref="Axis2DTargets.Position"/>),
  964. /// primary 2D axis (<see cref="Axis2DTargets.Primary2DAxis"/>), or
  965. /// secondary 2D axis (<see cref="Axis2DTargets.Secondary2DAxis"/>) of manipulated device(s).
  966. /// </remarks>
  967. /// <seealso cref="keyboardXTranslateAction"/>
  968. /// <seealso cref="keyboardYTranslateAction"/>
  969. /// <seealso cref="keyboardZTranslateAction"/>
  970. /// <seealso cref="axis2DAction"/>
  971. /// <seealso cref="restingHandAxis2DAction"/>
  972. public Axis2DTargets axis2DTargets { get; set; } = Axis2DTargets.Primary2DAxis;
  973. float m_KeyboardXTranslateInput;
  974. float m_KeyboardYTranslateInput;
  975. float m_KeyboardZTranslateInput;
  976. bool m_ManipulateLeftInput;
  977. bool m_ManipulateRightInput;
  978. bool m_ManipulateHeadInput;
  979. Vector2 m_MouseDeltaInput;
  980. Vector2 m_MouseScrollInput;
  981. bool m_RotateModeOverrideInput;
  982. bool m_NegateModeInput;
  983. bool m_XConstraintInput;
  984. bool m_YConstraintInput;
  985. bool m_ZConstraintInput;
  986. bool m_ResetInput;
  987. Vector2 m_Axis2DInput;
  988. Vector2 m_RestingHandAxis2DInput;
  989. bool m_GripInput;
  990. bool m_TriggerInput;
  991. bool m_PrimaryButtonInput;
  992. bool m_SecondaryButtonInput;
  993. bool m_MenuInput;
  994. bool m_Primary2DAxisClickInput;
  995. bool m_Secondary2DAxisClickInput;
  996. bool m_Primary2DAxisTouchInput;
  997. bool m_Secondary2DAxisTouchInput;
  998. bool m_PrimaryTouchInput;
  999. bool m_SecondaryTouchInput;
  1000. bool m_ManipulatedRestingHandAxis2D;
  1001. Vector3 m_LeftControllerEuler;
  1002. Vector3 m_RightControllerEuler;
  1003. Vector3 m_CenterEyeEuler;
  1004. XRSimulatedHMDState m_HMDState;
  1005. XRSimulatedControllerState m_LeftControllerState;
  1006. XRSimulatedControllerState m_RightControllerState;
  1007. XRSimulatedHMD m_HMDDevice;
  1008. XRSimulatedController m_LeftControllerDevice;
  1009. XRSimulatedController m_RightControllerDevice;
  1010. /// <summary>
  1011. /// See <see cref="MonoBehaviour"/>.
  1012. /// </summary>
  1013. protected virtual void Awake()
  1014. {
  1015. #if !UNITY_EDITOR
  1016. this.gameObject.SetActive(false);
  1017. return;
  1018. #endif
  1019. m_HMDState.Reset();
  1020. m_LeftControllerState.Reset();
  1021. m_RightControllerState.Reset();
  1022. }
  1023. /// <summary>
  1024. /// See <see cref="MonoBehaviour"/>.
  1025. /// </summary>
  1026. protected virtual void OnEnable()
  1027. {
  1028. Debug.Log("Enable");
  1029. // Find the Camera if necessary
  1030. if (m_CameraTransform == null)
  1031. {
  1032. var mainCamera = Camera.main;
  1033. if (mainCamera != null)
  1034. m_CameraTransform = mainCamera.transform;
  1035. }
  1036. AddDevices();
  1037. SubscribeKeyboardXTranslateAction();
  1038. SubscribeKeyboardYTranslateAction();
  1039. SubscribeKeyboardZTranslateAction();
  1040. SubscribeManipulateLeftAction();
  1041. SubscribeManipulateRightAction();
  1042. SubscribeToggleManipulateLeftAction();
  1043. SubscribeToggleManipulateRightAction();
  1044. SubscribeManipulateHeadAction();
  1045. SubscribeMouseDeltaAction();
  1046. SubscribeMouseScrollAction();
  1047. SubscribeRotateModeOverrideAction();
  1048. SubscribeToggleMouseTransformationModeAction();
  1049. SubscribeNegateModeAction();
  1050. SubscribeXConstraintAction();
  1051. SubscribeYConstraintAction();
  1052. SubscribeZConstraintAction();
  1053. SubscribeResetAction();
  1054. SubscribeToggleCursorLockAction();
  1055. SubscribeToggleDevicePositionTargetAction();
  1056. SubscribeTogglePrimary2DAxisTargetAction();
  1057. SubscribeToggleSecondary2DAxisTargetAction();
  1058. SubscribeAxis2DAction();
  1059. SubscribeRestingHandAxis2DAction();
  1060. SubscribeGripAction();
  1061. SubscribeTriggerAction();
  1062. SubscribePrimaryButtonAction();
  1063. SubscribeSecondaryButtonAction();
  1064. SubscribeMenuAction();
  1065. SubscribePrimary2DAxisClickAction();
  1066. SubscribeSecondary2DAxisClickAction();
  1067. SubscribePrimary2DAxisTouchAction();
  1068. SubscribeSecondary2DAxisTouchAction();
  1069. SubscribePrimaryTouchAction();
  1070. SubscribeSecondaryTouchAction();
  1071. }
  1072. /// <summary>
  1073. /// See <see cref="MonoBehaviour"/>.
  1074. /// </summary>
  1075. protected virtual void OnDisable()
  1076. {
  1077. RemoveDevices();
  1078. UnsubscribeKeyboardXTranslateAction();
  1079. UnsubscribeKeyboardYTranslateAction();
  1080. UnsubscribeKeyboardZTranslateAction();
  1081. UnsubscribeManipulateLeftAction();
  1082. UnsubscribeManipulateRightAction();
  1083. UnsubscribeToggleManipulateLeftAction();
  1084. UnsubscribeToggleManipulateRightAction();
  1085. UnsubscribeManipulateHeadAction();
  1086. UnsubscribeMouseDeltaAction();
  1087. UnsubscribeMouseScrollAction();
  1088. UnsubscribeRotateModeOverrideAction();
  1089. UnsubscribeToggleMouseTransformationModeAction();
  1090. UnsubscribeNegateModeAction();
  1091. UnsubscribeXConstraintAction();
  1092. UnsubscribeYConstraintAction();
  1093. UnsubscribeZConstraintAction();
  1094. UnsubscribeResetAction();
  1095. UnsubscribeToggleCursorLockAction();
  1096. UnsubscribeToggleDevicePositionTargetAction();
  1097. UnsubscribeTogglePrimary2DAxisTargetAction();
  1098. UnsubscribeToggleSecondary2DAxisTargetAction();
  1099. UnsubscribeAxis2DAction();
  1100. UnsubscribeRestingHandAxis2DAction();
  1101. UnsubscribeGripAction();
  1102. UnsubscribeTriggerAction();
  1103. UnsubscribePrimaryButtonAction();
  1104. UnsubscribeSecondaryButtonAction();
  1105. UnsubscribeMenuAction();
  1106. UnsubscribePrimary2DAxisClickAction();
  1107. UnsubscribeSecondary2DAxisClickAction();
  1108. UnsubscribePrimary2DAxisTouchAction();
  1109. UnsubscribeSecondary2DAxisTouchAction();
  1110. UnsubscribePrimaryTouchAction();
  1111. UnsubscribeSecondaryTouchAction();
  1112. }
  1113. /// <summary>
  1114. /// See <see cref="MonoBehaviour"/>.
  1115. /// </summary>
  1116. protected virtual void Update()
  1117. {
  1118. ProcessPoseInput();
  1119. ProcessControlInput();
  1120. if (m_HMDDevice != null)
  1121. {
  1122. InputState.Change(m_HMDDevice, m_HMDState);
  1123. }
  1124. if (m_LeftControllerDevice != null)
  1125. {
  1126. InputState.Change(m_LeftControllerDevice, m_LeftControllerState);
  1127. }
  1128. if (m_RightControllerDevice != null)
  1129. {
  1130. InputState.Change(m_RightControllerDevice, m_RightControllerState);
  1131. }
  1132. }
  1133. /// <summary>
  1134. /// Process input from the user and update the state of manipulated device(s)
  1135. /// related to position and rotation.
  1136. /// </summary>
  1137. protected virtual void ProcessPoseInput() {
  1138. if (m_CameraTransform == null)
  1139. return;
  1140. // Set tracked states
  1141. m_LeftControllerState.isTracked = true;
  1142. m_RightControllerState.isTracked = true;
  1143. m_HMDState.isTracked = true;
  1144. m_LeftControllerState.trackingState = (int)(InputTrackingState.Position | InputTrackingState.Rotation);
  1145. m_RightControllerState.trackingState = (int)(InputTrackingState.Position | InputTrackingState.Rotation);
  1146. m_HMDState.trackingState = (int)(InputTrackingState.Position | InputTrackingState.Rotation);
  1147. //if (!m_ManipulateLeftInput && !m_ManipulateRightInput && !m_ManipulateHeadInput)
  1148. // return;
  1149. var cameraParent = m_CameraTransform.parent;
  1150. var cameraParentRotation = cameraParent != null ? cameraParent.rotation : Quaternion.identity;
  1151. var inverseCameraParentRotation = Quaternion.Inverse(cameraParentRotation);
  1152. // if ((axis2DTargets & Axis2DTargets.Position) != 0)
  1153. //{
  1154. var deltaPosition = Vector3.zero;
  1155. if (m_ManipulateLeftInput) {
  1156. // Determine frame of reference
  1157. GetAxes(m_KeyboardTranslateSpace, m_CameraTransform, out var right, out var up, out var forward);
  1158. // Keyboard translation
  1159. deltaPosition =
  1160. right * (m_KeyboardXTranslateInput * m_KeyboardXTranslateSpeed * Time.deltaTime) +
  1161. up * (m_KeyboardYTranslateInput * m_KeyboardYTranslateSpeed * Time.deltaTime) +
  1162. forward * (m_KeyboardZTranslateInput * m_KeyboardZTranslateSpeed * Time.deltaTime);
  1163. }
  1164. // if (m_ManipulateLeftInput)
  1165. {
  1166. //var deltaRotation = GetDeltaRotation(m_KeyboardTranslateSpace, m_LeftControllerState, inverseCameraParentRotation);
  1167. //m_LeftControllerState.devicePosition += deltaRotation * deltaPosition;
  1168. m_LeftControllerState.devicePosition = m_CameraTransform.localPosition + m_CameraTransform.localRotation * new Vector3(-0.25f, -0.25f, 0.3f);
  1169. }
  1170. // if (m_ManipulateRightInput)
  1171. {
  1172. //var deltaRotation = GetDeltaRotation(m_KeyboardTranslateSpace, m_RightControllerState, inverseCameraParentRotation);
  1173. //m_RightControllerState.devicePosition += deltaRotation * deltaPosition;
  1174. m_RightControllerState.devicePosition = m_CameraTransform.localPosition + m_CameraTransform.localRotation * new Vector3(0.25f, -0.25f, 0.3f);
  1175. }
  1176. //if (m_ManipulateHeadInput)
  1177. {
  1178. var deltaRotation = GetDeltaRotation(m_KeyboardTranslateSpace, m_HMDState, inverseCameraParentRotation);
  1179. m_HMDState.centerEyePosition += deltaRotation * deltaPosition;
  1180. m_HMDState.devicePosition = m_HMDState.centerEyePosition;
  1181. }
  1182. //}
  1183. //if ((mouseTransformationMode == TransformationMode.Translate && !m_RotateModeOverrideInput && !m_NegateModeInput) ||
  1184. // (mouseTransformationMode == TransformationMode.Rotate || m_RotateModeOverrideInput) && m_NegateModeInput)
  1185. //{
  1186. // // Determine frame of reference
  1187. // GetAxes(m_MouseTranslateSpace, m_CameraTransform, out var right, out var up, out var forward);
  1188. // // Mouse translation
  1189. // var scaledMouseDeltaInput =
  1190. // new Vector3(m_MouseDeltaInput.x * m_MouseXTranslateSensitivity,
  1191. // m_MouseDeltaInput.y * m_MouseYTranslateSensitivity,
  1192. // m_MouseScrollInput.y * m_MouseScrollTranslateSensitivity);
  1193. // Vector3 deltaPosition;
  1194. // if (m_XConstraintInput && !m_YConstraintInput && m_ZConstraintInput) // XZ
  1195. // {
  1196. // deltaPosition =
  1197. // right * scaledMouseDeltaInput.x +
  1198. // forward * scaledMouseDeltaInput.y;
  1199. // }
  1200. // else if (!m_XConstraintInput && m_YConstraintInput && m_ZConstraintInput) // YZ
  1201. // {
  1202. // deltaPosition =
  1203. // up * scaledMouseDeltaInput.y +
  1204. // forward * scaledMouseDeltaInput.x;
  1205. // }
  1206. // else if (m_XConstraintInput && !m_YConstraintInput && !m_ZConstraintInput) // X
  1207. // {
  1208. // deltaPosition =
  1209. // right * (scaledMouseDeltaInput.x + scaledMouseDeltaInput.y);
  1210. // }
  1211. // else if (!m_XConstraintInput && m_YConstraintInput && !m_ZConstraintInput) // Y
  1212. // {
  1213. // deltaPosition =
  1214. // up * (scaledMouseDeltaInput.x + scaledMouseDeltaInput.y);
  1215. // }
  1216. // else if (!m_XConstraintInput && !m_YConstraintInput && m_ZConstraintInput) // Z
  1217. // {
  1218. // deltaPosition =
  1219. // forward * (scaledMouseDeltaInput.x + scaledMouseDeltaInput.y);
  1220. // }
  1221. // else
  1222. // {
  1223. // deltaPosition =
  1224. // right * scaledMouseDeltaInput.x +
  1225. // up * scaledMouseDeltaInput.y;
  1226. // }
  1227. // // Scroll contribution
  1228. // deltaPosition +=
  1229. // forward * scaledMouseDeltaInput.z;
  1230. // if (m_ManipulateLeftInput)
  1231. // {
  1232. // var deltaRotation = GetDeltaRotation(m_MouseTranslateSpace, m_LeftControllerState, inverseCameraParentRotation);
  1233. // m_LeftControllerState.devicePosition += deltaRotation * deltaPosition;
  1234. // }
  1235. // if (m_ManipulateRightInput)
  1236. // {
  1237. // var deltaRotation = GetDeltaRotation(m_MouseTranslateSpace, m_RightControllerState, inverseCameraParentRotation);
  1238. // m_RightControllerState.devicePosition += deltaRotation * deltaPosition;
  1239. // }
  1240. // if (m_ManipulateHeadInput)
  1241. // {
  1242. // var deltaRotation = GetDeltaRotation(m_MouseTranslateSpace, m_HMDState, inverseCameraParentRotation);
  1243. // m_HMDState.centerEyePosition += deltaRotation * deltaPosition;
  1244. // m_HMDState.devicePosition = m_HMDState.centerEyePosition;
  1245. // }
  1246. // // Reset
  1247. // if (m_ResetInput)
  1248. // {
  1249. // var resetScale = GetResetScale();
  1250. // if (m_ManipulateLeftInput)
  1251. // {
  1252. // var devicePosition = Vector3.Scale(m_LeftControllerState.devicePosition, resetScale);
  1253. // // The active control for the InputAction will be null while the Action is in waiting at (0, 0, 0)
  1254. // // so use a small value to reset the position to near origin.
  1255. // if (devicePosition.magnitude <= 0f)
  1256. // devicePosition = new Vector3(Mathf.Epsilon, Mathf.Epsilon, Mathf.Epsilon);
  1257. // m_LeftControllerState.devicePosition = devicePosition;
  1258. // }
  1259. // if (m_ManipulateRightInput)
  1260. // {
  1261. // var devicePosition = Vector3.Scale(m_RightControllerState.devicePosition, resetScale);
  1262. // // The active control for the InputAction will be null while the Action is in waiting at (0, 0, 0)
  1263. // // so use a small value to reset the position to near origin.
  1264. // if (devicePosition.magnitude <= 0f)
  1265. // devicePosition = new Vector3(Mathf.Epsilon, Mathf.Epsilon, Mathf.Epsilon);
  1266. // m_RightControllerState.devicePosition = devicePosition;
  1267. // }
  1268. // if (m_ManipulateHeadInput)
  1269. // {
  1270. // // TODO: Tracked Pose Driver (New Input System) has a bug where it only subscribes to
  1271. // // performed and not canceled, so the Transform will not be updated until the magnitude
  1272. // // is considered actuated to trigger a performed event. As a workaround, set to
  1273. // // a small value (enough to be considered actuated) instead of Vector3.zero.
  1274. // var centerEyePosition = Vector3.Scale(m_HMDState.centerEyePosition, resetScale);
  1275. // if (centerEyePosition.magnitude <= 0f)
  1276. // centerEyePosition = new Vector3(Mathf.Epsilon, Mathf.Epsilon, Mathf.Epsilon);
  1277. // m_HMDState.centerEyePosition = centerEyePosition;
  1278. // m_HMDState.devicePosition = m_HMDState.centerEyePosition;
  1279. // }
  1280. // }
  1281. //}
  1282. //else
  1283. Vector3 anglesDelta = Vector3.zero; var scaledMouseDeltaInput = Vector3.zero;
  1284. if (m_ManipulateLeftInput) {
  1285. // Mouse rotation
  1286. scaledMouseDeltaInput =
  1287. new Vector3(m_MouseDeltaInput.x * m_MouseXRotateSensitivity,
  1288. m_MouseDeltaInput.y * m_MouseYRotateSensitivity * (m_MouseYRotateInvert ? 1f : -1f),
  1289. m_MouseScrollInput.y * m_MouseScrollRotateSensitivity);
  1290. anglesDelta = new Vector3(scaledMouseDeltaInput.y, scaledMouseDeltaInput.x, 0f);
  1291. }
  1292. // Scroll contribution
  1293. anglesDelta += new Vector3(0f, 0f, scaledMouseDeltaInput.z);
  1294. // if (m_ManipulateLeftInput)
  1295. {
  1296. m_LeftControllerEuler += anglesDelta;
  1297. m_LeftControllerState.deviceRotation = Quaternion.Euler(m_LeftControllerEuler);
  1298. }
  1299. // if (m_ManipulateRightInput)
  1300. {
  1301. m_RightControllerEuler += anglesDelta;
  1302. m_RightControllerState.deviceRotation = Quaternion.Euler(m_RightControllerEuler);
  1303. }
  1304. // if (m_ManipulateHeadInput)
  1305. {
  1306. m_CenterEyeEuler += anglesDelta;
  1307. m_HMDState.centerEyeRotation = Quaternion.Euler(m_CenterEyeEuler);
  1308. }
  1309. // Reset
  1310. if (m_ResetInput)
  1311. {
  1312. var resetScale = GetResetScale();
  1313. if (m_ManipulateLeftInput)
  1314. {
  1315. m_LeftControllerEuler = Vector3.Scale(m_LeftControllerEuler, resetScale);
  1316. m_LeftControllerState.deviceRotation = Quaternion.Euler(m_LeftControllerEuler);
  1317. }
  1318. if (m_ManipulateRightInput)
  1319. {
  1320. m_RightControllerEuler = Vector3.Scale(m_RightControllerEuler, resetScale);
  1321. m_RightControllerState.deviceRotation = Quaternion.Euler(m_RightControllerEuler);
  1322. }
  1323. if (m_ManipulateHeadInput)
  1324. {
  1325. m_CenterEyeEuler = Vector3.Scale(m_CenterEyeEuler, resetScale);
  1326. m_HMDState.centerEyeRotation = Quaternion.Euler(m_CenterEyeEuler);
  1327. }
  1328. }
  1329. }
  1330. //}
  1331. /// <summary>
  1332. /// Process input from the user and update the state of manipulated controller device(s)
  1333. /// related to input controls.
  1334. /// </summary>
  1335. protected virtual void ProcessControlInput()
  1336. {
  1337. ProcessAxis2DControlInput();
  1338. // if (m_ManipulateLeftInput)
  1339. {
  1340. ProcessButtonControlInput(ref m_LeftControllerState);
  1341. }
  1342. // if (m_ManipulateRightInput)
  1343. {
  1344. ProcessButtonControlInput(ref m_RightControllerState);
  1345. }
  1346. }
  1347. /// <summary>
  1348. /// Process input from the user and update the state of manipulated controller device(s)
  1349. /// related to 2D Axis input controls.
  1350. /// </summary>
  1351. protected virtual void ProcessAxis2DControlInput()
  1352. {
  1353. //if (!m_ManipulateLeftInput && !m_ManipulateRightInput)
  1354. // return;
  1355. //if ((axis2DTargets & Axis2DTargets.Primary2DAxis) != 0)
  1356. {
  1357. // if (m_ManipulateLeftInput)
  1358. m_LeftControllerState.primary2DAxis = m_Axis2DInput;
  1359. // if (m_ManipulateRightInput)
  1360. m_RightControllerState.primary2DAxis = m_Axis2DInput;
  1361. // if (m_ManipulateLeftInput ^ m_ManipulateRightInput)
  1362. //{
  1363. // if (m_RestingHandAxis2DInput != Vector2.zero || m_ManipulatedRestingHandAxis2D)
  1364. // {
  1365. // if (m_ManipulateLeftInput)
  1366. // m_RightControllerState.primary2DAxis = m_RestingHandAxis2DInput;
  1367. // if (m_ManipulateRightInput)
  1368. // m_LeftControllerState.primary2DAxis = m_RestingHandAxis2DInput;
  1369. // m_ManipulatedRestingHandAxis2D = m_RestingHandAxis2DInput != Vector2.zero;
  1370. // }
  1371. // else
  1372. // {
  1373. // m_ManipulatedRestingHandAxis2D = false;
  1374. // }
  1375. //}
  1376. }
  1377. // if ((axis2DTargets & Axis2DTargets.Secondary2DAxis) != 0)
  1378. {
  1379. // if (m_ManipulateLeftInput)
  1380. m_LeftControllerState.secondary2DAxis = m_Axis2DInput;
  1381. // if (m_ManipulateRightInput)
  1382. m_RightControllerState.secondary2DAxis = m_Axis2DInput;
  1383. // if (m_ManipulateLeftInput ^ m_ManipulateRightInput)
  1384. //{
  1385. // if (m_RestingHandAxis2DInput != Vector2.zero || m_ManipulatedRestingHandAxis2D)
  1386. // {
  1387. // if (m_ManipulateLeftInput)
  1388. // m_RightControllerState.secondary2DAxis = m_RestingHandAxis2DInput;
  1389. // if (m_ManipulateRightInput)
  1390. // m_LeftControllerState.secondary2DAxis = m_RestingHandAxis2DInput;
  1391. // m_ManipulatedRestingHandAxis2D = m_RestingHandAxis2DInput != Vector2.zero;
  1392. // }
  1393. // else
  1394. // {
  1395. // m_ManipulatedRestingHandAxis2D = false;
  1396. // }
  1397. //}
  1398. }
  1399. }
  1400. /// <summary>
  1401. /// Process input from the user and update the state of manipulated controller device(s)
  1402. /// related to button input controls.
  1403. /// </summary>
  1404. /// <param name="controllerState">The controller state that will be processed.</param>
  1405. protected virtual void ProcessButtonControlInput(ref XRSimulatedControllerState controllerState)
  1406. {
  1407. controllerState.grip = m_GripInput ? 1f : 0f;
  1408. controllerState.WithButton(ControllerButton.GripButton, m_GripInput);
  1409. controllerState.trigger = m_TriggerInput ? 1f : 0f;
  1410. controllerState.WithButton(ControllerButton.TriggerButton, m_TriggerInput);
  1411. controllerState.WithButton(ControllerButton.PrimaryButton, m_PrimaryButtonInput);
  1412. controllerState.WithButton(ControllerButton.SecondaryButton, m_SecondaryButtonInput);
  1413. controllerState.WithButton(ControllerButton.MenuButton, m_MenuInput);
  1414. controllerState.WithButton(ControllerButton.Primary2DAxisClick, m_Primary2DAxisClickInput);
  1415. controllerState.WithButton(ControllerButton.Secondary2DAxisClick, m_Secondary2DAxisClickInput);
  1416. controllerState.WithButton(ControllerButton.Primary2DAxisTouch, m_Primary2DAxisTouchInput);
  1417. controllerState.WithButton(ControllerButton.Secondary2DAxisTouch, m_Secondary2DAxisTouchInput);
  1418. controllerState.WithButton(ControllerButton.PrimaryTouch, m_PrimaryTouchInput);
  1419. controllerState.WithButton(ControllerButton.SecondaryTouch, m_SecondaryTouchInput);
  1420. }
  1421. /// <summary>
  1422. /// Gets a <see cref="Vector3"/> that can be multiplied component-wise with another <see cref="Vector3"/>
  1423. /// to reset components of the <see cref="Vector3"/>, based on axis constraint inputs.
  1424. /// </summary>
  1425. /// <returns></returns>
  1426. /// <seealso cref="resetAction"/>
  1427. /// <seealso cref="xConstraintAction"/>
  1428. /// <seealso cref="yConstraintAction"/>
  1429. /// <seealso cref="zConstraintAction"/>
  1430. protected Vector3 GetResetScale()
  1431. {
  1432. return m_XConstraintInput || m_YConstraintInput || m_ZConstraintInput
  1433. ? new Vector3(m_XConstraintInput ? 0f : 1f, m_YConstraintInput ? 0f : 1f, m_ZConstraintInput ? 0f : 1f)
  1434. : Vector3.zero;
  1435. }
  1436. static void GetAxes(Space translateSpace, Transform cameraTransform, out Vector3 right, out Vector3 up, out Vector3 forward)
  1437. {
  1438. if (cameraTransform == null)
  1439. throw new ArgumentNullException(nameof(cameraTransform));
  1440. switch (translateSpace)
  1441. {
  1442. case Space.Local:
  1443. // Makes the assumption that the Camera and the Controllers are siblings
  1444. // (meaning they share a parent GameObject).
  1445. var cameraParent = cameraTransform.parent;
  1446. if (cameraParent != null)
  1447. {
  1448. right = cameraParent.TransformDirection(Vector3.right);
  1449. up = cameraParent.TransformDirection(Vector3.up);
  1450. forward = cameraParent.TransformDirection(Vector3.forward);
  1451. }
  1452. else
  1453. {
  1454. right = Vector3.right;
  1455. up = Vector3.up;
  1456. forward = Vector3.forward;
  1457. }
  1458. break;
  1459. case Space.Parent:
  1460. right = Vector3.right;
  1461. up = Vector3.up;
  1462. forward = Vector3.forward;
  1463. break;
  1464. case Space.Screen:
  1465. right = cameraTransform.TransformDirection(Vector3.right);
  1466. up = cameraTransform.TransformDirection(Vector3.up);
  1467. forward = cameraTransform.TransformDirection(Vector3.forward);
  1468. break;
  1469. default:
  1470. right = Vector3.right;
  1471. up = Vector3.up;
  1472. forward = Vector3.forward;
  1473. Assert.IsTrue(false, $"Unhandled {nameof(translateSpace)}={translateSpace}.");
  1474. return;
  1475. }
  1476. }
  1477. static Quaternion GetDeltaRotation(Space translateSpace, in XRSimulatedControllerState state, in Quaternion inverseCameraParentRotation)
  1478. {
  1479. switch (translateSpace)
  1480. {
  1481. case Space.Local:
  1482. return state.deviceRotation * inverseCameraParentRotation;
  1483. case Space.Parent:
  1484. return Quaternion.identity;
  1485. case Space.Screen:
  1486. return inverseCameraParentRotation;
  1487. default:
  1488. Assert.IsTrue(false, $"Unhandled {nameof(translateSpace)}={translateSpace}.");
  1489. return Quaternion.identity;
  1490. }
  1491. }
  1492. static Quaternion GetDeltaRotation(Space translateSpace, in XRSimulatedHMDState state, in Quaternion inverseCameraParentRotation)
  1493. {
  1494. switch (translateSpace)
  1495. {
  1496. case Space.Local:
  1497. return state.centerEyeRotation * inverseCameraParentRotation;
  1498. case Space.Parent:
  1499. return Quaternion.identity;
  1500. case Space.Screen:
  1501. return inverseCameraParentRotation;
  1502. default:
  1503. Assert.IsTrue(false, $"Unhandled {nameof(translateSpace)}={translateSpace}.");
  1504. return Quaternion.identity;
  1505. }
  1506. }
  1507. static void Subscribe(InputActionReference reference, Action<InputAction.CallbackContext> performed = null, Action<InputAction.CallbackContext> canceled = null)
  1508. {
  1509. var action = GetInputAction(reference);
  1510. if (action != null)
  1511. {
  1512. if (performed != null)
  1513. action.performed += performed;
  1514. if (canceled != null)
  1515. action.canceled += canceled;
  1516. }
  1517. }
  1518. static void Unsubscribe(InputActionReference reference, Action<InputAction.CallbackContext> performed = null, Action<InputAction.CallbackContext> canceled = null)
  1519. {
  1520. var action = GetInputAction(reference);
  1521. if (action != null)
  1522. {
  1523. if (performed != null)
  1524. action.performed -= performed;
  1525. if (canceled != null)
  1526. action.canceled -= canceled;
  1527. }
  1528. }
  1529. static TransformationMode Negate(TransformationMode mode)
  1530. {
  1531. switch (mode)
  1532. {
  1533. case TransformationMode.Rotate:
  1534. return TransformationMode.Translate;
  1535. case TransformationMode.Translate:
  1536. return TransformationMode.Rotate;
  1537. default:
  1538. Assert.IsTrue(false, $"Unhandled {nameof(mode)}={mode}.");
  1539. return TransformationMode.Rotate;
  1540. }
  1541. }
  1542. CursorLockMode Negate(CursorLockMode mode)
  1543. {
  1544. switch (mode)
  1545. {
  1546. case CursorLockMode.None:
  1547. return m_DesiredCursorLockMode;
  1548. case CursorLockMode.Locked:
  1549. case CursorLockMode.Confined:
  1550. return CursorLockMode.None;
  1551. default:
  1552. Assert.IsTrue(false, $"Unhandled {nameof(mode)}={mode}.");
  1553. return CursorLockMode.None;
  1554. }
  1555. }
  1556. void SubscribeKeyboardXTranslateAction() => Subscribe(m_KeyboardXTranslateAction, OnKeyboardXTranslatePerformed, OnKeyboardXTranslateCanceled);
  1557. void UnsubscribeKeyboardXTranslateAction() => Unsubscribe(m_KeyboardXTranslateAction, OnKeyboardXTranslatePerformed, OnKeyboardXTranslateCanceled);
  1558. void SubscribeKeyboardYTranslateAction() => Subscribe(m_KeyboardYTranslateAction, OnKeyboardYTranslatePerformed, OnKeyboardYTranslateCanceled);
  1559. void UnsubscribeKeyboardYTranslateAction() => Unsubscribe(m_KeyboardYTranslateAction, OnKeyboardYTranslatePerformed, OnKeyboardYTranslateCanceled);
  1560. void SubscribeKeyboardZTranslateAction() => Subscribe(m_KeyboardZTranslateAction, OnKeyboardZTranslatePerformed, OnKeyboardZTranslateCanceled);
  1561. void UnsubscribeKeyboardZTranslateAction() => Unsubscribe(m_KeyboardZTranslateAction, OnKeyboardZTranslatePerformed, OnKeyboardZTranslateCanceled);
  1562. void SubscribeManipulateLeftAction() => Subscribe(m_ManipulateLeftAction, OnManipulateLeftPerformed, OnManipulateLeftCanceled);
  1563. void UnsubscribeManipulateLeftAction() => Unsubscribe(m_ManipulateLeftAction, OnManipulateLeftPerformed, OnManipulateLeftCanceled);
  1564. void SubscribeManipulateRightAction() => Subscribe(m_ManipulateRightAction, OnManipulateRightPerformed, OnManipulateRightCanceled);
  1565. void UnsubscribeManipulateRightAction() => Unsubscribe(m_ManipulateRightAction, OnManipulateRightPerformed, OnManipulateRightCanceled);
  1566. void SubscribeToggleManipulateLeftAction() => Subscribe(m_ToggleManipulateLeftAction, OnToggleManipulateLeftPerformed);
  1567. void UnsubscribeToggleManipulateLeftAction() => Unsubscribe(m_ToggleManipulateLeftAction, OnToggleManipulateLeftPerformed);
  1568. void SubscribeToggleManipulateRightAction() => Subscribe(m_ToggleManipulateRightAction, OnToggleManipulateRightPerformed);
  1569. void UnsubscribeToggleManipulateRightAction() => Unsubscribe(m_ToggleManipulateRightAction, OnToggleManipulateRightPerformed);
  1570. void SubscribeManipulateHeadAction() => Subscribe(m_ManipulateHeadAction, OnManipulateHeadPerformed, OnManipulateHeadCanceled);
  1571. void UnsubscribeManipulateHeadAction() => Unsubscribe(m_ManipulateHeadAction, OnManipulateHeadPerformed, OnManipulateHeadCanceled);
  1572. void SubscribeMouseDeltaAction() => Subscribe(m_MouseDeltaAction, OnMouseDeltaPerformed, OnMouseDeltaCanceled);
  1573. void UnsubscribeMouseDeltaAction() => Unsubscribe(m_MouseDeltaAction, OnMouseDeltaPerformed, OnMouseDeltaCanceled);
  1574. void SubscribeMouseScrollAction() => Subscribe(m_MouseScrollAction, OnMouseScrollPerformed, OnMouseScrollCanceled);
  1575. void UnsubscribeMouseScrollAction() => Unsubscribe(m_MouseScrollAction, OnMouseScrollPerformed, OnMouseScrollCanceled);
  1576. void SubscribeRotateModeOverrideAction() => Subscribe(m_RotateModeOverrideAction, OnRotateModeOverridePerformed, OnRotateModeOverrideCanceled);
  1577. void UnsubscribeRotateModeOverrideAction() => Unsubscribe(m_RotateModeOverrideAction, OnRotateModeOverridePerformed, OnRotateModeOverrideCanceled);
  1578. void SubscribeToggleMouseTransformationModeAction() => Subscribe(m_ToggleMouseTransformationModeAction, OnToggleMouseTransformationModePerformed);
  1579. void UnsubscribeToggleMouseTransformationModeAction() => Unsubscribe(m_ToggleMouseTransformationModeAction, OnToggleMouseTransformationModePerformed);
  1580. void SubscribeNegateModeAction() => Subscribe(m_NegateModeAction, OnNegateModePerformed, OnNegateModeCanceled);
  1581. void UnsubscribeNegateModeAction() => Unsubscribe(m_NegateModeAction, OnNegateModePerformed, OnNegateModeCanceled);
  1582. void SubscribeXConstraintAction() => Subscribe(m_XConstraintAction, OnXConstraintPerformed, OnXConstraintCanceled);
  1583. void UnsubscribeXConstraintAction() => Unsubscribe(m_XConstraintAction, OnXConstraintPerformed, OnXConstraintCanceled);
  1584. void SubscribeYConstraintAction() => Subscribe(m_YConstraintAction, OnYConstraintPerformed, OnYConstraintCanceled);
  1585. void UnsubscribeYConstraintAction() => Unsubscribe(m_YConstraintAction, OnYConstraintPerformed, OnYConstraintCanceled);
  1586. void SubscribeZConstraintAction() => Subscribe(m_ZConstraintAction, OnZConstraintPerformed, OnZConstraintCanceled);
  1587. void UnsubscribeZConstraintAction() => Unsubscribe(m_ZConstraintAction, OnZConstraintPerformed, OnZConstraintCanceled);
  1588. void SubscribeResetAction() => Subscribe(m_ResetAction, OnResetPerformed, OnResetCanceled);
  1589. void UnsubscribeResetAction() => Unsubscribe(m_ResetAction, OnResetPerformed, OnResetCanceled);
  1590. void SubscribeToggleCursorLockAction() => Subscribe(m_ToggleCursorLockAction, OnToggleCursorLockPerformed);
  1591. void UnsubscribeToggleCursorLockAction() => Unsubscribe(m_ToggleCursorLockAction, OnToggleCursorLockPerformed);
  1592. void SubscribeToggleDevicePositionTargetAction() => Subscribe(m_ToggleDevicePositionTargetAction, OnToggleDevicePositionTargetPerformed);
  1593. void UnsubscribeToggleDevicePositionTargetAction() => Unsubscribe(m_ToggleDevicePositionTargetAction, OnToggleDevicePositionTargetPerformed);
  1594. void SubscribeTogglePrimary2DAxisTargetAction() => Subscribe(m_TogglePrimary2DAxisTargetAction, OnTogglePrimary2DAxisTargetPerformed);
  1595. void UnsubscribeTogglePrimary2DAxisTargetAction() => Unsubscribe(m_TogglePrimary2DAxisTargetAction, OnTogglePrimary2DAxisTargetPerformed);
  1596. void SubscribeToggleSecondary2DAxisTargetAction() => Subscribe(m_ToggleSecondary2DAxisTargetAction, OnToggleSecondary2DAxisTargetPerformed);
  1597. void UnsubscribeToggleSecondary2DAxisTargetAction() => Unsubscribe(m_ToggleSecondary2DAxisTargetAction, OnToggleSecondary2DAxisTargetPerformed);
  1598. void SubscribeAxis2DAction() => Subscribe(m_Axis2DAction, OnAxis2DPerformed, OnAxis2DCanceled);
  1599. void UnsubscribeAxis2DAction() => Unsubscribe(m_Axis2DAction, OnAxis2DPerformed, OnAxis2DCanceled);
  1600. void SubscribeRestingHandAxis2DAction() => Subscribe(m_RestingHandAxis2DAction, OnRestingHandAxis2DPerformed, OnRestingHandAxis2DCanceled);
  1601. void UnsubscribeRestingHandAxis2DAction() => Unsubscribe(m_RestingHandAxis2DAction, OnRestingHandAxis2DPerformed, OnRestingHandAxis2DCanceled);
  1602. void SubscribeGripAction() => Subscribe(m_GripAction, OnGripPerformed, OnGripCanceled);
  1603. void UnsubscribeGripAction() => Unsubscribe(m_GripAction, OnGripPerformed, OnGripCanceled);
  1604. void SubscribeTriggerAction() => Subscribe(m_TriggerAction, OnTriggerPerformed, OnTriggerCanceled);
  1605. void UnsubscribeTriggerAction() => Unsubscribe(m_TriggerAction, OnTriggerPerformed, OnTriggerCanceled);
  1606. void SubscribePrimaryButtonAction() => Subscribe(m_PrimaryButtonAction, OnPrimaryButtonPerformed, OnPrimaryButtonCanceled);
  1607. void UnsubscribePrimaryButtonAction() => Unsubscribe(m_PrimaryButtonAction, OnPrimaryButtonPerformed, OnPrimaryButtonCanceled);
  1608. void SubscribeSecondaryButtonAction() => Subscribe(m_SecondaryButtonAction, OnSecondaryButtonPerformed, OnSecondaryButtonCanceled);
  1609. void UnsubscribeSecondaryButtonAction() => Unsubscribe(m_SecondaryButtonAction, OnSecondaryButtonPerformed, OnSecondaryButtonCanceled);
  1610. void SubscribeMenuAction() => Subscribe(m_MenuAction, OnMenuPerformed, OnMenuCanceled);
  1611. void UnsubscribeMenuAction() => Unsubscribe(m_MenuAction, OnMenuPerformed, OnMenuCanceled);
  1612. void SubscribePrimary2DAxisClickAction() => Subscribe(m_Primary2DAxisClickAction, OnPrimary2DAxisClickPerformed, OnPrimary2DAxisClickCanceled);
  1613. void UnsubscribePrimary2DAxisClickAction() => Unsubscribe(m_Primary2DAxisClickAction, OnPrimary2DAxisClickPerformed, OnPrimary2DAxisClickCanceled);
  1614. void SubscribeSecondary2DAxisClickAction() => Subscribe(m_Secondary2DAxisClickAction, OnSecondary2DAxisClickPerformed, OnSecondary2DAxisClickCanceled);
  1615. void UnsubscribeSecondary2DAxisClickAction() => Unsubscribe(m_Secondary2DAxisClickAction, OnSecondary2DAxisClickPerformed, OnSecondary2DAxisClickCanceled);
  1616. void SubscribePrimary2DAxisTouchAction() => Subscribe(m_Primary2DAxisTouchAction, OnPrimary2DAxisTouchPerformed, OnPrimary2DAxisTouchCanceled);
  1617. void UnsubscribePrimary2DAxisTouchAction() => Unsubscribe(m_Primary2DAxisTouchAction, OnPrimary2DAxisTouchPerformed, OnPrimary2DAxisTouchCanceled);
  1618. void SubscribeSecondary2DAxisTouchAction() => Subscribe(m_Secondary2DAxisTouchAction, OnSecondary2DAxisTouchPerformed, OnSecondary2DAxisTouchCanceled);
  1619. void UnsubscribeSecondary2DAxisTouchAction() => Unsubscribe(m_Secondary2DAxisTouchAction, OnSecondary2DAxisTouchPerformed, OnSecondary2DAxisTouchCanceled);
  1620. void SubscribePrimaryTouchAction() => Subscribe(m_PrimaryTouchAction, OnPrimaryTouchPerformed, OnPrimaryTouchCanceled);
  1621. void UnsubscribePrimaryTouchAction() => Unsubscribe(m_PrimaryTouchAction, OnPrimaryTouchPerformed, OnPrimaryTouchCanceled);
  1622. void SubscribeSecondaryTouchAction() => Subscribe(m_SecondaryTouchAction, OnSecondaryTouchPerformed, OnSecondaryTouchCanceled);
  1623. void UnsubscribeSecondaryTouchAction() => Unsubscribe(m_SecondaryTouchAction, OnSecondaryTouchPerformed, OnSecondaryTouchCanceled);
  1624. void OnKeyboardXTranslatePerformed(InputAction.CallbackContext context) => m_KeyboardXTranslateInput = context.ReadValue<float>();
  1625. void OnKeyboardXTranslateCanceled(InputAction.CallbackContext context) => m_KeyboardXTranslateInput = 0f;
  1626. void OnKeyboardYTranslatePerformed(InputAction.CallbackContext context) => m_KeyboardYTranslateInput = context.ReadValue<float>();
  1627. void OnKeyboardYTranslateCanceled(InputAction.CallbackContext context) => m_KeyboardYTranslateInput = 0f;
  1628. void OnKeyboardZTranslatePerformed(InputAction.CallbackContext context) => m_KeyboardZTranslateInput = context.ReadValue<float>();
  1629. void OnKeyboardZTranslateCanceled(InputAction.CallbackContext context) => m_KeyboardZTranslateInput = 0f;
  1630. void OnManipulateLeftPerformed(InputAction.CallbackContext context) => m_ManipulateLeftInput = true;
  1631. void OnManipulateLeftCanceled(InputAction.CallbackContext context) => m_ManipulateLeftInput = false;
  1632. void OnManipulateRightPerformed(InputAction.CallbackContext context) => m_ManipulateRightInput = true;
  1633. void OnManipulateRightCanceled(InputAction.CallbackContext context) => m_ManipulateRightInput = false;
  1634. void OnToggleManipulateLeftPerformed(InputAction.CallbackContext context) => m_ManipulateLeftInput = !m_ManipulateLeftInput;
  1635. void OnToggleManipulateRightPerformed(InputAction.CallbackContext context) => m_ManipulateRightInput = !m_ManipulateRightInput;
  1636. void OnManipulateHeadPerformed(InputAction.CallbackContext context) => m_ManipulateHeadInput = true;
  1637. void OnManipulateHeadCanceled(InputAction.CallbackContext context) => m_ManipulateHeadInput = false;
  1638. void OnMouseDeltaPerformed(InputAction.CallbackContext context) => m_MouseDeltaInput = context.ReadValue<Vector2>();
  1639. void OnMouseDeltaCanceled(InputAction.CallbackContext context) => m_MouseDeltaInput = Vector2.zero;
  1640. void OnMouseScrollPerformed(InputAction.CallbackContext context) => m_MouseScrollInput = context.ReadValue<Vector2>();
  1641. void OnMouseScrollCanceled(InputAction.CallbackContext context) => m_MouseScrollInput = Vector2.zero;
  1642. void OnRotateModeOverridePerformed(InputAction.CallbackContext context) => m_RotateModeOverrideInput = true;
  1643. void OnRotateModeOverrideCanceled(InputAction.CallbackContext context) => m_RotateModeOverrideInput = false;
  1644. void OnToggleMouseTransformationModePerformed(InputAction.CallbackContext context) => mouseTransformationMode = Negate(mouseTransformationMode);
  1645. void OnNegateModePerformed(InputAction.CallbackContext context) => m_NegateModeInput = true;
  1646. void OnNegateModeCanceled(InputAction.CallbackContext context) => m_NegateModeInput = false;
  1647. void OnXConstraintPerformed(InputAction.CallbackContext context) => m_XConstraintInput = true;
  1648. void OnXConstraintCanceled(InputAction.CallbackContext context) => m_XConstraintInput = false;
  1649. void OnYConstraintPerformed(InputAction.CallbackContext context) => m_YConstraintInput = true;
  1650. void OnYConstraintCanceled(InputAction.CallbackContext context) => m_YConstraintInput = false;
  1651. void OnZConstraintPerformed(InputAction.CallbackContext context) => m_ZConstraintInput = true;
  1652. void OnZConstraintCanceled(InputAction.CallbackContext context) => m_ZConstraintInput = false;
  1653. void OnResetPerformed(InputAction.CallbackContext context) => m_ResetInput = true;
  1654. void OnResetCanceled(InputAction.CallbackContext context) => m_ResetInput = false;
  1655. void OnToggleCursorLockPerformed(InputAction.CallbackContext context) => Cursor.lockState = Negate(Cursor.lockState);
  1656. void OnToggleDevicePositionTargetPerformed(InputAction.CallbackContext context) => axis2DTargets ^= Axis2DTargets.Position;
  1657. void OnTogglePrimary2DAxisTargetPerformed(InputAction.CallbackContext context) => axis2DTargets ^= Axis2DTargets.Primary2DAxis;
  1658. void OnToggleSecondary2DAxisTargetPerformed(InputAction.CallbackContext context) => axis2DTargets ^= Axis2DTargets.Secondary2DAxis;
  1659. void OnAxis2DPerformed(InputAction.CallbackContext context) => m_Axis2DInput = Vector2.ClampMagnitude(context.ReadValue<Vector2>(), 1f);
  1660. void OnAxis2DCanceled(InputAction.CallbackContext context) => m_Axis2DInput = Vector2.zero;
  1661. void OnRestingHandAxis2DPerformed(InputAction.CallbackContext context) => m_RestingHandAxis2DInput = Vector2.ClampMagnitude(context.ReadValue<Vector2>(), 1f);
  1662. void OnRestingHandAxis2DCanceled(InputAction.CallbackContext context) => m_RestingHandAxis2DInput = Vector2.zero;
  1663. void OnGripPerformed(InputAction.CallbackContext context) => m_GripInput = true;
  1664. void OnGripCanceled(InputAction.CallbackContext context) => m_GripInput = false;
  1665. void OnTriggerPerformed(InputAction.CallbackContext context) => m_TriggerInput = true;
  1666. void OnTriggerCanceled(InputAction.CallbackContext context) => m_TriggerInput = false;
  1667. void OnPrimaryButtonPerformed(InputAction.CallbackContext context) => m_PrimaryButtonInput = true;
  1668. void OnPrimaryButtonCanceled(InputAction.CallbackContext context) => m_PrimaryButtonInput = false;
  1669. void OnSecondaryButtonPerformed(InputAction.CallbackContext context) => m_SecondaryButtonInput = true;
  1670. void OnSecondaryButtonCanceled(InputAction.CallbackContext context) => m_SecondaryButtonInput = false;
  1671. void OnMenuPerformed(InputAction.CallbackContext context) => m_MenuInput = true;
  1672. void OnMenuCanceled(InputAction.CallbackContext context) => m_MenuInput = false;
  1673. void OnPrimary2DAxisClickPerformed(InputAction.CallbackContext context) => m_Primary2DAxisClickInput = true;
  1674. void OnPrimary2DAxisClickCanceled(InputAction.CallbackContext context) => m_Primary2DAxisClickInput = false;
  1675. void OnSecondary2DAxisClickPerformed(InputAction.CallbackContext context) => m_Secondary2DAxisClickInput = true;
  1676. void OnSecondary2DAxisClickCanceled(InputAction.CallbackContext context) => m_Secondary2DAxisClickInput = false;
  1677. void OnPrimary2DAxisTouchPerformed(InputAction.CallbackContext context) => m_Primary2DAxisTouchInput = true;
  1678. void OnPrimary2DAxisTouchCanceled(InputAction.CallbackContext context) => m_Primary2DAxisTouchInput = false;
  1679. void OnSecondary2DAxisTouchPerformed(InputAction.CallbackContext context) => m_Secondary2DAxisTouchInput = true;
  1680. void OnSecondary2DAxisTouchCanceled(InputAction.CallbackContext context) => m_Secondary2DAxisTouchInput = false;
  1681. void OnPrimaryTouchPerformed(InputAction.CallbackContext context) => m_PrimaryTouchInput = true;
  1682. void OnPrimaryTouchCanceled(InputAction.CallbackContext context) => m_PrimaryTouchInput = false;
  1683. void OnSecondaryTouchPerformed(InputAction.CallbackContext context) => m_SecondaryTouchInput = true;
  1684. void OnSecondaryTouchCanceled(InputAction.CallbackContext context) => m_SecondaryTouchInput = false;
  1685. static InputAction GetInputAction(InputActionReference actionReference)
  1686. {
  1687. #pragma warning disable IDE0031 // Use null propagation -- Do not use for UnityEngine.Object types
  1688. return actionReference != null ? actionReference.action : null;
  1689. #pragma warning restore IDE0031
  1690. }
  1691. /// <summary>
  1692. /// Add simulated XR devices to the Input System.
  1693. /// </summary>
  1694. /// <see href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.2/api/UnityEngine.InputSystem.InputSystem.html#UnityEngine_InputSystem_InputSystem_AddDevice__1_System_String_"/>
  1695. protected virtual void AddDevices() {
  1696. m_HMDDevice = InputSystem.AddDevice<XRSimulatedHMD>();
  1697. if (m_HMDDevice == null) {
  1698. Debug.LogError($"Failed to create {nameof(XRSimulatedHMD)}.");
  1699. }
  1700. m_LeftControllerDevice = InputSystem.AddDevice<XRSimulatedController>($"{nameof(XRSimulatedController)} - { UnityEngine.InputSystem.CommonUsages.LeftHand}");
  1701. if (m_LeftControllerDevice != null) {
  1702. InputSystem.SetDeviceUsage(m_LeftControllerDevice, UnityEngine.InputSystem.CommonUsages.LeftHand);
  1703. } else {
  1704. Debug.LogError($"Failed to create {nameof(XRSimulatedController)} for { UnityEngine.InputSystem.CommonUsages.LeftHand}.", this);
  1705. }
  1706. m_RightControllerDevice = InputSystem.AddDevice<XRSimulatedController>($"{nameof(XRSimulatedController)} - { UnityEngine.InputSystem.CommonUsages.RightHand}");
  1707. if (m_RightControllerDevice != null) {
  1708. InputSystem.SetDeviceUsage(m_RightControllerDevice, UnityEngine.InputSystem.CommonUsages.RightHand);
  1709. } else {
  1710. Debug.LogError($"Failed to create {nameof(XRSimulatedController)} for { UnityEngine.InputSystem.CommonUsages.RightHand}.", this);
  1711. }
  1712. }
  1713. /// <summary>
  1714. /// Remove simulated XR devices from the Input System.
  1715. /// </summary>
  1716. /// <see href="https://docs.unity3d.com/Packages/com.unity.inputsystem@1.2/api/UnityEngine.InputSystem.InputSystem.html#UnityEngine_InputSystem_InputSystem_RemoveDevice_UnityEngine_InputSystem_InputDevice_"/>
  1717. protected virtual void RemoveDevices() {
  1718. if (m_HMDDevice != null && m_HMDDevice.added)
  1719. InputSystem.RemoveDevice(m_HMDDevice);
  1720. if (m_LeftControllerDevice != null && m_LeftControllerDevice.added)
  1721. InputSystem.RemoveDevice(m_LeftControllerDevice);
  1722. if (m_RightControllerDevice != null && m_RightControllerDevice.added)
  1723. InputSystem.RemoveDevice(m_RightControllerDevice);
  1724. }
  1725. }
  1726. }