InputSystem.cs 157 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Reflection;
  4. using UnityEngine.InputSystem.Haptics;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using UnityEngine.InputSystem.Layouts;
  7. using UnityEngine.InputSystem.LowLevel;
  8. using UnityEngine.InputSystem.DualShock;
  9. using UnityEngine.InputSystem.HID;
  10. using UnityEngine.InputSystem.Users;
  11. using UnityEngine.InputSystem.XInput;
  12. using UnityEngine.InputSystem.Utilities;
  13. #if UNITY_EDITOR
  14. using UnityEditor;
  15. using UnityEngine.InputSystem.Editor;
  16. using UnityEditor.Networking.PlayerConnection;
  17. #else
  18. using System.Linq;
  19. using UnityEngine.Networking.PlayerConnection;
  20. #endif
  21. ////TODO: allow aliasing processors etc
  22. ////REVIEW: rename all references to "frame" to refer to "update" instead (e.g. wasPressedThisUpdate)?
  23. ////TODO: add APIs to get to the state blocks (equivalent to what you currently get with e.g. InputSystem.devices[0].currentStatePtr)
  24. ////FIXME: modal dialogs (or anything that interrupts normal Unity operation) are likely a problem for the system as is; there's a good
  25. //// chance the event queue will just get swamped; should be only the background queue though so I guess once it fills up we
  26. //// simply start losing input but it won't grow infinitely
  27. ////REVIEW: make more APIs thread-safe?
  28. ////REVIEW: it'd be great to be able to set up monitors from control paths (independently of actions; or should we just use actions?)
  29. ////REVIEW: have InputSystem.onTextInput that's fired directly from the event processing loop?
  30. //// (and allow text input events that have no associated target device? this way we don't need a keyboard to get text input)
  31. ////REVIEW: split lower-level APIs (anything mentioning events and state) off into InputSystemLowLevel API to make this API more focused?
  32. ////TODO: release native allocations when exiting
  33. namespace UnityEngine.InputSystem
  34. {
  35. /// <summary>
  36. /// This is the central hub for the input system.
  37. /// </summary>
  38. /// <remarks>
  39. /// This class has the central APIs for working with the input system. You
  40. /// can manage devices available in the system (<see cref="AddDevice{TDevice}"/>,
  41. /// <see cref="devices"/>, <see cref="onDeviceChange"/> and related APIs) or extend
  42. /// the input system with custom functionality (<see cref="RegisterLayout{TLayout}"/>,
  43. /// <see cref="RegisterInteraction{T}"/>, <see cref="RegisterProcessor{T}"/>,
  44. /// <see cref="RegisterBindingComposite{T}"/>, and related APIs).
  45. ///
  46. /// To control haptics globally, you can use <see cref="PauseHaptics"/>, <see cref="ResumeHaptics"/>,
  47. /// and <see cref="ResetHaptics"/>.
  48. ///
  49. /// To enable and disable individual devices (such as <see cref="Sensor"/> devices),
  50. /// you can use <see cref="EnableDevice"/> and <see cref="DisableDevice"/>.
  51. ///
  52. /// The input system is initialized as part of Unity starting up. It is generally safe
  53. /// to call the APIs here from any of Unity's script callbacks.
  54. ///
  55. /// Note that, like most Unity APIs, most of the properties and methods in this API can only
  56. /// be called on the main thread. However, select APIs like <see cref="QueueEvent"/> can be
  57. /// called from threads. Where this is the case, it is stated in the documentation.
  58. /// </remarks>
  59. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1724:TypeNamesShouldNotMatchNamespaces", Justification = "Options for namespaces are limited due to the legacy input class. Agreed on this as the least bad solution.")]
  60. #if UNITY_EDITOR
  61. [InitializeOnLoad]
  62. #endif
  63. public static class InputSystem
  64. {
  65. #region Layouts
  66. /// <summary>
  67. /// Event that is signalled when the layout setup in the system changes.
  68. /// </summary>
  69. /// <remarks>
  70. /// First parameter is the name of the layout that has changed and second parameter is the
  71. /// type of change that has occurred.
  72. ///
  73. /// <example>
  74. /// <code>
  75. /// InputSystem.onLayoutChange +=
  76. /// (name, change) =>
  77. /// {
  78. /// switch (change)
  79. /// {
  80. /// case InputControlLayoutChange.Added:
  81. /// Debug.Log($"New layout {name} has been added");
  82. /// break;
  83. /// case InputControlLayoutChange.Removed:
  84. /// Debug.Log($"Layout {name} has been removed");
  85. /// break;
  86. /// case InputControlLayoutChange.Replaced:
  87. /// Debug.Log($"Layout {name} has been updated");
  88. /// break;
  89. /// }
  90. /// }
  91. /// </code>
  92. /// </example>
  93. /// </remarks>
  94. /// <seealso cref="InputControlLayout"/>
  95. public static event Action<string, InputControlLayoutChange> onLayoutChange
  96. {
  97. add
  98. {
  99. lock (s_Manager)
  100. s_Manager.onLayoutChange += value;
  101. }
  102. remove
  103. {
  104. lock (s_Manager)
  105. s_Manager.onLayoutChange -= value;
  106. }
  107. }
  108. /// <summary>
  109. /// Register a control layout based on a type.
  110. /// </summary>
  111. /// <param name="type">Type to derive a control layout from. Must be derived from <see cref="InputControl"/>.</param>
  112. /// <param name="name">Name to use for the layout. If null or empty, the short name of the type (<see cref="Type.Name"/>) will be used.</param>
  113. /// <param name="matches">Optional device matcher. If this is supplied, the layout will automatically
  114. /// be instantiated for newly discovered devices that match the description.</param>
  115. /// <remarks>
  116. /// When the layout is instantiated, the system will reflect on all public fields and properties of the type
  117. /// which have a value type derived from <see cref="InputControl"/> or which are annotated with <see cref="InputControlAttribute"/>.
  118. ///
  119. /// The type can be annotated with <see cref="InputControlLayoutAttribute"/> for additional options
  120. /// but the attribute is not necessary for a type to be usable as a control layout. Note that if the type
  121. /// does have <see cref="InputControlLayoutAttribute"/> and has set <see cref="InputControlLayoutAttribute.stateType"/>,
  122. /// the system will <em>not</em> reflect on properties and fields in the type but do that on the given
  123. /// state type instead.
  124. ///
  125. /// <example>
  126. /// <code>
  127. /// // InputControlLayoutAttribute attribute is only necessary if you want
  128. /// // to override default behavior that occurs when registering your device
  129. /// // as a layout.
  130. /// // The most common use of InputControlLayoutAttribute is to direct the system
  131. /// // to a custom "state struct" through the `stateType` property. See below for details.
  132. /// [InputControlLayout(displayName = "My Device", stateType = typeof(MyDeviceState))]
  133. /// #if UNITY_EDITOR
  134. /// [InitializeOnLoad]
  135. /// #endif
  136. /// public class MyDevice : InputDevice
  137. /// {
  138. /// public ButtonControl button { get; private set; }
  139. /// public AxisControl axis { get; private set; }
  140. ///
  141. /// // Register the device.
  142. /// static MyDevice()
  143. /// {
  144. /// // In case you want instance of your device to automatically be created
  145. /// // when specific hardware is detected by the Unity runtime, you have to
  146. /// // add one or more "device matchers" (InputDeviceMatcher) for the layout.
  147. /// // These matchers are compared to an InputDeviceDescription received from
  148. /// // the Unity runtime when a device is connected. You can add them either
  149. /// // using InputSystem.RegisterLayoutMatcher() or by directly specifying a
  150. /// // matcher when registering the layout.
  151. /// InputSystem.RegisterLayout&lt;MyDevice&gt;(
  152. /// // For the sake of demonstration, let's assume your device is a HID
  153. /// // and you want to match by PID and VID.
  154. /// matches: new InputDeviceMatcher()
  155. /// .WithInterface("HID")
  156. /// .WithCapability("PID", 1234)
  157. /// .WithCapability("VID", 5678));
  158. /// }
  159. ///
  160. /// // This is only to trigger the static class constructor to automatically run
  161. /// // in the player.
  162. /// [RuntimeInitializeOnLoadMethod]
  163. /// private static void InitializeInPlayer() {}
  164. ///
  165. /// protected override void FinishSetup()
  166. /// {
  167. /// base.FinishSetup();
  168. /// button = GetChildControl&lt;ButtonControl&gt;("button");
  169. /// axis = GetChildControl&lt;AxisControl&gt;("axis");
  170. /// }
  171. /// }
  172. ///
  173. /// // A "state struct" describes the memory format used by a device. Each device can
  174. /// // receive and store memory in its custom format. InputControls are then connected
  175. /// // the individual pieces of memory and read out values from them.
  176. /// [StructLayout(LayoutKind.Explicit, Size = 32)]
  177. /// public struct MyDeviceState : IInputStateTypeInfo
  178. /// {
  179. /// // In the case of a HID (which we assume for the sake of this demonstration),
  180. /// // the format will be "HID". In practice, the format will depend on how your
  181. /// // particular device is connected and fed into the input system.
  182. /// // The format is a simple FourCC code that "tags" state memory blocks for the
  183. /// // device to give a base level of safety checks on memory operations.
  184. /// public FourCC format => return new FourCC('H', 'I', 'D');
  185. ///
  186. /// // InputControlAttributes on fields tell the input system to create controls
  187. /// // for the public fields found in the struct.
  188. ///
  189. /// // Assume a 16bit field of buttons. Create one button that is tied to
  190. /// // bit #3 (zero-based). Note that buttons do not need to be stored as bits.
  191. /// // They can also be stored as floats or shorts, for example.
  192. /// [InputControl(name = "button", layout = "Button", bit = 3)]
  193. /// public ushort buttons;
  194. ///
  195. /// // Create a floating-point axis. The name, if not supplied, is taken from
  196. /// // the field.
  197. /// [InputControl(layout = "Axis")]
  198. /// public short axis;
  199. /// }
  200. /// </code>
  201. /// </example>
  202. ///
  203. /// Note that if <paramref name="matches"/> is supplied, it will immediately be matched
  204. /// against the descriptions (<see cref="InputDeviceDescription"/>) of all available devices.
  205. /// If it matches any description where no layout matched before, a new device will immediately
  206. /// be created (except if suppressed by <see cref="InputSettings.supportedDevices"/>). If it
  207. /// matches a description better (see <see cref="InputDeviceMatcher.MatchPercentage"/>) than
  208. /// the currently used layout, the existing device will be a removed and a new device with
  209. /// the newly registered layout will be created.
  210. ///
  211. /// See <see cref="Controls.StickControl"/> or <see cref="Gamepad"/> for examples of layouts.
  212. /// </remarks>
  213. /// <exception cref="ArgumentNullException"><paramref name="type"/> is null.</exception>
  214. /// <seealso cref="InputControlLayout"/>
  215. public static void RegisterLayout(Type type, string name = null, InputDeviceMatcher? matches = null)
  216. {
  217. if (type == null)
  218. throw new ArgumentNullException(nameof(type));
  219. if (string.IsNullOrEmpty(name))
  220. name = type.Name;
  221. s_Manager.RegisterControlLayout(name, type);
  222. if (matches != null)
  223. s_Manager.RegisterControlLayoutMatcher(name, matches.Value);
  224. }
  225. /// <summary>
  226. /// Register a type as a control layout.
  227. /// </summary>
  228. /// <typeparam name="T">Type to derive a control layout from.</typeparam>
  229. /// <param name="name">Name to use for the layout. If null or empty, the short name of the type will be used.</param>
  230. /// <param name="matches">Optional device matcher. If this is supplied, the layout will automatically
  231. /// be instantiated for newly discovered devices that match the description.</param>
  232. /// <remarks>
  233. /// This method is equivalent to calling <see cref="RegisterLayout(Type,string,InputDeviceMatcher?)"/> with
  234. /// <c>typeof(T)</c>. See that method for details of the layout registration process.
  235. /// </remarks>
  236. /// <seealso cref="RegisterLayout(Type,string,InputDeviceMatcher?)"/>
  237. public static void RegisterLayout<T>(string name = null, InputDeviceMatcher? matches = null)
  238. where T : InputControl
  239. {
  240. RegisterLayout(typeof(T), name, matches);
  241. }
  242. /// <summary>
  243. /// Register a layout in JSON format.
  244. /// </summary>
  245. /// <param name="json">JSON data describing the layout.</param>
  246. /// <param name="name">Optional name of the layout. If null or empty, the name is taken from the "name"
  247. /// property of the JSON data. If it is supplied, it will override the "name" property if present. If neither
  248. /// is supplied, an <see cref="ArgumentException"/> is thrown.</param>
  249. /// <param name="matches">Optional device matcher. If this is supplied, the layout will automatically
  250. /// be instantiated for newly discovered devices that match the description.</param>
  251. /// <exception cref="ArgumentNullException"><paramref name="json"/> is null or empty.</exception>
  252. /// <exception cref="ArgumentException">No name has been supplied either through <paramref name="name"/>
  253. /// or the "name" JSON property.</exception>
  254. /// <remarks>
  255. /// The JSON format makes it possible to create new device and control layouts completely
  256. /// in data. They have to ultimately be based on a layout backed by a C# type, however (e.g.
  257. /// <see cref="Gamepad"/>).
  258. ///
  259. /// Note that most errors in layouts will only be detected when instantiated (i.e. when a device or control is
  260. /// being created from a layout). The JSON data will, however, be parsed once on registration to check for a
  261. /// device description in the layout. JSON format errors will thus be detected during registration.
  262. ///
  263. /// <example>
  264. /// <code>
  265. /// InputSystem.RegisterLayout(@"
  266. /// {
  267. /// ""name"" : ""MyDevice"",
  268. /// ""controls"" : [
  269. /// {
  270. /// ""name"" : ""myButton"",
  271. /// ""layout"" : ""Button""
  272. /// }
  273. /// ]
  274. /// }
  275. /// );
  276. /// </code>
  277. /// </example>
  278. /// </remarks>
  279. /// <seealso cref="RemoveLayout"/>
  280. public static void RegisterLayout(string json, string name = null, InputDeviceMatcher? matches = null)
  281. {
  282. s_Manager.RegisterControlLayout(json, name);
  283. if (matches != null)
  284. s_Manager.RegisterControlLayoutMatcher(name, matches.Value);
  285. }
  286. /// <summary>
  287. /// Register a layout that applies overrides to one or more other layouts.
  288. /// </summary>
  289. /// <param name="json">Layout in JSON format.</param>
  290. /// <param name="name">Optional name of the layout. If null or empty, the name is taken from the "name"
  291. /// property of the JSON data. If it is supplied, it will override the "name" property if present. If neither
  292. /// is supplied, an <see cref="ArgumentException"/> is thrown.</param>
  293. /// <remarks>
  294. /// Layout overrides are layout pieces that are applied on top of existing layouts.
  295. /// This can be used to modify any layout in the system non-destructively. The process works the
  296. /// same as extending an existing layout except that instead of creating a new layout
  297. /// by merging the derived layout and the base layout, the overrides are merged
  298. /// directly into the base layout.
  299. ///
  300. /// The layout merging logic used for overrides, is the same as the one used for
  301. /// derived layouts, i.e. <see cref="InputControlLayout.MergeLayout"/>.
  302. ///
  303. /// Layouts used as overrides look the same as normal layouts and have the same format.
  304. /// The only difference is that they are explicitly registered as overrides.
  305. ///
  306. /// Note that unlike "normal" layouts, layout overrides have the ability to extend
  307. /// multiple base layouts. The changes from the override will simply be merged into
  308. /// each of the layouts it extends. Use the <c>extendMultiple</c> rather than the
  309. /// <c>extend</c> property in JSON to give a list of base layouts instead of a single
  310. /// one.
  311. ///
  312. /// <example>
  313. /// <code>
  314. /// // Override default button press points on the gamepad triggers.
  315. /// InputSystem.RegisterLayoutOverride(@"
  316. /// {
  317. /// ""name"" : ""CustomTriggerPressPoints"",
  318. /// ""extend"" : ""Gamepad"",
  319. /// ""controls"" : [
  320. /// { ""name"" : ""leftTrigger"", ""parameters"" : ""pressPoint=0.25"" },
  321. /// { ""name"" : ""rightTrigger"", ""parameters"" : ""pressPoint=0.25"" }
  322. /// ]
  323. /// }
  324. /// ");
  325. /// </code>
  326. /// </example>
  327. /// </remarks>
  328. public static void RegisterLayoutOverride(string json, string name = null)
  329. {
  330. s_Manager.RegisterControlLayout(json, name, isOverride: true);
  331. }
  332. /// <summary>
  333. /// Add an additional device matcher to an existing layout.
  334. /// </summary>
  335. /// <param name="layoutName">Name of the device layout that should be instantiated if <paramref name="matcher"/>
  336. /// matches an <see cref="InputDeviceDescription"/> of a discovered device.</param>
  337. /// <param name="matcher">Specification to match against <see cref="InputDeviceDescription"/> instances.</param>
  338. /// <remarks>
  339. /// Each device layout can have zero or more matchers associated with it. If any one of the
  340. /// matchers matches a given <see cref="InputDeviceDescription"/> (see <see cref="InputDeviceMatcher.MatchPercentage"/>)
  341. /// better than any other matcher (for the same or any other layout), then the given layout
  342. /// will be used for the discovered device.
  343. ///
  344. /// Note that registering a matcher may immediately lead to devices being created or recreated.
  345. /// If <paramref name="matcher"/> matches any devices currently on the list of unsupported devices
  346. /// (see <see cref="GetUnsupportedDevices()"/>), new <see cref="InputDevice"/>s will be created
  347. /// using the layout called <paramref name="layoutName"/>. Also, if <paramref name="matcher"/>
  348. /// matches the description of a device better than the matcher (if any) for the device's currently
  349. /// used layout, the device will be recreated using the given layout.
  350. /// </remarks>
  351. /// <exception cref="ArgumentNullException"><paramref name="layoutName"/> is <c>null</c> or empty/</exception>
  352. /// <exception cref="ArgumentException"><paramref name="matcher"/> is empty (<see cref="InputDeviceMatcher.empty"/>).</exception>
  353. /// <seealso cref="RegisterLayout(Type,string,InputDeviceMatcher?)"/>
  354. /// <seealso cref="TryFindMatchingLayout"/>
  355. public static void RegisterLayoutMatcher(string layoutName, InputDeviceMatcher matcher)
  356. {
  357. s_Manager.RegisterControlLayoutMatcher(layoutName, matcher);
  358. }
  359. /// <summary>
  360. /// Add an additional device matcher to the layout registered for <typeparamref name="TDevice"/>.
  361. /// </summary>
  362. /// <param name="matcher">A device matcher.</param>
  363. /// <typeparam name="TDevice">Type that has been registered as a layout. See <see cref="RegisterLayout{T}"/>.</typeparam>
  364. /// <remarks>
  365. /// Calling this method is equivalent to calling <see cref="RegisterLayoutMatcher(string,InputDeviceMatcher)"/>
  366. /// with the name under which <typeparamref name="TDevice"/> has been registered.
  367. /// </remarks>
  368. /// <exception cref="ArgumentException"><paramref name="matcher"/> is empty (<see cref="InputDeviceMatcher.empty"/>)
  369. /// -or- <typeparamref name="TDevice"/> has not been registered as a layout.</exception>
  370. public static void RegisterLayoutMatcher<TDevice>(InputDeviceMatcher matcher)
  371. where TDevice : InputDevice
  372. {
  373. s_Manager.RegisterControlLayoutMatcher(typeof(TDevice), matcher);
  374. }
  375. /// <summary>
  376. /// Register a builder that delivers an <see cref="InputControlLayout"/> instance on demand.
  377. /// </summary>
  378. /// <param name="buildMethod">Method to invoke to generate a layout when the layout is chosen.
  379. /// Should not cache the layout but rather return a fresh instance every time.</param>
  380. /// <param name="name">Name under which to register the layout. If a layout with the same
  381. /// name is already registered, the call to this method will replace the existing layout.</param>
  382. /// <param name="baseLayout">Name of the layout that the layout returned from <paramref name="buildMethod"/>
  383. /// will be based on. The system needs to know this in advance in order to update devices
  384. /// correctly if layout registrations in the system are changed.</param>
  385. /// <param name="matches">Optional matcher for an <see cref="InputDeviceDescription"/>. If supplied,
  386. /// it is equivalent to calling <see cref="RegisterLayoutMatcher"/>.</param>
  387. /// <exception cref="ArgumentNullException"><paramref name="buildMethod"/> is <c>null</c> -or-
  388. /// <paramref name="name"/> is <c>null</c> or empty.</exception>
  389. /// <remarks>
  390. /// Layout builders are most useful for procedurally building device layouts from metadata
  391. /// supplied by external systems. A good example is <see cref="HID"/> where the "HID" standard
  392. /// includes a way for input devices to describe their various inputs and outputs in the form
  393. /// of a <see cref="HID.HIDDeviceDescriptor"/>. While not sufficient to build a perfectly robust
  394. /// <see cref="InputDevice"/>, these descriptions are usually enough to at least make the device
  395. /// work out-of-the-box to some extent.
  396. ///
  397. /// The builder method would usually use <see cref="InputControlLayout.Builder"/> to build the
  398. /// actual layout.
  399. ///
  400. /// <example>
  401. /// <code>
  402. /// InputSystem.RegisterLayoutBuilder(
  403. /// () =>
  404. /// {
  405. /// var builder = new InputControlLayout.Builder()
  406. /// .WithType&lt;MyDevice&gt;();
  407. /// builder.AddControl("button1").WithLayout("Button");
  408. /// return builder.Build();
  409. /// }, "MyCustomLayout"
  410. /// }
  411. /// </code>
  412. /// </example>
  413. ///
  414. /// Layout builders can be used in combination with <see cref="onFindLayoutForDevice"/> to
  415. /// build layouts dynamically for devices as they are connected to the system.
  416. ///
  417. /// Be aware that the same builder <em>must</em> not build different layouts. Each
  418. /// layout registered in the system is considered to be immutable for as long as it
  419. /// is registered. So, if a layout builder is registered under the name "Custom", for
  420. /// example, then every time the builder is invoked, it must return the same identical
  421. /// <see cref="InputControlLayout"/>.
  422. /// </remarks>
  423. /// <seealso cref="InputControlLayout.Builder"/>
  424. /// <seealso cref="onFindLayoutForDevice"/>
  425. public static void RegisterLayoutBuilder(Func<InputControlLayout> buildMethod, string name,
  426. string baseLayout = null, InputDeviceMatcher? matches = null)
  427. {
  428. if (buildMethod == null)
  429. throw new ArgumentNullException(nameof(buildMethod));
  430. if (string.IsNullOrEmpty(name))
  431. throw new ArgumentNullException(nameof(name));
  432. s_Manager.RegisterControlLayoutBuilder(buildMethod, name, baseLayout: baseLayout);
  433. if (matches != null)
  434. s_Manager.RegisterControlLayoutMatcher(name, matches.Value);
  435. }
  436. /// <summary>
  437. /// Remove an already registered layout from the system.
  438. /// </summary>
  439. /// <param name="name">Name of the layout to remove. Note that layout names are case-insensitive.</param>
  440. /// <remarks>
  441. /// Note that removing a layout also removes all devices that directly or indirectly
  442. /// use the layout.
  443. ///
  444. /// This method can be used to remove both control or device layouts.
  445. /// </remarks>
  446. public static void RemoveLayout(string name)
  447. {
  448. s_Manager.RemoveControlLayout(name);
  449. }
  450. /// <summary>
  451. /// Try to match a description for an input device to a layout.
  452. /// </summary>
  453. /// <param name="deviceDescription">Description of an input device.</param>
  454. /// <returns>Name of the layout that has been matched to the given description or null if no
  455. /// matching layout was found.</returns>
  456. /// <remarks>
  457. /// This method performs the same matching process that is invoked if a device is reported
  458. /// by the Unity runtime or using <see cref="AddDevice(InputDeviceDescription)"/>. The result
  459. /// depends on the matches (<see cref="InputDeviceMatcher"/>) registered for the device
  460. /// layout in the system.
  461. ///
  462. /// <example>
  463. /// <code>
  464. /// var layoutName = InputSystem.TryFindMatchingLayout(
  465. /// new InputDeviceDescription
  466. /// {
  467. /// interface = "XInput",
  468. /// product = "Xbox Wired Controller",
  469. /// manufacturer = "Microsoft"
  470. /// }
  471. /// );
  472. /// </code>
  473. /// </example>
  474. /// </remarks>
  475. /// <seealso cref="RegisterLayoutMatcher{TDevice}"/>
  476. /// <seealso cref="RegisterLayoutMatcher(string,InputDeviceMatcher)"/>
  477. public static string TryFindMatchingLayout(InputDeviceDescription deviceDescription)
  478. {
  479. return s_Manager.TryFindMatchingControlLayout(ref deviceDescription);
  480. }
  481. /// <summary>
  482. /// Return a list with the names of all layouts that have been registered.
  483. /// </summary>
  484. /// <returns>A list of layout names.</returns>
  485. /// <seealso cref="LoadLayout"/>
  486. /// <seealso cref="ListLayoutsBasedOn"/>
  487. /// <seealso cref="RegisterLayout(System.Type,string,Nullable{InputDeviceMatcher})"/>
  488. public static IEnumerable<string> ListLayouts()
  489. {
  490. return s_Manager.ListControlLayouts();
  491. }
  492. /// <summary>
  493. /// List all the layouts that are based on the given layout.
  494. /// </summary>
  495. /// <param name="baseLayout">Name of a registered layout.</param>
  496. /// <exception cref="ArgumentNullException"><paramref name="baseLayout"/> is <c>null</c> or empty.</exception>
  497. /// <returns>The names of all registered layouts based on <paramref name="baseLayout"/>.</returns>
  498. /// <remarks>
  499. /// The list will not include layout overrides (see <see cref="RegisterLayoutOverride"/>).
  500. ///
  501. /// <example>
  502. /// <code>
  503. /// // List all gamepad layouts in the system.
  504. /// Debug.Log(string.Join("\n", InputSystem.ListLayoutsBasedOn("Gamepad"));
  505. /// </code>
  506. /// </example>
  507. /// </remarks>
  508. public static IEnumerable<string> ListLayoutsBasedOn(string baseLayout)
  509. {
  510. if (string.IsNullOrEmpty(baseLayout))
  511. throw new ArgumentNullException(nameof(baseLayout));
  512. return s_Manager.ListControlLayouts(basedOn: baseLayout);
  513. }
  514. /// <summary>
  515. /// Load a registered layout.
  516. /// </summary>
  517. /// <param name="name">Name of the layout to load. Note that layout names are case-insensitive.</param>
  518. /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception>
  519. /// <returns>The constructed layout instance or <c>null</c> if no layout of the given name could be found.</returns>
  520. /// <remarks>
  521. /// The result of this method is what's called a "fully merged" layout, i.e. a layout with
  522. /// the information of all the base layouts as well as from all overrides merged into it. See
  523. /// <see cref="InputControlLayout.MergeLayout"/> for details.
  524. ///
  525. /// What this means in practice is that all inherited controls and settings will be present
  526. /// on the layout.
  527. ///
  528. /// <example>
  529. /// // List all controls defined for gamepads.
  530. /// var gamepadLayout = InputSystem.LoadLayout("Gamepad");
  531. /// foreach (var control in gamepadLayout.controls)
  532. /// {
  533. /// // There may be control elements that are not introducing new controls but rather
  534. /// // change settings on controls added indirectly by other layouts referenced from
  535. /// // Gamepad. These are not adding new controls so we skip them here.
  536. /// if (control.isModifyingExistingControl)
  537. /// continue;
  538. ///
  539. /// Debug.Log($"Control: {control.name} ({control.layout])");
  540. /// }
  541. /// </example>
  542. ///
  543. /// However, note that controls which are added from other layouts referenced by the loaded layout
  544. /// will not necessarily be visible on it (they will only if referenced by a <see cref="InputControlLayout.ControlItem"/>
  545. /// where <see cref="InputControlLayout.ControlItem.isModifyingExistingControl"/> is <c>true</c>).
  546. /// For example, let's assume we have the following layout which adds a device with a single stick.
  547. ///
  548. /// <example>
  549. /// <code>
  550. /// InputSystem.RegisterLayout(@"
  551. /// {
  552. /// ""name"" : ""DeviceWithStick"",
  553. /// ""controls"" : [
  554. /// { ""name"" : ""stick"", ""layout"" : ""Stick"" }
  555. /// ]
  556. /// }
  557. /// ");
  558. /// </code>
  559. /// </example>
  560. ///
  561. /// If we load this layout, the <c>"stick"</c> control will be visible on the layout but the
  562. /// X and Y (as well as up/down/left/right) controls added by the <c>"Stick"</c> layout will
  563. /// not be.
  564. /// </remarks>
  565. /// <seealso cref="RegisterLayout(Type,string,Nullable{InputDeviceMatcher})"/>
  566. public static InputControlLayout LoadLayout(string name)
  567. {
  568. if (string.IsNullOrEmpty(name))
  569. throw new ArgumentNullException(nameof(name));
  570. ////FIXME: this will intern the name even if the operation fails
  571. return s_Manager.TryLoadControlLayout(new InternedString(name));
  572. }
  573. /// <summary>
  574. /// Load the layout registered for the given type.
  575. /// </summary>
  576. /// <typeparam name="TControl">An InputControl type.</typeparam>
  577. /// <returns>The layout registered for <typeparamref name="TControl"/> or <c>null</c> if no
  578. /// such layout exists.</returns>
  579. /// <remarks>
  580. /// This method is equivalent to calling <see cref="LoadLayout(string)"/> with the name
  581. /// of the layout under which <typeparamref name="TControl"/> has been registered.
  582. ///
  583. /// <example>
  584. /// <code>
  585. /// // Load the InputControlLayout generated from StickControl.
  586. /// var stickLayout = InputSystem.LoadLayout&lt;StickControl&gt;();
  587. /// </code>
  588. /// </example>
  589. /// </remarks>
  590. /// <seealso cref="LoadLayout(string)"/>
  591. public static InputControlLayout LoadLayout<TControl>()
  592. where TControl : InputControl
  593. {
  594. return s_Manager.TryLoadControlLayout(typeof(TControl));
  595. }
  596. /// <summary>
  597. /// Return the name of the layout that the layout registered as <paramref name="layoutName"/>
  598. /// is based on.
  599. /// </summary>
  600. /// <param name="layoutName">Name of a layout as registered with a method such as <see
  601. /// cref="RegisterLayout{T}(string,InputDeviceMatcher?)"/>. Case-insensitive.</param>
  602. /// <returns>Name of the immediate parent layout of <paramref name="layoutName"/> or <c>null</c> if no layout
  603. /// with the given name is registered or if it is not based on another layout or if it is a layout override.</returns>
  604. /// <exception cref="ArgumentNullException"><paramref name="layoutName"/> is <c>null</c> or empty.</exception>
  605. /// <remarks>
  606. /// This method does not work for layout overrides (which can be based on multiple base layouts). To find
  607. /// out which layouts a specific override registered with <see cref="RegisterLayoutOverride"/> is based on,
  608. /// load the layout with <see cref="LoadLayout"/> and inspect <see cref="InputControlLayout.baseLayouts"/>.
  609. /// This method will return <c>null</c> when <paramref name="layoutName"/> is the name of a layout override.
  610. ///
  611. /// One advantage of this method over calling <see cref="LoadLayout"/> and looking at <see cref="InputControlLayout.baseLayouts"/>
  612. /// is that this method does not have to actually load the layout but instead only performs a simple lookup.
  613. ///
  614. /// <example>
  615. /// <code>
  616. /// // Prints "Pointer".
  617. /// Debug.Log(InputSystem.GetNameOfBaseLayout("Mouse"));
  618. ///
  619. /// // Also works for control layouts. Prints "Axis".
  620. /// Debug.Log(InputSystem.GetNameOfBaseLayout("Button"));
  621. /// </code>
  622. /// </example>
  623. /// </remarks>
  624. /// <seealso cref="InputControlLayout.baseLayouts"/>
  625. public static string GetNameOfBaseLayout(string layoutName)
  626. {
  627. if (string.IsNullOrEmpty(layoutName))
  628. throw new ArgumentNullException(nameof(layoutName));
  629. var internedLayoutName = new InternedString(layoutName);
  630. if (InputControlLayout.s_Layouts.baseLayoutTable.TryGetValue(internedLayoutName, out var result))
  631. return result;
  632. return null;
  633. }
  634. /// <summary>
  635. /// Check whether the first layout is based on the second.
  636. /// </summary>
  637. /// <param name="firstLayoutName">Name of a registered <see cref="InputControlLayout"/>.</param>
  638. /// <param name="secondLayoutName">Name of a registered <see cref="InputControlLayout"/>.</param>
  639. /// <returns>True if <paramref name="firstLayoutName"/> is based on <paramref name="secondLayoutName"/>.</returns>
  640. /// <exception cref="ArgumentNullException"><paramref name="firstLayoutName"/> is <c>null</c> or empty -or-
  641. /// <paramref name="secondLayoutName"/> is <c>null</c> or empty.</exception>
  642. /// <remarks>
  643. /// This is
  644. /// <example>
  645. /// </example>
  646. /// </remarks>
  647. public static bool IsFirstLayoutBasedOnSecond(string firstLayoutName, string secondLayoutName)
  648. {
  649. if (string.IsNullOrEmpty(firstLayoutName))
  650. throw new ArgumentNullException(nameof(firstLayoutName));
  651. if (string.IsNullOrEmpty(secondLayoutName))
  652. throw new ArgumentNullException(nameof(secondLayoutName));
  653. var internedFirstName = new InternedString(firstLayoutName);
  654. var internedSecondName = new InternedString(secondLayoutName);
  655. if (internedFirstName == internedSecondName)
  656. return true;
  657. return InputControlLayout.s_Layouts.IsBasedOn(internedSecondName, internedFirstName);
  658. }
  659. #endregion
  660. #region Processors
  661. /// <summary>
  662. /// Register an <see cref="InputProcessor{TValue}"/> with the system.
  663. /// </summary>
  664. /// <param name="type">Type that implements <see cref="InputProcessor"/>.</param>
  665. /// <param name="name">Name to use for the processor. If <c>null</c> or empty, name will be taken from the short name
  666. /// of <paramref name="type"/> (if it ends in "Processor", that suffix will be clipped from the name). Names
  667. /// are case-insensitive.</param>
  668. /// <remarks>
  669. /// Processors are used by both bindings (see <see cref="InputBinding"/>) and by controls
  670. /// (see <see cref="InputControl"/>) to post-process input values as they are being requested
  671. /// from calls such as <see cref="InputAction.ReadValue{TValue}"/> or <see
  672. /// cref="InputControl{T}.ReadValue"/>.
  673. ///
  674. /// <example>
  675. /// <code>
  676. /// // Let's say that we want to define a processor that adds some random jitter to its input.
  677. /// // We have to pick a value type to operate on if we want to derive from InputProcessor&lt;T&gt;
  678. /// // so we go with float here.
  679. /// //
  680. /// // Also, as we will need to place our call to RegisterProcessor somewhere, we add attributes
  681. /// // to hook into Unity's initialization. This works differently in the editor and in the player,
  682. /// // so we use both [InitializeOnLoad] and [RuntimeInitializeOnLoadMethod].
  683. /// #if UNITY_EDITOR
  684. /// [InitializeOnLoad]
  685. /// #endif
  686. /// public class JitterProcessor : InputProcessor&lt;float&gt;
  687. /// {
  688. /// // Add a parameter that defines the amount of jitter we apply.
  689. /// // This will be editable in the Unity editor UI and can be set
  690. /// // programmatically in code. For example:
  691. /// //
  692. /// // myAction.AddBinding("&lt;Gamepad&gt;/rightTrigger",
  693. /// // processors: "jitter(amount=0.1)");
  694. /// //
  695. /// [Tooltip("Amount of jitter to apply. Will add a random value in the range [-amount..amount] "
  696. /// + "to each input value.)]
  697. /// public float amount;
  698. ///
  699. /// // Process is called when an input value is read from a control. This is
  700. /// // where we perform our jitter.
  701. /// public override float Process(float value, InputControl control)
  702. /// {
  703. /// return float + Random.Range(-amount, amount);
  704. /// }
  705. ///
  706. /// // [InitializeOnLoad] will call the static class constructor which
  707. /// // we use to call Register.
  708. /// #if UNITY_EDITOR
  709. /// static JitterProcessor()
  710. /// {
  711. /// Register();
  712. /// }
  713. /// #endif
  714. ///
  715. /// // [RuntimeInitializeOnLoadMethod] will make sure that Register gets called
  716. /// // in the player on startup.
  717. /// // NOTE: This will also get called when going into play mode in the editor. In that
  718. /// // case we get two calls to Register instead of one. We don't bother with that
  719. /// // here. Calling RegisterProcessor twice here doesn't do any harm.
  720. /// [RuntimeInitializeOnLoadMethod]
  721. /// static void Register()
  722. /// {
  723. /// // We don't supply a name here. The input system will take "JitterProcessor"
  724. /// // and automatically snip off the "Processor" suffix thus leaving us with
  725. /// // a name of "Jitter" (all this is case-insensitive).
  726. /// InputSystem.RegisterProcessor&lt;JitterProcessor&gt;();
  727. /// }
  728. /// }
  729. ///
  730. /// // It doesn't really make sense in our case as the default parameter editor is just
  731. /// // fine (it will pick up the tooltip we defined above) but let's say we want to replace
  732. /// // the default float edit field we get on the "amount" parameter with a slider. We can
  733. /// // do so by defining a custom parameter editor.
  734. /// //
  735. /// // NOTE: We don't need to have a registration call here. The input system will automatically
  736. /// // find our parameter editor based on the JitterProcessor type parameter we give to
  737. /// // InputParameterEditor&lt;T&gt;.
  738. /// #if UNITY_EDITOR
  739. /// public class JitterProcessorEditor : InputParameterEditor&lt;JitterProcessor&gt;
  740. /// {
  741. /// public override void OnGUI()
  742. /// {
  743. /// target.amount = EditorGUILayout.Slider(m_AmountLabel, target.amount, 0, 0.25f);
  744. /// }
  745. ///
  746. /// private GUIContent m_AmountLabel = new GUIContent("Amount",
  747. /// "Amount of jitter to apply. Will add a random value in the range [-amount..amount] "
  748. /// + "to each input value.);
  749. /// }
  750. /// #endif
  751. /// </code>
  752. /// </example>
  753. ///
  754. /// Note that it is allowed to register the same processor type multiple types with
  755. /// different names. When doing so, the first registration is considered as the "proper"
  756. /// name for the processor and all subsequent registrations will be considered aliases.
  757. ///
  758. /// See the <a href="../manual/Processors.html">manual</a> for more details.
  759. /// </remarks>
  760. /// <seealso cref="InputProcessor{T}"/>
  761. /// <seealso cref="InputBinding.processors"/>
  762. /// <seealso cref="InputAction.processors"/>
  763. /// <seealso cref="InputControlLayout.ControlItem.processors"/>
  764. /// <seealso cref="InputParameterEditor{TObject}"/>
  765. public static void RegisterProcessor(Type type, string name = null)
  766. {
  767. if (type == null)
  768. throw new ArgumentNullException(nameof(type));
  769. if (string.IsNullOrEmpty(name))
  770. {
  771. name = type.Name;
  772. if (name.EndsWith("Processor"))
  773. name = name.Substring(0, name.Length - "Processor".Length);
  774. }
  775. s_Manager.processors.AddTypeRegistration(name, type);
  776. }
  777. /// <summary>
  778. /// Register an <see cref="InputProcessor{TValue}"/> with the system.
  779. /// </summary>
  780. /// <typeparam name="T">Type that implements <see cref="InputProcessor"/>.</typeparam>
  781. /// <param name="name">Name to use for the processor. If <c>null</c> or empty, name will be taken from the short name
  782. /// of <typeparamref name="T"/> (if it ends in "Processor", that suffix will be clipped from the name). Names
  783. /// are case-insensitive.</param>
  784. /// <remarks>
  785. /// Processors are used by both bindings (see <see cref="InputBinding"/>) and by controls
  786. /// (see <see cref="InputControl"/>) to post-process input values as they are being requested
  787. /// from calls such as <see cref="InputAction.ReadValue{TValue}"/> or <see
  788. /// cref="InputControl{T}.ReadValue"/>.
  789. ///
  790. /// <example>
  791. /// <code>
  792. /// // Let's say that we want to define a processor that adds some random jitter to its input.
  793. /// // We have to pick a value type to operate on if we want to derive from InputProcessor&lt;T&gt;
  794. /// // so we go with float here.
  795. /// //
  796. /// // Also, as we will need to place our call to RegisterProcessor somewhere, we add attributes
  797. /// // to hook into Unity's initialization. This works differently in the editor and in the player,
  798. /// // so we use both [InitializeOnLoad] and [RuntimeInitializeOnLoadMethod].
  799. /// #if UNITY_EDITOR
  800. /// [InitializeOnLoad]
  801. /// #endif
  802. /// public class JitterProcessor : InputProcessor&lt;float&gt;
  803. /// {
  804. /// // Add a parameter that defines the amount of jitter we apply.
  805. /// // This will be editable in the Unity editor UI and can be set
  806. /// // programmatically in code. For example:
  807. /// //
  808. /// // myAction.AddBinding("&lt;Gamepad&gt;/rightTrigger",
  809. /// // processors: "jitter(amount=0.1)");
  810. /// //
  811. /// [Tooltip("Amount of jitter to apply. Will add a random value in the range [-amount..amount] "
  812. /// + "to each input value.)]
  813. /// public float amount;
  814. ///
  815. /// // Process is called when an input value is read from a control. This is
  816. /// // where we perform our jitter.
  817. /// public override float Process(float value, InputControl control)
  818. /// {
  819. /// return float + Random.Range(-amount, amount);
  820. /// }
  821. ///
  822. /// // [InitializeOnLoad] will call the static class constructor which
  823. /// // we use to call Register.
  824. /// #if UNITY_EDITOR
  825. /// static JitterProcessor()
  826. /// {
  827. /// Register();
  828. /// }
  829. /// #endif
  830. ///
  831. /// // [RuntimeInitializeOnLoadMethod] will make sure that Register gets called
  832. /// // in the player on startup.
  833. /// // NOTE: This will also get called when going into play mode in the editor. In that
  834. /// // case we get two calls to Register instead of one. We don't bother with that
  835. /// // here. Calling RegisterProcessor twice here doesn't do any harm.
  836. /// [RuntimeInitializeOnLoadMethod]
  837. /// static void Register()
  838. /// {
  839. /// // We don't supply a name here. The input system will take "JitterProcessor"
  840. /// // and automatically snip off the "Processor" suffix thus leaving us with
  841. /// // a name of "Jitter" (all this is case-insensitive).
  842. /// InputSystem.RegisterProcessor&lt;JitterProcessor&gt;();
  843. /// }
  844. /// }
  845. ///
  846. /// // It doesn't really make sense in our case as the default parameter editor is just
  847. /// // fine (it will pick up the tooltip we defined above) but let's say we want to replace
  848. /// // the default float edit field we get on the "amount" parameter with a slider. We can
  849. /// // do so by defining a custom parameter editor.
  850. /// //
  851. /// // NOTE: We don't need to have a registration call here. The input system will automatically
  852. /// // find our parameter editor based on the JitterProcessor type parameter we give to
  853. /// // InputParameterEditor&lt;T&gt;.
  854. /// #if UNITY_EDITOR
  855. /// public class JitterProcessorEditor : InputParameterEditor&lt;JitterProcessor&gt;
  856. /// {
  857. /// public override void OnGUI()
  858. /// {
  859. /// target.amount = EditorGUILayout.Slider(m_AmountLabel, target.amount, 0, 0.25f);
  860. /// }
  861. ///
  862. /// private GUIContent m_AmountLabel = new GUIContent("Amount",
  863. /// "Amount of jitter to apply. Will add a random value in the range [-amount..amount] "
  864. /// + "to each input value.);
  865. /// }
  866. /// #endif
  867. /// </code>
  868. /// </example>
  869. ///
  870. /// Note that it is allowed to register the same processor type multiple types with
  871. /// different names. When doing so, the first registration is considered as the "proper"
  872. /// name for the processor and all subsequent registrations will be considered aliases.
  873. ///
  874. /// See the <a href="../manual/Processors.html">manual</a> for more details.
  875. /// </remarks>
  876. /// <seealso cref="InputProcessor{T}"/>
  877. /// <seealso cref="InputBinding.processors"/>
  878. /// <seealso cref="InputAction.processors"/>
  879. /// <seealso cref="InputControlLayout.ControlItem.processors"/>
  880. /// <seealso cref="InputParameterEditor{TObject}"/>
  881. public static void RegisterProcessor<T>(string name = null)
  882. {
  883. RegisterProcessor(typeof(T), name);
  884. }
  885. /// <summary>
  886. /// Return the processor type registered under the given name. If no such processor
  887. /// has been registered, return <c>null</c>.
  888. /// </summary>
  889. /// <param name="name">Name of processor. Case-insensitive.</param>
  890. /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception>
  891. /// <returns>The given processor type or <c>null</c> if not found.</returns>
  892. /// <seealso cref="RegisterProcessor{T}"/>
  893. public static Type TryGetProcessor(string name)
  894. {
  895. if (string.IsNullOrEmpty(name))
  896. throw new ArgumentNullException(nameof(name));
  897. return s_Manager.processors.LookupTypeRegistration(name);
  898. }
  899. /// <summary>
  900. /// List the names of all processors have been registered.
  901. /// </summary>
  902. /// <returns>List of registered processors.</returns>
  903. /// <remarks>
  904. /// Note that the result will include both "proper" names and aliases registered
  905. /// for processors. If, for example, a given type <c>JitterProcessor</c> has been registered
  906. /// under both "Jitter" and "Randomize", it will appear in the list with both those names.
  907. /// </remarks>
  908. /// <seealso cref="TryGetProcessor"/>
  909. /// <seealso cref="RegisterProcessor{T}"/>
  910. public static IEnumerable<string> ListProcessors()
  911. {
  912. return s_Manager.processors.names;
  913. }
  914. #endregion
  915. #region Devices
  916. /// <summary>
  917. /// The list of currently connected devices.
  918. /// </summary>
  919. /// <value>Currently connected devices.</value>
  920. /// <remarks>
  921. /// Note that accessing this property does not allocate. It gives read-only access
  922. /// directly to the system's internal array of devices.
  923. ///
  924. /// The value returned by this property should not be held on to. When the device
  925. /// setup in the system changes, any value previously returned by this property
  926. /// may become invalid. Query the property directly whenever you need it.
  927. /// </remarks>
  928. /// <seealso cref="AddDevice{TDevice}"/>
  929. /// <seealso cref="RemoveDevice"/>
  930. public static ReadOnlyArray<InputDevice> devices => s_Manager.devices;
  931. /// <summary>
  932. /// Devices that have been disconnected but are retained by the input system in case
  933. /// they are plugged back in.
  934. /// </summary>
  935. /// <value>Devices that have been retained by the input system in case they are plugged
  936. /// back in.</value>
  937. /// <remarks>
  938. /// During gameplay it is undesirable to have the system allocate and release managed memory
  939. /// as devices are unplugged and plugged back in as it would ultimately lead to GC spikes
  940. /// during gameplay. To avoid that, input devices that have been reported by the <see cref="IInputRuntime">
  941. /// runtime</see> and are removed through <see cref="DeviceRemoveEvent">events</see> are retained
  942. /// by the system and then reused if the device is plugged back in.
  943. ///
  944. /// Note that the devices moved to disconnected status will still see a <see cref="InputDeviceChange.Removed"/>
  945. /// notification and a <see cref="InputDeviceChange.Added"/> notification when plugged back in.
  946. ///
  947. /// To determine if a newly discovered device is one we have seen before, the system uses a
  948. /// simple approach of comparing <see cref="InputDeviceDescription">device descriptions</see>.
  949. /// Note that there can be errors and a device may be incorrectly classified as <see cref="InputDeviceChange.Reconnected"/>
  950. /// when in fact it is a different device from before. The problem is that based on information
  951. /// made available by platforms, it can be inherently difficult to determine whether a device is
  952. /// indeed the very same one.
  953. ///
  954. /// For example, it is often not possible to determine with 100% certainty whether an identical looking device
  955. /// to one we've previously seen on a different USB port is indeed the very same device. OSs will usually
  956. /// reattach a USB device to its previous instance if it is plugged into the same USB port but create a
  957. /// new instance of the same device is plugged into a different port.
  958. ///
  959. /// For devices that do relay their <see cref="InputDeviceDescription.serial">serials</see> the matching
  960. /// is reliable.
  961. ///
  962. /// The list can be purged by calling <see cref="FlushDisconnectedDevices"/>. Doing so, will release
  963. /// all reference we hold to the devices or any controls inside of them and allow the devices to be
  964. /// reclaimed by the garbage collector.
  965. ///
  966. /// Note that if you call <see cref="RemoveDevice"/> explicitly, the given device is not retained
  967. /// by the input system and will not appear on this list.
  968. ///
  969. /// Also note that devices on this list will be lost when domain reloads happen in the editor (i.e. on
  970. /// script recompilation and when entering play mode).
  971. /// </remarks>
  972. /// <seealso cref="FlushDisconnectedDevices"/>
  973. public static ReadOnlyArray<InputDevice> disconnectedDevices =>
  974. new ReadOnlyArray<InputDevice>(s_Manager.m_DisconnectedDevices, 0,
  975. s_Manager.m_DisconnectedDevicesCount);
  976. /// <summary>
  977. /// Event that is signalled when the device setup in the system changes.
  978. /// </summary>
  979. /// <value>Callback when device setup ni system changes.</value>
  980. /// <remarks>
  981. /// This can be used to detect when devices are added or removed as well as
  982. /// detecting when existing devices change their configuration.
  983. ///
  984. /// <example>
  985. /// <code>
  986. /// InputSystem.onDeviceChange +=
  987. /// (device, change) =>
  988. /// {
  989. /// switch (change)
  990. /// {
  991. /// case InputDeviceChange.Added:
  992. /// Debug.Log("Device added: " + device);
  993. /// break;
  994. /// case InputDeviceChange.Removed:
  995. /// Debug.Log("Device removed: " + device);
  996. /// break;
  997. /// case InputDeviceChange.ConfigurationChanged:
  998. /// Debug.Log("Device configuration changed: " + device);
  999. /// break;
  1000. /// }
  1001. /// };
  1002. /// </code>
  1003. /// </example>
  1004. /// </remarks>
  1005. /// <exception cref="ArgumentNullException">Delegate reference is <c>null</c>.</exception>
  1006. /// <seealso cref="devices"/>
  1007. /// <seealso cref="AddDevice{TDevice}"/>
  1008. /// <seealso cref="RemoveDevice"/>
  1009. public static event Action<InputDevice, InputDeviceChange> onDeviceChange
  1010. {
  1011. add
  1012. {
  1013. if (value == null)
  1014. throw new ArgumentNullException(nameof(value));
  1015. lock (s_Manager)
  1016. s_Manager.onDeviceChange += value;
  1017. }
  1018. remove
  1019. {
  1020. if (value == null)
  1021. throw new ArgumentNullException(nameof(value));
  1022. lock (s_Manager)
  1023. s_Manager.onDeviceChange -= value;
  1024. }
  1025. }
  1026. ////REVIEW: this one isn't really well-designed and the means of intercepting communication
  1027. //// with the backend should be revisited >1.0
  1028. /// <summary>
  1029. /// Event that is signalled when an <see cref="InputDeviceCommand"/> is sent to
  1030. /// an <see cref="InputDevice"/>.
  1031. /// </summary>
  1032. /// <value>Event that gets signalled on <see cref="InputDeviceCommand"/>s.</value>
  1033. /// <remarks>
  1034. /// This can be used to intercept commands and optionally handle them without them reaching
  1035. /// the <see cref="IInputRuntime"/>.
  1036. ///
  1037. /// The first delegate in the list that returns a result other than <c>null</c> is considered
  1038. /// to have handled the command. If a command is handled by a delegate in the list, it will
  1039. /// not be sent on to the runtime.
  1040. /// </remarks>
  1041. /// <exception cref="ArgumentNullException">Delegate reference is <c>null</c>.</exception>
  1042. /// <seealso cref="InputDevice.ExecuteCommand{TCommand}"/>
  1043. /// <seealso cref="IInputRuntime.DeviceCommand"/>
  1044. public static event InputDeviceCommandDelegate onDeviceCommand
  1045. {
  1046. add
  1047. {
  1048. if (value == null)
  1049. throw new ArgumentNullException(nameof(value));
  1050. lock (s_Manager)
  1051. s_Manager.onDeviceCommand += value;
  1052. }
  1053. remove
  1054. {
  1055. if (value == null)
  1056. throw new ArgumentNullException(nameof(value));
  1057. lock (s_Manager)
  1058. s_Manager.onDeviceCommand -= value;
  1059. }
  1060. }
  1061. /// <summary>
  1062. /// Event that is signalled when the system is trying to match a layout to
  1063. /// a device it has discovered.
  1064. /// </summary>
  1065. /// <remarks>
  1066. /// This event allows customizing the layout discovery process and to generate
  1067. /// layouts on the fly, if need be. When a device is reported from the Unity
  1068. /// runtime or through <see cref="AddDevice(InputDeviceDescription)"/>, it is
  1069. /// reported in the form of an <see cref="InputDeviceDescription"/>. The system
  1070. /// will take that description and run it through all the <see cref="InputDeviceMatcher"/>s
  1071. /// that have been registered for layouts (<see cref="RegisterLayoutMatcher{TDevice}"/>).
  1072. /// Based on that, it will come up with either no matching layout or with a single
  1073. /// layout that has the highest matching score according to <see
  1074. /// cref="InputDeviceMatcher.MatchPercentage"/> (or, in case multiple layouts have
  1075. /// the same score, the first one to achieve that score -- which is quasi-non-deterministic).
  1076. ///
  1077. /// It will then take this layout name (which, again, may be empty) and invoke this
  1078. /// event here passing it not only the layout name but also information such as the
  1079. /// <see cref="InputDeviceDescription"/> for the device. Each of the callbacks hooked
  1080. /// into the event will be run in turn. The <em>first</em> one to return a string
  1081. /// that is not <c>null</c> and not empty will cause a switch from the layout the
  1082. /// system has chosen to the layout that has been returned by the callback. The remaining
  1083. /// layouts after that will then be invoked with that newly selected name but will not
  1084. /// be able to change the name anymore.
  1085. ///
  1086. /// If none of the callbacks returns a string that is not <c>null</c> or empty,
  1087. /// the system will stick with the layout that it had initially selected.
  1088. ///
  1089. /// Once all callbacks have been run, the system will either have a final layout
  1090. /// name or not. If it does, a device is created using that layout. If it does not,
  1091. /// no device is created.
  1092. ///
  1093. /// One thing this allows is to generate callbacks on the fly. Let's say that if
  1094. /// an input device is reported with the "Custom" interface, we want to generate
  1095. /// a layout for it on the fly. For details about how to build layouts dynamically
  1096. /// from code, see <see cref="InputControlLayout.Builder"/> and <see cref="RegisterLayoutBuilder"/>.
  1097. ///
  1098. /// <example>
  1099. /// <code>
  1100. /// InputSystem.onFindLayoutForDevice +=
  1101. /// (deviceId, description, matchedLayout, runtime) =>
  1102. /// {
  1103. /// // If the system does have a matching layout, we do nothing.
  1104. /// // This could be the case, for example, if we already generated
  1105. /// // a layout for the device or if someone explicitly registered
  1106. /// // a layout.
  1107. /// if (!string.IsNullOrEmpty(matchedLayout))
  1108. /// return null; // Tell system we did nothing.
  1109. ///
  1110. /// // See if the reported device uses the "Custom" interface. We
  1111. /// // are only interested in those.
  1112. /// if (description.interfaceName != "Custom")
  1113. /// return null; // Tell system we did nothing.
  1114. ///
  1115. /// // So now we know that we want to build a layout on the fly
  1116. /// // for this device. What we do is to register what's called a
  1117. /// // layout builder. These can use C# code to build an InputControlLayout
  1118. /// // on the fly.
  1119. ///
  1120. /// // First we need to come up with a sufficiently unique name for the layout
  1121. /// // under which we register the builder. This will usually involve some
  1122. /// // information from the InputDeviceDescription we have been supplied with.
  1123. /// // Let's say we can sufficiently tell devices on our interface apart by
  1124. /// // product name alone. So we just do this:
  1125. /// var layoutName = "Custom" + description.product;
  1126. ///
  1127. /// // We also need an InputDeviceMatcher that in the future will automatically
  1128. /// // select our newly registered layout whenever a new device of the same type
  1129. /// // is connected. We can get one simply like so:
  1130. /// var matcher = InputDeviceMatcher.FromDescription(description);
  1131. ///
  1132. /// // With these pieces in place, we can register our builder which
  1133. /// // mainly consists of a delegate that will get invoked when an instance
  1134. /// // of InputControlLayout is needed for the layout.
  1135. /// InputSystem.RegisterLayoutBuilder(
  1136. /// () =>
  1137. /// {
  1138. /// // Here is where we do the actual building. In practice,
  1139. /// // this would probably look at the 'capabilities' property
  1140. /// // of the InputDeviceDescription we got and create a tailor-made
  1141. /// // layout. But what you put in the layout here really depends on
  1142. /// // the specific use case you have.
  1143. /// //
  1144. /// // We just add some preset things here which should still sufficiently
  1145. /// // serve as a demonstration.
  1146. /// //
  1147. /// // Note that we can base our layout here on whatever other layout
  1148. /// // in the system. We could extend Gamepad, for example. If we don't
  1149. /// // choose a base layout, the system automatically implies InputDevice.
  1150. ///
  1151. /// var builder = new InputControlLayout.Builder()
  1152. /// .WithDisplayName(description.product);
  1153. ///
  1154. /// // Add controls.
  1155. /// builder.AddControl("stick")
  1156. /// .WithLayout("Stick");
  1157. ///
  1158. /// return builder.Build();
  1159. /// },
  1160. /// layoutName,
  1161. /// matches: matcher);
  1162. ///
  1163. /// // So, we want the system to use our layout for the device that has just
  1164. /// // been connected. We return it from this callback to do that.
  1165. /// return layoutName;
  1166. /// };
  1167. /// </code>
  1168. /// </example>
  1169. ///
  1170. /// Note that it may appear like one could simply use <see cref="RegisterLayoutBuilder"/>
  1171. /// like below instead of going through <c>onFindLayoutForDevice</c>.
  1172. ///
  1173. /// <example>
  1174. /// <code>
  1175. /// InputSystem.RegisterLayoutBuilder(
  1176. /// () =>
  1177. /// {
  1178. /// // Layout building code from above...
  1179. /// },
  1180. /// "CustomLayout",
  1181. /// matches: new InputDeviceMatcher().WithInterface("Custom"));
  1182. /// </code>
  1183. /// </example>
  1184. ///
  1185. /// However, the difference here is that all devices using the "Custom" interface will
  1186. /// end up with the same single layout -- which has to be identical. By hooking into
  1187. /// <c>onFindLayoutForDevice</c>, it is possible to register a new layout for every new
  1188. /// type of device that is discovered and thus build a multitude of different layouts.
  1189. ///
  1190. /// It is best to register for this callback during startup. One way to do it is to
  1191. /// use <c>InitializeOnLoadAttribute</c> and <c>RuntimeInitializeOnLoadMethod</c>.
  1192. /// </remarks>
  1193. /// <seealso cref="RegisterLayoutBuilder"/>
  1194. /// <seealso cref="InputControlLayout"/>
  1195. public static event InputDeviceFindControlLayoutDelegate onFindLayoutForDevice
  1196. {
  1197. add
  1198. {
  1199. lock (s_Manager)
  1200. s_Manager.onFindControlLayoutForDevice += value;
  1201. }
  1202. remove
  1203. {
  1204. lock (s_Manager)
  1205. s_Manager.onFindControlLayoutForDevice -= value;
  1206. }
  1207. }
  1208. ////REVIEW: should this be disambiguated more to separate it more from sensor sampling frequency?
  1209. ////REVIEW: this should probably be exposed as an input setting
  1210. /// <summary>
  1211. /// Frequency at which devices that need polling are being queried in the background.
  1212. /// </summary>
  1213. /// <value>Polled device sampling frequency in Hertz.</value>
  1214. /// <remarks>
  1215. /// Input data is gathered from platform APIs either as events or polled periodically.
  1216. ///
  1217. /// In the former case, where we get input as events, the platform is responsible for monitoring
  1218. /// input devices and sending their state changes which the Unity runtime receives
  1219. /// and queues as <see cref="InputEvent"/>s. This form of input collection usually happens on a
  1220. /// system-specific thread (which may be Unity's main thread) as part of how the Unity player
  1221. /// loop operates. In most cases, this means that this form of input will invariably get picked up
  1222. /// once per frame.
  1223. ///
  1224. /// In the latter case, where input has to be explicitly polled from the system, the Unity runtime
  1225. /// will periodically sample the state of input devices and send it off as input events. Wherever
  1226. /// possible, this happens in the background at a fixed frequency on a dedicated thread. The
  1227. /// <c>pollingFrequency</c> property controls the rate at which this sampling happens.
  1228. ///
  1229. /// The unit is Hertz. A value of 120, for example, means that devices are sampled 120 times
  1230. /// per second.
  1231. ///
  1232. /// The default polling frequency is 60 Hz.
  1233. ///
  1234. /// For devices that are polled, the frequency setting will directly translate to changes in the
  1235. /// <see cref="InputEvent.time"/> patterns. At 60 Hz, for example, timestamps for a specific,
  1236. /// polled device will be spaced at roughly 1/60th of a second apart.
  1237. ///
  1238. /// Note that it depends on the platform which devices are polled (if any). On Win32, for example,
  1239. /// only XInput gamepads are polled.
  1240. ///
  1241. /// Also note that the polling frequency applies to all devices that are polled. It is not possible
  1242. /// to set polling frequency on a per-device basis.
  1243. /// </remarks>
  1244. public static float pollingFrequency
  1245. {
  1246. get => s_Manager.pollingFrequency;
  1247. set => s_Manager.pollingFrequency = value;
  1248. }
  1249. /// <summary>
  1250. /// Add a new device by instantiating the given device layout.
  1251. /// </summary>
  1252. /// <param name="layout">Name of the layout to instantiate. Must be a device layout. Note that
  1253. /// layout names are case-insensitive.</param>
  1254. /// <param name="name">Name to assign to the device. If null, the layout's display name (<see
  1255. /// cref="InputControlLayout.displayName"/> is used instead. Note that device names are made
  1256. /// unique automatically by the system by appending numbers to them (e.g. "gamepad", "gamepad1",
  1257. /// "gamepad2", etc.).</param>
  1258. /// <param name="variants">Semicolon-separated list of layout variants to use for the device.</param>
  1259. /// <exception cref="ArgumentNullException"><paramref name="layout"/> is <c>null</c> or empty.</exception>
  1260. /// <returns>The newly created input device.</returns>
  1261. /// <remarks>
  1262. /// The device will be added to the <see cref="devices"/> list and a notification on
  1263. /// <see cref="onDeviceChange"/> will be triggered.
  1264. ///
  1265. /// Note that adding a device to the system will allocate and also create garbage on the GC heap.
  1266. ///
  1267. /// <example>
  1268. /// <code>
  1269. /// // This is one way to instantiate the "Gamepad" layout.
  1270. /// InputSystem.AddDevice("Gamepad");
  1271. ///
  1272. /// // In this case, because the "Gamepad" layout is based on the Gamepad
  1273. /// // class, we can also do this instead:
  1274. /// InputSystem.AddDevice&lt;Gamepad&gt;();
  1275. /// </code>
  1276. /// </example>
  1277. /// </remarks>
  1278. /// <seealso cref="AddDevice{T}"/>
  1279. /// <seealso cref="RemoveDevice"/>
  1280. /// <seealso cref="onDeviceChange"/>
  1281. /// <seealso cref="InputDeviceChange.Added"/>
  1282. /// <seealso cref="devices"/>
  1283. /// <seealso cref="RegisterLayout(Type,string,Nullable{InputDeviceMatcher})"/>
  1284. public static InputDevice AddDevice(string layout, string name = null, string variants = null)
  1285. {
  1286. if (string.IsNullOrEmpty(layout))
  1287. throw new ArgumentNullException(nameof(layout));
  1288. return s_Manager.AddDevice(layout, name, new InternedString(variants));
  1289. }
  1290. /// <summary>
  1291. /// Add a new device by instantiating the layout registered for type <typeparamref name="TDevice"/>.
  1292. /// </summary>
  1293. /// <param name="name">Name to assign to the device. If null, the layout's display name (<see
  1294. /// cref="InputControlLayout.displayName"/> is used instead. Note that device names are made
  1295. /// unique automatically by the system by appending numbers to them (e.g. "gamepad", "gamepad1",
  1296. /// "gamepad2", etc.).</param>
  1297. /// <typeparam name="TDevice">Type of device to add.</typeparam>
  1298. /// <returns>The newly added device.</returns>
  1299. /// <exception cref="InvalidOperationException">Instantiating the layout for <typeparamref name="TDevice"/>
  1300. /// did not produce a device of type <typeparamref name="TDevice"/>.</exception>
  1301. /// <remarks>
  1302. /// The device will be added to the <see cref="devices"/> list and a notification on
  1303. /// <see cref="onDeviceChange"/> will be triggered.
  1304. ///
  1305. /// Note that adding a device to the system will allocate and also create garbage on the GC heap.
  1306. ///
  1307. /// <example>
  1308. /// <code>
  1309. /// // Add a gamepad.
  1310. /// InputSystem.AddDevice&lt;Gamepad&gt;();
  1311. /// </code>
  1312. /// </example>
  1313. /// </remarks>
  1314. /// <seealso cref="RemoveDevice"/>
  1315. /// <seealso cref="onDeviceChange"/>
  1316. /// <seealso cref="InputDeviceChange.Added"/>
  1317. /// <seealso cref="devices"/>
  1318. public static TDevice AddDevice<TDevice>(string name = null)
  1319. where TDevice : InputDevice
  1320. {
  1321. var device = s_Manager.AddDevice(typeof(TDevice), name);
  1322. if (!(device is TDevice deviceOfType))
  1323. {
  1324. // Consider the entire operation as failed, so remove the device we just added.
  1325. if (device != null)
  1326. RemoveDevice(device);
  1327. throw new InvalidOperationException(
  1328. $"Layout registered for type '{typeof(TDevice).Name}' did not produce a device of that type; layout probably has been overridden");
  1329. }
  1330. return deviceOfType;
  1331. }
  1332. /// <summary>
  1333. /// Tell the input system that a new device has become available.
  1334. /// </summary>
  1335. /// <param name="description">Description of the input device.</param>
  1336. /// <returns>The newly created device that has been added to <see cref="devices"/>.</returns>
  1337. /// <exception cref="ArgumentException">The given <paramref name="description"/> is empty -or-
  1338. /// no layout can be found that matches the given device <paramref name="description"/>.</exception>
  1339. /// <remarks>
  1340. /// This method is different from methods such as <see cref="AddDevice(string,string,string)"/>
  1341. /// or <see cref="AddDevice{TDevice}"/> in that it employs the usual matching process the
  1342. /// same way that it happens when the Unity runtime reports an input device.
  1343. ///
  1344. /// In particular, the same procedure described in the documentation for <see cref="onFindLayoutForDevice"/>
  1345. /// is employed where all registered <see cref="InputDeviceMatcher"/>s are matched against the
  1346. /// supplied device description and the most suitable match determines the layout to use. This in
  1347. /// turn is run through <see cref="onFindLayoutForDevice"/> to determine the final layout to use.
  1348. ///
  1349. /// If no suitable layout can be found, the method throws <c>ArgumentException</c>.
  1350. /// <example>
  1351. /// <code>
  1352. /// InputSystem.AddDevice(
  1353. /// new InputDeviceDescription
  1354. /// {
  1355. /// interfaceName = "Custom",
  1356. /// product = "Product"
  1357. /// });
  1358. /// </code>
  1359. /// </example>
  1360. /// </remarks>
  1361. public static InputDevice AddDevice(InputDeviceDescription description)
  1362. {
  1363. if (description.empty)
  1364. throw new ArgumentException("Description must not be empty", nameof(description));
  1365. return s_Manager.AddDevice(description);
  1366. }
  1367. /// <summary>
  1368. /// Add the given device back to the system.
  1369. /// </summary>
  1370. /// <param name="device">An input device. If the device is currently already added to
  1371. /// the system (i.e. is in <see cref="devices"/>), the method will do nothing.</param>
  1372. /// <exception cref="ArgumentNullException"></exception>
  1373. /// <remarks>
  1374. /// This can be used when a device has been manually removed with <see cref="RemoveDevice"/>.
  1375. ///
  1376. /// The device will be added to the <see cref="devices"/> list and a notification on
  1377. /// <see cref="onDeviceChange"/> will be triggered.
  1378. ///
  1379. /// It may be tempting to do the following but this will not work:
  1380. ///
  1381. /// <example>
  1382. /// <code>
  1383. /// // This will *NOT* work.
  1384. /// var device = new Gamepad();
  1385. /// InputSystem.AddDevice(device);
  1386. /// </code>
  1387. /// </example>
  1388. ///
  1389. /// <see cref="InputDevice"/>s, like <see cref="InputControl"/>s in general, cannot
  1390. /// simply be instantiated with <c>new</c> but must be created by the input system
  1391. /// instead.
  1392. /// </remarks>
  1393. /// <seealso cref="RemoveDevice"/>
  1394. /// <seealso cref="AddDevice{TDevice}"/>
  1395. /// <seealso cref="devices"/>
  1396. public static void AddDevice(InputDevice device)
  1397. {
  1398. if (device == null)
  1399. throw new ArgumentNullException(nameof(device));
  1400. s_Manager.AddDevice(device);
  1401. }
  1402. /// <summary>
  1403. /// Remove a device from the system such that it no longer receives input and is no longer part of the
  1404. /// set of devices in <see cref="devices"/>.
  1405. /// </summary>
  1406. /// <param name="device">Device to remove. If the device has already been removed (i.e. if <see cref="InputDevice.added"/>
  1407. /// is false), the method does nothing.</param>
  1408. /// <remarks>
  1409. /// Actions that are bound to controls on the device will automatically unbind when the device
  1410. /// is removed.
  1411. ///
  1412. /// When a device is removed, <see cref="onDeviceChange"/> will be triggered with <see cref="InputDeviceChange.Removed"/>.
  1413. /// The device will be removed from <see cref="devices"/> as well as from any device-specific getters such as
  1414. /// <see cref="Gamepad.all"/>.
  1415. /// </remarks>
  1416. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  1417. /// <seealso cref="InputDevice.added"/>
  1418. public static void RemoveDevice(InputDevice device)
  1419. {
  1420. s_Manager.RemoveDevice(device);
  1421. }
  1422. /// <summary>
  1423. /// Purge all disconnected devices from <see cref="disconnectedDevices"/>.
  1424. /// </summary>
  1425. /// <remarks>
  1426. /// This will release all references held on to for these devices or any of their controls and will
  1427. /// allow the devices to be reclaimed by the garbage collector.
  1428. /// </remarks>
  1429. /// <seealso cref="disconnectedDevices"/>
  1430. public static void FlushDisconnectedDevices()
  1431. {
  1432. s_Manager.FlushDisconnectedDevices();
  1433. }
  1434. public static InputDevice GetDevice(string nameOrLayout)
  1435. {
  1436. return s_Manager.TryGetDevice(nameOrLayout);
  1437. }
  1438. public static TDevice GetDevice<TDevice>()
  1439. where TDevice : InputDevice
  1440. {
  1441. TDevice result = null;
  1442. var lastUpdateTime = -1.0;
  1443. foreach (var device in devices)
  1444. {
  1445. var deviceOfType = device as TDevice;
  1446. if (deviceOfType == null)
  1447. continue;
  1448. if (result == null || deviceOfType.m_LastUpdateTimeInternal > lastUpdateTime)
  1449. {
  1450. result = deviceOfType;
  1451. lastUpdateTime = result.m_LastUpdateTimeInternal;
  1452. }
  1453. }
  1454. return result;
  1455. }
  1456. ////REVIEW: this API seems inconsistent with GetDevice(string); both have very different meaning yet very similar signatures
  1457. /// <summary>
  1458. /// Return the device of the given type <typeparamref name="TDevice"/> that has the
  1459. /// given usage assigned. Returns null if no such device currently exists.
  1460. /// </summary>
  1461. /// <param name="usage">Usage of the device, e.g. "LeftHand".</param>
  1462. /// <typeparam name="TDevice">Type of device to look for.</typeparam>
  1463. /// <returns>The device with the given type and usage or null.</returns>
  1464. /// <remarks>
  1465. /// Devices usages are most commonly employed to "tag" devices for a specific role.
  1466. /// A common scenario, for example, is to distinguish which hand a specific <see cref="XR.XRController"/>
  1467. /// is associated with. However, arbitrary usages can be assigned to devices.
  1468. /// <example>
  1469. /// <code>
  1470. /// // Get the left hand XRController.
  1471. /// var leftHand = InputSystem.GetDevice&lt;XRController&gt;(CommonUsages.leftHand);
  1472. ///
  1473. /// // Mark gamepad #2 as being for player 1.
  1474. /// InputSystem.SetDeviceUsage(Gamepad.all[1], "Player1");
  1475. /// // And later look it up.
  1476. /// var player1Gamepad = InputSystem.GetDevice&lt;Gamepad&gt;(new InternedString("Player1"));
  1477. /// </code>
  1478. /// </example>
  1479. /// </remarks>
  1480. /// <seealso cref="SetDeviceUsage(InputDevice,string)"/>
  1481. /// <seealso cref="InputControl.usages"/>
  1482. public static TDevice GetDevice<TDevice>(InternedString usage)
  1483. where TDevice : InputDevice
  1484. {
  1485. TDevice result = null;
  1486. var lastUpdateTime = -1.0;
  1487. foreach (var device in devices)
  1488. {
  1489. var deviceOfType = device as TDevice;
  1490. if (deviceOfType == null)
  1491. continue;
  1492. if (!deviceOfType.usages.Contains(usage))
  1493. continue;
  1494. if (result == null || deviceOfType.m_LastUpdateTimeInternal > lastUpdateTime)
  1495. {
  1496. result = deviceOfType;
  1497. lastUpdateTime = result.m_LastUpdateTimeInternal;
  1498. }
  1499. }
  1500. return result;
  1501. }
  1502. public static TDevice GetDevice<TDevice>(string usage)
  1503. where TDevice : InputDevice
  1504. {
  1505. return GetDevice<TDevice>(new InternedString(usage));
  1506. }
  1507. /// <summary>
  1508. /// Look up a device by its unique ID.
  1509. /// </summary>
  1510. /// <param name="deviceId">Unique ID of device. Such as given by <see cref="InputEvent.deviceId"/>.</param>
  1511. /// <returns>The device for the given ID or null if no device with the given ID exists (or no longer exists).</returns>
  1512. /// <remarks>
  1513. /// Device IDs are not reused in a given session of the application (or Unity editor).
  1514. /// </remarks>
  1515. /// <seealso cref="InputEvent.deviceId"/>
  1516. /// <seealso cref="InputDevice.deviceId"/>
  1517. /// <seealso cref="IInputRuntime.AllocateDeviceId"/>
  1518. public static InputDevice GetDeviceById(int deviceId)
  1519. {
  1520. return s_Manager.TryGetDeviceById(deviceId);
  1521. }
  1522. /// <summary>
  1523. /// Return the list of devices that have been reported by the <see cref="IInputRuntime">runtime</see>
  1524. /// but could not be matched to any known <see cref="InputControlLayout">layout</see>.
  1525. /// </summary>
  1526. /// <returns>A list of descriptions of devices that could not be recognized.</returns>
  1527. /// <remarks>
  1528. /// If new layouts are added to the system or if additional <see cref="InputDeviceMatcher">matches</see>
  1529. /// are added to existing layouts, devices in this list may appear or disappear.
  1530. /// </remarks>
  1531. /// <seealso cref="InputDeviceMatcher"/>
  1532. /// <seealso cref="RegisterLayoutMatcher"/>
  1533. public static List<InputDeviceDescription> GetUnsupportedDevices()
  1534. {
  1535. var list = new List<InputDeviceDescription>();
  1536. GetUnsupportedDevices(list);
  1537. return list;
  1538. }
  1539. public static int GetUnsupportedDevices(List<InputDeviceDescription> descriptions)
  1540. {
  1541. return s_Manager.GetUnsupportedDevices(descriptions);
  1542. }
  1543. /// <summary>
  1544. /// (Re-)enable the given device.
  1545. /// </summary>
  1546. /// <param name="device">Device to enable. If already enabled, the method will do nothing.</param>
  1547. /// <remarks>
  1548. /// This can be used after a device has been disabled with <see cref="DisableDevice"/> or
  1549. /// with devices that start out in disabled state (usually the case for all <see cref="Sensor"/>
  1550. /// devices).
  1551. ///
  1552. /// When enabled, a device will receive input when available.
  1553. ///
  1554. /// <example>
  1555. /// <code>
  1556. /// // Enable the gyroscope, if present.
  1557. /// if (Gyroscope.current != null)
  1558. /// InputSystem.EnableDevice(Gyroscope.current);
  1559. /// </code>
  1560. /// </example>
  1561. /// </remarks>
  1562. /// <seealso cref="DisableDevice"/>
  1563. /// <seealso cref="InputDevice.enabled"/>
  1564. public static void EnableDevice(InputDevice device)
  1565. {
  1566. s_Manager.EnableOrDisableDevice(device, true);
  1567. }
  1568. /// <summary>
  1569. /// Disable the given device, i.e. "mute" it.
  1570. /// </summary>
  1571. /// <param name="device">Device to disable. If already disabled, the method will do nothing.</param>
  1572. /// <remarks>
  1573. /// A disabled device will not receive input and will remain in its default state. It will remain
  1574. /// present in the system but without actually feeding input into it.
  1575. ///
  1576. /// Disabling devices is most useful for <see cref="Sensor"/> devices on battery-powered platforms
  1577. /// where having a sensor enabled will increase energy consumption. Sensors will usually start
  1578. /// out in disabled state and can be enabled, when needed, with <see cref="EnableDevice"/> and
  1579. /// disabled again wth this method.
  1580. ///
  1581. /// However, disabling a device can be useful in other situations, too. For example, when simulating
  1582. /// input (say, mouse input) locally from a remote source, it can be desirable to turn off the respective
  1583. /// local device.
  1584. ///
  1585. /// To remove a device altogether, use <see cref="RemoveDevice"/> instead. This will not only silence
  1586. /// input but remove the <see cref="InputDevice"/> instance from the system altogether.
  1587. /// </remarks>
  1588. /// <seealso cref="EnableDevice"/>
  1589. /// <seealso cref="InputDevice.enabled"/>
  1590. public static void DisableDevice(InputDevice device)
  1591. {
  1592. s_Manager.EnableOrDisableDevice(device, false);
  1593. }
  1594. public static bool TrySyncDevice(InputDevice device)
  1595. {
  1596. if (device == null)
  1597. throw new ArgumentNullException(nameof(device));
  1598. var syncCommand = RequestSyncCommand.Create();
  1599. var result = device.ExecuteCommand(ref syncCommand);
  1600. return result >= 0;
  1601. }
  1602. public static bool TryResetDevice(InputDevice device)
  1603. {
  1604. if (device == null)
  1605. throw new ArgumentNullException(nameof(device));
  1606. return device.RequestReset();
  1607. }
  1608. ////REVIEW: should there be a global pause state? what about haptics that are issued *while* paused?
  1609. /// <summary>
  1610. /// Pause haptic effect playback on all devices.
  1611. /// </summary>
  1612. /// <remarks>
  1613. /// Calls <see cref="Haptics.IHaptics.PauseHaptics"/> on all <see cref="InputDevice">input devices</see>
  1614. /// that implement the interface.
  1615. /// </remarks>
  1616. /// <seealso cref="ResumeHaptics"/>
  1617. /// <seealso cref="ResetHaptics"/>
  1618. /// <example>
  1619. /// <code>
  1620. /// // When going into the menu from gameplay, pause haptics.
  1621. /// gameplayControls.backAction.onPerformed +=
  1622. /// ctx =>
  1623. /// {
  1624. /// gameplayControls.Disable();
  1625. /// menuControls.Enable();
  1626. /// InputSystem.PauseHaptics();
  1627. /// };
  1628. /// </code>
  1629. /// </example>
  1630. public static void PauseHaptics()
  1631. {
  1632. var devicesList = devices;
  1633. var devicesCount = devicesList.Count;
  1634. for (var i = 0; i < devicesCount; ++i)
  1635. {
  1636. var device = devicesList[i];
  1637. if (device is IHaptics haptics)
  1638. haptics.PauseHaptics();
  1639. }
  1640. }
  1641. /// <summary>
  1642. /// Resume haptic effect playback on all devices.
  1643. /// </summary>
  1644. /// <remarks>
  1645. /// Calls <see cref="Haptics.IHaptics.ResumeHaptics"/> on all <see cref="InputDevice">input devices</see>
  1646. /// that implement the interface.
  1647. /// </remarks>
  1648. /// <seealso cref="PauseHaptics"/>
  1649. public static void ResumeHaptics()
  1650. {
  1651. var devicesList = devices;
  1652. var devicesCount = devicesList.Count;
  1653. for (var i = 0; i < devicesCount; ++i)
  1654. {
  1655. var device = devicesList[i];
  1656. if (device is IHaptics haptics)
  1657. haptics.ResumeHaptics();
  1658. }
  1659. }
  1660. /// <summary>
  1661. /// Stop haptic effect playback on all devices.
  1662. /// </summary>
  1663. /// <remarks>
  1664. /// Will reset haptics effects on all devices to their default state.
  1665. ///
  1666. /// Calls <see cref="Haptics.IHaptics.ResetHaptics"/> on all <see cref="InputDevice">input devices</see>
  1667. /// that implement the interface.
  1668. /// </remarks>
  1669. public static void ResetHaptics()
  1670. {
  1671. var devicesList = devices;
  1672. var devicesCount = devicesList.Count;
  1673. for (var i = 0; i < devicesCount; ++i)
  1674. {
  1675. var device = devicesList[i];
  1676. if (device is IHaptics haptics)
  1677. haptics.ResetHaptics();
  1678. }
  1679. }
  1680. #endregion
  1681. #region Controls
  1682. /// <summary>
  1683. /// Set the usage tag of the given device to <paramref name="usage"/>.
  1684. /// </summary>
  1685. /// <param name="device">Device to set the usage on.</param>
  1686. /// <param name="usage">New usage for the device.</param>
  1687. /// <remarks>
  1688. /// Usages allow to "tag" a specific device such that the tag can then be used in lookups
  1689. /// and bindings. A common use is for identifying the handedness of an <see cref="XR.XRController"/>
  1690. /// but the usages can be arbitrary strings.
  1691. ///
  1692. /// This method either sets the usages of the device to a single string (meaning it will
  1693. /// clear whatever, if any usages, the device has when the method is called) or,
  1694. /// if <paramref name="usage"/> is null or empty, resets the usages of the device
  1695. /// to be empty. To add to a device's set of usages, call <see cref="AddDeviceUsage(InputDevice,string)"/>.
  1696. /// To remove usages from a device, call <see cref="RemoveDeviceUsage(InputDevice,string)"/>.
  1697. ///
  1698. /// The set of usages a device has can be queried with <see cref="InputControl.usages"/> (a device
  1699. /// is an <see cref="InputControl"/> and thus, like controls, has an associated set of usages).
  1700. ///
  1701. /// <example>
  1702. /// <code>
  1703. /// // Tag a gamepad to be associated with player #1.
  1704. /// InputSystem.SetDeviceUsage(myGamepad, "Player1");
  1705. ///
  1706. /// // Create an action that binds to player #1's gamepad specifically.
  1707. /// var action = new InputAction(binding: "&lt;Gamepad&gt;{Player1}/buttonSouth");
  1708. ///
  1709. /// // Move the tag from one gamepad to another.
  1710. /// InputSystem.SetDeviceUsage(myGamepad, null); // Clears usages on 'myGamepad'.
  1711. /// InputSystem.SetDeviceUsage(otherGamepad, "Player1");
  1712. /// </code>
  1713. /// </example>
  1714. /// </remarks>
  1715. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  1716. /// <seealso cref="InputControl.usages"/>
  1717. /// <seealso cref="AddDeviceUsage(InputDevice,string)"/>
  1718. /// <seealso cref="RemoveDeviceUsage(InputDevice,string)"/>
  1719. /// <seealso cref="CommonUsages"/>
  1720. /// <seealso cref="InputDeviceChange.UsageChanged"/>
  1721. public static void SetDeviceUsage(InputDevice device, string usage)
  1722. {
  1723. SetDeviceUsage(device, new InternedString(usage));
  1724. }
  1725. /// <summary>
  1726. /// Set the usage tag of the given device to <paramref name="usage"/>.
  1727. /// </summary>
  1728. /// <param name="device">Device to set the usage on.</param>
  1729. /// <param name="usage">New usage for the device.</param>
  1730. /// <remarks>
  1731. /// Usages allow to "tag" a specific device such that the tag can then be used in lookups
  1732. /// and bindings. A common use is for identifying the handedness of an <see cref="XR.XRController"/>
  1733. /// but the usages can be arbitrary strings.
  1734. ///
  1735. /// This method either sets the usages of the device to a single string (meaning it will
  1736. /// clear whatever, if any usages, the device has when the method is called) or,
  1737. /// if <paramref name="usage"/> is null or empty, resets the usages of the device
  1738. /// to be empty. To add to a device's set of usages, call <see cref="AddDeviceUsage(InputDevice,InternedString)"/>.
  1739. /// To remove usages from a device, call <see cref="RemoveDeviceUsage(InputDevice,InternedString)"/>.
  1740. ///
  1741. /// The set of usages a device has can be queried with <see cref="InputControl.usages"/> (a device
  1742. /// is an <see cref="InputControl"/> and thus, like controls, has an associated set of usages).
  1743. ///
  1744. /// If the set of usages on the device changes as a result of calling this method, <see cref="onDeviceChange"/>
  1745. /// will be triggered with <see cref="InputDeviceChange.UsageChanged"/>.
  1746. ///
  1747. /// <example>
  1748. /// <code>
  1749. /// // Tag a gamepad to be associated with player #1.
  1750. /// InputSystem.SetDeviceUsage(myGamepad, new InternedString("Player1"));
  1751. ///
  1752. /// // Create an action that binds to player #1's gamepad specifically.
  1753. /// var action = new InputAction(binding: "&lt;Gamepad&gt;{Player1}/buttonSouth");
  1754. ///
  1755. /// // Move the tag from one gamepad to another.
  1756. /// InputSystem.SetDeviceUsage(myGamepad, null); // Clears usages on 'myGamepad'.
  1757. /// InputSystem.SetDeviceUsage(otherGamepad, new InternedString("Player1"));
  1758. /// </code>
  1759. /// </example>
  1760. /// </remarks>
  1761. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  1762. /// <seealso cref="InputControl.usages"/>
  1763. /// <seealso cref="AddDeviceUsage(InputDevice,InternedString)"/>
  1764. /// <seealso cref="RemoveDeviceUsage(InputDevice,InternedString)"/>
  1765. /// <seealso cref="CommonUsages"/>
  1766. /// <seealso cref="InputDeviceChange.UsageChanged"/>
  1767. public static void SetDeviceUsage(InputDevice device, InternedString usage)
  1768. {
  1769. s_Manager.SetDeviceUsage(device, usage);
  1770. }
  1771. /// <summary>
  1772. /// Add a usage tag to the given device.
  1773. /// </summary>
  1774. /// <param name="device">Device to add the usage to.</param>
  1775. /// <param name="usage">New usage to add to the device.</param>
  1776. /// <remarks>
  1777. /// Usages allow to "tag" a specific device such that the tag can then be used in lookups
  1778. /// and bindings. A common use is for identifying the handedness of an <see cref="XR.XRController"/>
  1779. /// but the usages can be arbitrary strings.
  1780. ///
  1781. /// This method adds a new usage to the device's set of usages. If the device already has
  1782. /// the given usage, the method does nothing. To instead set the device's usages to a single
  1783. /// one, use <see cref="SetDeviceUsage(InputDevice,string)"/>. To remove usages from a device,
  1784. /// call <see cref="RemoveDeviceUsage(InputDevice,string)"/>.
  1785. ///
  1786. /// The set of usages a device has can be queried with <see cref="InputControl.usages"/> (a device
  1787. /// is an <see cref="InputControl"/> and thus, like controls, has an associated set of usages).
  1788. ///
  1789. /// If the set of usages on the device changes as a result of calling this method, <see cref="onDeviceChange"/>
  1790. /// will be triggered with <see cref="InputDeviceChange.UsageChanged"/>.
  1791. /// </remarks>
  1792. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  1793. /// <exception cref="ArgumentException"><paramref name="usage"/> is null or empty.</exception>
  1794. /// <seealso cref="InputControl.usages"/>
  1795. /// <seealso cref="SetDeviceUsage(InputDevice,string)"/>
  1796. /// <seealso cref="RemoveDeviceUsage(InputDevice,string)"/>
  1797. /// <seealso cref="CommonUsages"/>
  1798. /// <seealso cref="InputDeviceChange.UsageChanged"/>
  1799. public static void AddDeviceUsage(InputDevice device, string usage)
  1800. {
  1801. s_Manager.AddDeviceUsage(device, new InternedString(usage));
  1802. }
  1803. /// <summary>
  1804. /// Add a usage tag to the given device.
  1805. /// </summary>
  1806. /// <param name="device">Device to add the usage to.</param>
  1807. /// <param name="usage">New usage to add to the device.</param>
  1808. /// <remarks>
  1809. /// Usages allow to "tag" a specific device such that the tag can then be used in lookups
  1810. /// and bindings. A common use is for identifying the handedness of an <see cref="XR.XRController"/>
  1811. /// but the usages can be arbitrary strings.
  1812. ///
  1813. /// This method adds a new usage to the device's set of usages. If the device already has
  1814. /// the given usage, the method does nothing. To instead set the device's usages to a single
  1815. /// one, use <see cref="SetDeviceUsage(InputDevice,InternedString)"/>. To remove usages from a device,
  1816. /// call <see cref="RemoveDeviceUsage(InputDevice,InternedString)"/>.
  1817. ///
  1818. /// The set of usages a device has can be queried with <see cref="InputControl.usages"/> (a device
  1819. /// is an <see cref="InputControl"/> and thus, like controls, has an associated set of usages).
  1820. ///
  1821. /// If the set of usages on the device changes as a result of calling this method, <see cref="onDeviceChange"/>
  1822. /// will be triggered with <see cref="InputDeviceChange.UsageChanged"/>.
  1823. /// </remarks>
  1824. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  1825. /// <exception cref="ArgumentException"><paramref name="usage"/> is empty.</exception>
  1826. /// <seealso cref="InputControl.usages"/>
  1827. /// <seealso cref="SetDeviceUsage(InputDevice,InternedString)"/>
  1828. /// <seealso cref="RemoveDeviceUsage(InputDevice,InternedString)"/>
  1829. /// <seealso cref="CommonUsages"/>
  1830. /// <seealso cref="InputDeviceChange.UsageChanged"/>
  1831. public static void AddDeviceUsage(InputDevice device, InternedString usage)
  1832. {
  1833. s_Manager.AddDeviceUsage(device, usage);
  1834. }
  1835. /// <summary>
  1836. /// Remove a usage tag from the given device.
  1837. /// </summary>
  1838. /// <param name="device">Device to remove the usage from.</param>
  1839. /// <param name="usage">Usage to remove from the device.</param>
  1840. /// <remarks>
  1841. /// This method removes an existing usage from the given device. If the device does not
  1842. /// have the given usage tag, the method does nothing. Use <see cref="SetDeviceUsage(InputDevice,string)"/>
  1843. /// or <see cref="AddDeviceUsage(InputDevice,string)"/> to add usages to a device.
  1844. ///
  1845. /// The set of usages a device has can be queried with <see cref="InputControl.usages"/> (a device
  1846. /// is an <see cref="InputControl"/> and thus, like controls, has an associated set of usages).
  1847. ///
  1848. /// If the set of usages on the device changes as a result of calling this method, <see cref="onDeviceChange"/>
  1849. /// will be triggered with <see cref="InputDeviceChange.UsageChanged"/>.
  1850. /// </remarks>
  1851. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  1852. /// <exception cref="ArgumentException"><paramref name="usage"/> is null or empty.</exception>
  1853. /// <seealso cref="InputControl.usages"/>
  1854. /// <seealso cref="SetDeviceUsage(InputDevice,string)"/>
  1855. /// <seealso cref="AddDeviceUsage(InputDevice,string)"/>
  1856. /// <seealso cref="CommonUsages"/>
  1857. /// <seealso cref="InputDeviceChange.UsageChanged"/>
  1858. public static void RemoveDeviceUsage(InputDevice device, string usage)
  1859. {
  1860. s_Manager.RemoveDeviceUsage(device, new InternedString(usage));
  1861. }
  1862. /// <summary>
  1863. /// Remove a usage tag from the given device.
  1864. /// </summary>
  1865. /// <param name="device">Device to remove the usage from.</param>
  1866. /// <param name="usage">Usage to remove from the device.</param>
  1867. /// <remarks>
  1868. /// This method removes an existing usage from the given device. If the device does not
  1869. /// have the given usage tag, the method does nothing. Use <see cref="SetDeviceUsage(InputDevice,InternedString)"/>
  1870. /// or <see cref="AddDeviceUsage(InputDevice,InternedString)"/> to add usages to a device.
  1871. ///
  1872. /// The set of usages a device has can be queried with <see cref="InputControl.usages"/> (a device
  1873. /// is an <see cref="InputControl"/> and thus, like controls, has an associated set of usages).
  1874. ///
  1875. /// If the set of usages on the device changes as a result of calling this method, <see cref="onDeviceChange"/>
  1876. /// will be triggered with <see cref="InputDeviceChange.UsageChanged"/>.
  1877. /// </remarks>
  1878. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  1879. /// <exception cref="ArgumentException"><paramref name="usage"/> is empty.</exception>
  1880. /// <seealso cref="InputControl.usages"/>
  1881. /// <seealso cref="SetDeviceUsage(InputDevice,InternedString)"/>
  1882. /// <seealso cref="AddDeviceUsage(InputDevice,InternedString)"/>
  1883. /// <seealso cref="CommonUsages"/>
  1884. /// <seealso cref="InputDeviceChange.UsageChanged"/>
  1885. public static void RemoveDeviceUsage(InputDevice device, InternedString usage)
  1886. {
  1887. s_Manager.RemoveDeviceUsage(device, usage);
  1888. }
  1889. /// <summary>
  1890. /// Find the first control that matches the given control path.
  1891. /// </summary>
  1892. /// <param name="path">Path of a control, e.g. <c>"&lt;Gamepad&gt;/buttonSouth"</c>. See <see cref="InputControlPath"/>
  1893. /// for details.</param>
  1894. /// <returns>The first control that matches the given path or <c>null</c> if no control matches.</returns>
  1895. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c> or empty.</exception>
  1896. /// <remarks>
  1897. /// If multiple controls match the given path, which result is considered the first is indeterminate.
  1898. ///
  1899. /// <example>
  1900. /// <code>
  1901. /// // Add gamepad.
  1902. /// InputSystem.AddDevice&lt;Gamepad&gt;();
  1903. ///
  1904. /// // Look up various controls on it.
  1905. /// var aButton = InputSystem.FindControl("&lt;Gamepad&gt;/buttonSouth");
  1906. /// var leftStickX = InputSystem.FindControl("*/leftStick/x");
  1907. /// var bButton = InputSystem.FindControl"*/{back}");
  1908. ///
  1909. /// // This one returns the gamepad itself as devices are also controls.
  1910. /// var gamepad = InputSystem.FindControl("&lt;Gamepad&gt;");
  1911. /// </code>
  1912. /// </example>
  1913. /// </remarks>
  1914. /// <seealso cref="InputControlPath"/>
  1915. /// <seealso cref="InputControl.path"/>
  1916. public static InputControl FindControl(string path)
  1917. {
  1918. if (string.IsNullOrEmpty(path))
  1919. throw new ArgumentNullException(nameof(path));
  1920. var devices = s_Manager.devices;
  1921. var numDevices = devices.Count;
  1922. for (var i = 0; i < numDevices; ++i)
  1923. {
  1924. var device = devices[i];
  1925. var control = InputControlPath.TryFindControl(device, path);
  1926. if (control != null)
  1927. return control;
  1928. }
  1929. return null;
  1930. }
  1931. /// <summary>
  1932. /// Find all controls that match the given <see cref="InputControlPath">control path</see>.
  1933. /// </summary>
  1934. /// <param name="path"></param>
  1935. /// <returns></returns>
  1936. /// <example>
  1937. /// <code>
  1938. /// // Find all gamepads (literally: that use the "Gamepad" layout).
  1939. /// InputSystem.FindControls("&lt;Gamepad&gt;");
  1940. ///
  1941. /// // Find all sticks on all gamepads.
  1942. /// InputSystem.FindControls("&lt;Gamepad&gt;/*stick");
  1943. ///
  1944. /// // Same but filter stick by type rather than by name.
  1945. /// InputSystem.FindControls&lt;StickControl&gt;("&lt;Gamepad&gt;/*");
  1946. /// </code>
  1947. /// </example>
  1948. /// <seealso cref="FindControls{TControl}(string)"/>
  1949. /// <seealso cref="FindControls{TControl}(string,ref UnityEngine.InputSystem.InputControlList{TControl})"/>
  1950. public static InputControlList<InputControl> FindControls(string path)
  1951. {
  1952. return FindControls<InputControl>(path);
  1953. }
  1954. public static InputControlList<TControl> FindControls<TControl>(string path)
  1955. where TControl : InputControl
  1956. {
  1957. var list = new InputControlList<TControl>();
  1958. FindControls(path, ref list);
  1959. return list;
  1960. }
  1961. public static int FindControls<TControl>(string path, ref InputControlList<TControl> controls)
  1962. where TControl : InputControl
  1963. {
  1964. return s_Manager.GetControls(path, ref controls);
  1965. }
  1966. #endregion
  1967. #region Events
  1968. /// <summary>
  1969. /// Called during <see cref="Update"/> for each event that is processed.
  1970. /// </summary>
  1971. /// <remarks>
  1972. /// Every time the input system updates (see <see cref="InputSettings.updateMode"/>
  1973. /// or <see cref="Update"/> for details about when and how this happens),
  1974. /// it flushes all events from the internal event buffer that are due in the current
  1975. /// update (<see cref="InputSettings.timesliceEvents"/> for details about when events
  1976. /// may be postponed to a subsequent frame).
  1977. ///
  1978. /// As the input system reads events from the buffer one by one, it will trigger this
  1979. /// callback for each event which originates from a recognized device, before then proceeding
  1980. /// to process the event. However, if any of the callbacks sets <see cref="InputEvent.handled"/>
  1981. /// to true, the event will be skipped and ignored.
  1982. ///
  1983. /// Note that the input system does NOT sort events by timestamps (<see cref="InputEvent.time"/>).
  1984. /// Instead, they are consumed in the order they are produced. This means that they
  1985. /// will also surface on this callback in that order.
  1986. ///
  1987. /// <example>
  1988. /// <code>
  1989. /// // Treat left+right mouse button as middle mouse button.
  1990. /// // (Note: This example is more for demonstrative purposes; it isn't necessarily a good use case)
  1991. /// InputSystem.onEvent +=
  1992. /// (eventPtr, device) =>
  1993. /// {
  1994. /// // Only deal with state events.
  1995. /// if (!eventPtr.IsA&lt;StateEvent&gt;())
  1996. /// return;
  1997. ///
  1998. /// if (!(device is Mouse mouse))
  1999. /// return;
  2000. ///
  2001. /// mouse.leftButton.ReadValueFromEvent(eventPtr, out var lmbDown);
  2002. /// mouse.rightButton.ReadValueFromEvent(eventPtr, out var rmbDown);
  2003. ///
  2004. /// if (lmbDown > 0 && rmbDown > 0)
  2005. /// mouse.middleButton.WriteValueIntoEvent(1f, eventPtr);
  2006. /// };
  2007. /// </code>
  2008. /// </example>
  2009. ///
  2010. /// If you are looking for a way to capture events, <see cref="InputEventTrace"/> may be of
  2011. /// interest and an alternative to directly hooking into this event.
  2012. ///
  2013. /// If you are looking to monitor changes to specific input controls, state change monitors
  2014. /// (see <see cref="InputState.AddChangeMonitor(InputControl,IInputStateChangeMonitor,long)"/>
  2015. /// are usually a more efficient and convenient way to set this up.
  2016. /// </remarks>
  2017. /// <exception cref="ArgumentNullException">Delegate reference is <c>null</c>.</exception>
  2018. /// <seealso cref="QueueEvent(InputEventPtr)"/>
  2019. /// <seealso cref="InputEvent"/>
  2020. /// <seealso cref="Update"/>
  2021. /// <seealso cref="InputSettings.updateMode"/>
  2022. public static event Action<InputEventPtr, InputDevice> onEvent
  2023. {
  2024. add
  2025. {
  2026. if (value == null)
  2027. throw new ArgumentNullException(nameof(value));
  2028. lock (s_Manager)
  2029. s_Manager.onEvent += value;
  2030. }
  2031. remove
  2032. {
  2033. if (value == null)
  2034. throw new ArgumentNullException(nameof(value));
  2035. lock (s_Manager)
  2036. s_Manager.onEvent -= value;
  2037. }
  2038. }
  2039. ////TODO: need to handle events being queued *during* event processing
  2040. /// <summary>
  2041. /// Add an event to the internal event queue.
  2042. /// </summary>
  2043. /// <param name="eventPtr">Event to add to the internal event buffer.</param>
  2044. /// <exception cref="ArgumentException"><paramref name="eventPtr"/> is not
  2045. /// valid (see <see cref="InputEventPtr.valid"/>).</exception>
  2046. /// <remarks>
  2047. /// The event will be copied in full to the internal event buffer meaning that
  2048. /// you can release memory for the event after it has been queued. The internal event
  2049. /// buffer is flushed on the next input system update (see <see cref="Update"/>).
  2050. /// Note that if timeslicing is in effect (see <see cref="InputSettings.timesliceEvents"/>),
  2051. /// then the event may not get processed until its <see cref="InputEvent.time"/> timestamp
  2052. /// is within the update window of the input system.
  2053. ///
  2054. /// As part of queuing, the event will receive its own unique ID (see <see cref="InputEvent.eventId"/>).
  2055. /// Note that this ID will be written into the memory buffer referenced by <see cref="eventPtr"/>
  2056. /// meaning that after calling <c>QueueEvent</c>, you will see the event ID with which the event
  2057. /// was queued.
  2058. ///
  2059. /// <example>
  2060. /// <code>
  2061. /// // Queue an input event on the first gamepad.
  2062. /// var gamepad = Gamepad.all[0];
  2063. /// using (StateEvent.From(gamepad, out var eventPtr))
  2064. /// {
  2065. /// gamepad.leftStick.WriteValueIntoEvent(new Vector2(0.123, 0.234), eventPtr);
  2066. /// InputSystem.QueueEvent(eventPtr);
  2067. /// }
  2068. /// </code>
  2069. /// </example>
  2070. /// </remarks>
  2071. /// <seealso cref="Update"/>
  2072. public static void QueueEvent(InputEventPtr eventPtr)
  2073. {
  2074. if (!eventPtr.valid)
  2075. throw new ArgumentException("Received a null event pointer", nameof(eventPtr));
  2076. s_Manager.QueueEvent(eventPtr);
  2077. }
  2078. public static void QueueEvent<TEvent>(ref TEvent inputEvent)
  2079. where TEvent : struct, IInputEventTypeInfo
  2080. {
  2081. s_Manager.QueueEvent(ref inputEvent);
  2082. }
  2083. ////REVIEW: consider moving these out into extension methods in UnityEngine.InputSystem.LowLevel
  2084. ////TODO: find a more elegant solution for this
  2085. // Mono will ungracefully poop exceptions if we try to use LayoutKind.Explicit in generic
  2086. // structs. So we can't just stuff a generic TState into a StateEvent<TState> and enforce
  2087. // proper layout. Thus the jumping through lots of ugly hoops here.
  2088. private unsafe struct StateEventBuffer
  2089. {
  2090. public StateEvent stateEvent;
  2091. public const int kMaxSize = 512;
  2092. public fixed byte data[kMaxSize - 1]; // StateEvent already adds one.
  2093. }
  2094. /// <summary>
  2095. /// Queue a <see cref="StateEvent"/> to update the input state of the given device.
  2096. /// </summary>
  2097. /// <param name="device">Device whose input state to update</param>
  2098. /// <param name="state"></param>
  2099. /// <param name="time">Timestamp for the event. If not supplied, the current time is used. Note
  2100. /// that if the given time is in the future and timeslicing is active (<see cref="InputSettings.timesliceEvents"/>,
  2101. /// the event will only get processed once the actual time has caught up with the given time.</param>
  2102. /// <typeparam name="TState">Type of input state, such as <see cref="MouseState"/>. Must match the expected
  2103. /// type of state of <paramref name="device"/>.</typeparam>
  2104. /// <remarks>
  2105. /// The given state must match exactly what is expected by the given device. If unsure, an alternative
  2106. /// is to grab the state as an event directly from the device using <see
  2107. /// cref="StateEvent.From(InputDevice,out InputEventPtr,Unity.Collections.Allocator)"/> which can then
  2108. /// be queued using <see cref="QueueEvent(InputEventPtr)"/>.
  2109. ///
  2110. /// <example>
  2111. /// <code>
  2112. /// // Allocates temporary, unmanaged memory for the event.
  2113. /// // using statement automatically disposes the memory once we have queued the event.
  2114. /// using (StateEvent.From(Mouse.current, out var eventPtr))
  2115. /// {
  2116. /// // Use controls on mouse to write values into event.
  2117. /// Mouse.current.position.WriteValueIntoEvent(new Vector(123, 234), eventPtr);
  2118. ///
  2119. /// // Queue event.
  2120. /// InputSystem.QueueEvent(eventPtr);
  2121. /// }
  2122. /// </code>
  2123. /// </example>
  2124. ///
  2125. /// The event will only be queued and not processed right away. This means that the state of
  2126. /// <paramref name="device"/> will not change immediately as a result of calling this method. Instead,
  2127. /// the event will be processed as part of the next input update.
  2128. ///
  2129. /// Note that this method updates the complete input state of the device including all of its
  2130. /// controls. To update just part of the state of a device, you can use <see cref="QueueDeltaStateEvent{TDelta}"/>
  2131. /// (however, note that there are some restrictions; see documentation).
  2132. /// <example>
  2133. /// <code>
  2134. /// InputSystem.QueueStateEvent(Mouse.current, new MouseState { position = new Vector(123, 234) });
  2135. /// </code>
  2136. /// </example>
  2137. /// </remarks>
  2138. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  2139. /// <exception cref="InvalidOperationException"><paramref name="device"/> has not been added to the system
  2140. /// (<see cref="AddDevice(InputDevice)"/>) and thus cannot receive events.</exception>
  2141. /// <exception cref="ArgumentException"></exception>
  2142. public static unsafe void QueueStateEvent<TState>(InputDevice device, TState state, double time = -1)
  2143. where TState : struct, IInputStateTypeInfo
  2144. {
  2145. if (device == null)
  2146. throw new ArgumentNullException(nameof(device));
  2147. // Make sure device is actually in the system.
  2148. if (device.m_DeviceIndex == InputDevice.kInvalidDeviceIndex)
  2149. throw new InvalidOperationException(
  2150. $"Cannot queue state event for device '{device}' because device has not been added to system");
  2151. ////REVIEW: does it make more sense to go off the 'stateBlock' on the device and let that determine size?
  2152. var stateSize = (uint)UnsafeUtility.SizeOf<TState>();
  2153. if (stateSize > StateEventBuffer.kMaxSize)
  2154. throw new ArgumentException(
  2155. $"Size of '{typeof(TState).Name}' exceeds maximum supported state size of {StateEventBuffer.kMaxSize}",
  2156. nameof(state));
  2157. var eventSize = UnsafeUtility.SizeOf<StateEvent>() + stateSize - StateEvent.kStateDataSizeToSubtract;
  2158. if (time < 0)
  2159. time = InputRuntime.s_Instance.currentTime;
  2160. else
  2161. time += InputRuntime.s_CurrentTimeOffsetToRealtimeSinceStartup;
  2162. StateEventBuffer eventBuffer;
  2163. eventBuffer.stateEvent =
  2164. new StateEvent
  2165. {
  2166. baseEvent = new InputEvent(StateEvent.Type, (int)eventSize, device.deviceId, time),
  2167. stateFormat = state.format
  2168. };
  2169. var ptr = eventBuffer.stateEvent.stateData;
  2170. UnsafeUtility.MemCpy(ptr, UnsafeUtility.AddressOf(ref state), stateSize);
  2171. s_Manager.QueueEvent(ref eventBuffer.stateEvent);
  2172. }
  2173. private unsafe struct DeltaStateEventBuffer
  2174. {
  2175. public DeltaStateEvent stateEvent;
  2176. public const int kMaxSize = 512;
  2177. public fixed byte data[kMaxSize - 1]; // DeltaStateEvent already adds one.
  2178. }
  2179. /// <summary>
  2180. /// Queue a <see cref="DeltaStateEvent"/> to update part of the input state of the given device.
  2181. /// </summary>
  2182. /// <param name="control">Control on a device to update state of.</param>
  2183. /// <param name="delta">New state for the control. Type of state must match the state of the control.</param>
  2184. /// <param name="time"></param>
  2185. /// <typeparam name="TDelta"></typeparam>
  2186. /// <exception cref="ArgumentNullException"><paramref name="control"/> is null.</exception>
  2187. /// <exception cref="InvalidOperationException"></exception>
  2188. /// <exception cref="ArgumentException"></exception>
  2189. public static unsafe void QueueDeltaStateEvent<TDelta>(InputControl control, TDelta delta, double time = -1)
  2190. where TDelta : struct
  2191. {
  2192. if (control == null)
  2193. throw new ArgumentNullException(nameof(control));
  2194. if (control.stateBlock.bitOffset != 0)
  2195. throw new InvalidOperationException(
  2196. $"Cannot send delta state events against bitfield controls: {control}");
  2197. // Make sure device is actually in the system.
  2198. var device = control.device;
  2199. if (device.m_DeviceIndex == InputDevice.kInvalidDeviceIndex)
  2200. throw new InvalidOperationException(
  2201. $"Cannot queue state event for control '{control}' on device '{device}' because device has not been added to system");
  2202. if (time < 0)
  2203. time = InputRuntime.s_Instance.currentTime;
  2204. else
  2205. time += InputRuntime.s_CurrentTimeOffsetToRealtimeSinceStartup;
  2206. var deltaSize = (uint)UnsafeUtility.SizeOf<TDelta>();
  2207. if (deltaSize > DeltaStateEventBuffer.kMaxSize)
  2208. throw new ArgumentException(
  2209. $"Size of state delta '{typeof(TDelta).Name}' exceeds maximum supported state size of {DeltaStateEventBuffer.kMaxSize}",
  2210. nameof(delta));
  2211. ////TODO: recognize a matching C# representation of a state format and convert to what we expect for trivial cases
  2212. if (deltaSize != control.stateBlock.alignedSizeInBytes)
  2213. throw new ArgumentException(
  2214. $"Size {deltaSize} of delta state of type {typeof(TDelta).Name} provided for control '{control}' does not match size {control.stateBlock.alignedSizeInBytes} of control",
  2215. nameof(delta));
  2216. var eventSize = UnsafeUtility.SizeOf<DeltaStateEvent>() + deltaSize - 1;
  2217. DeltaStateEventBuffer eventBuffer;
  2218. eventBuffer.stateEvent =
  2219. new DeltaStateEvent
  2220. {
  2221. baseEvent = new InputEvent(DeltaStateEvent.Type, (int)eventSize, device.deviceId, time),
  2222. stateFormat = device.stateBlock.format,
  2223. stateOffset = control.m_StateBlock.byteOffset - device.m_StateBlock.byteOffset
  2224. };
  2225. var ptr = eventBuffer.stateEvent.stateData;
  2226. UnsafeUtility.MemCpy(ptr, UnsafeUtility.AddressOf(ref delta), deltaSize);
  2227. s_Manager.QueueEvent(ref eventBuffer.stateEvent);
  2228. }
  2229. /// <summary>
  2230. /// Queue a <see cref="DeviceConfigurationEvent"/> that signals that the configuration of the given device has changed
  2231. /// and that cached configuration will thus have to be refreshed.
  2232. /// </summary>
  2233. /// <param name="device">Device whose configuration has changed.</param>
  2234. /// <param name="time">Timestamp for the event. If not supplied, the current time will be used.</param>
  2235. /// <remarks>
  2236. /// All state of an input device that is not input or output state is considered its "configuration".
  2237. ///
  2238. /// A simple example is keyboard layouts. A <see cref="Keyboard"/> will typically have an associated
  2239. /// keyboard layout that dictates the function of each key and which can be changed by the user at the
  2240. /// system level. In the input system, the current keyboard layout can be queried via <see cref="Keyboard.keyboardLayout"/>.
  2241. /// When the layout changes at the system level, the input backend sends a configuration change event
  2242. /// to signal that the configuration of the keyboard has changed and that cached data may be outdated.
  2243. /// In response, <see cref="Keyboard"/> will flush out cached information such as the name of the keyboard
  2244. /// layout and display names (<see cref="InputControl.displayName"/>) of individual keys which causes them
  2245. /// to be fetched again from the backend the next time they are accessed.
  2246. /// </remarks>
  2247. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  2248. /// <exception cref="InvalidOperationException"><paramref name="device"/> has not been added
  2249. /// (<see cref="InputDevice.added"/>; <see cref="AddDevice(InputDevice)"/>) and thus cannot
  2250. /// receive events.</exception>
  2251. public static void QueueConfigChangeEvent(InputDevice device, double time = -1)
  2252. {
  2253. if (device == null)
  2254. throw new ArgumentNullException(nameof(device));
  2255. if (device.deviceId == InputDevice.InvalidDeviceId)
  2256. throw new InvalidOperationException("Device has not been added");
  2257. if (time < 0)
  2258. time = InputRuntime.s_Instance.currentTime;
  2259. else
  2260. time += InputRuntime.s_CurrentTimeOffsetToRealtimeSinceStartup;
  2261. var inputEvent = DeviceConfigurationEvent.Create(device.deviceId, time);
  2262. s_Manager.QueueEvent(ref inputEvent);
  2263. }
  2264. /// <summary>
  2265. /// Queue a <see cref="TextEvent"/> on the given device.
  2266. /// </summary>
  2267. /// <param name="device">Device to queue the event on.</param>
  2268. /// <param name="character">Text character to input through the event.</param>
  2269. /// <param name="time">Optional event time stamp. If not supplied, the current time will be used.</param>
  2270. /// <remarks>
  2271. /// Text input is sent to devices character by character. This allows sending strings of arbitrary
  2272. /// length without necessary incurring GC overhead.
  2273. ///
  2274. /// For the event to have any effect on <paramref name="device"/>, the device must
  2275. /// implement <see cref="ITextInputReceiver"/>. It will see <see cref="ITextInputReceiver.OnTextInput"/>
  2276. /// being called when the event is processed.
  2277. /// </remarks>
  2278. /// <exception cref="ArgumentNullException"><paramref name="device"/> is null.</exception>
  2279. /// <exception cref="InvalidOperationException"><paramref name="device"/> is a device that has not been
  2280. /// added to the system.</exception>
  2281. /// <seealso cref="Keyboard.onTextInput"/>
  2282. public static void QueueTextEvent(InputDevice device, char character, double time = -1)
  2283. {
  2284. if (device == null)
  2285. throw new ArgumentNullException(nameof(device));
  2286. if (device.deviceId == InputDevice.InvalidDeviceId)
  2287. throw new InvalidOperationException("Device has not been added");
  2288. if (time < 0)
  2289. time = InputRuntime.s_Instance.currentTime;
  2290. else
  2291. time += InputRuntime.s_CurrentTimeOffsetToRealtimeSinceStartup;
  2292. var inputEvent = TextEvent.Create(device.deviceId, character, time);
  2293. s_Manager.QueueEvent(ref inputEvent);
  2294. }
  2295. ////TODO: rename or move this to a less obvious place
  2296. /// <summary>
  2297. /// Run a single update of input state.
  2298. /// </summary>
  2299. /// <remarks>
  2300. /// Except in tests and when using <see cref="InputSettings.UpdateMode.ProcessEventsManually"/>, this method should not
  2301. /// normally be called. The input system will automatically update as part of the player loop as
  2302. /// determined by <see cref="InputSettings.updateMode"/>. Calling this method is equivalent to
  2303. /// inserting extra frames, i.e. it will advance the entire state of the input system by one complete
  2304. /// frame.
  2305. ///
  2306. /// When using <see cref="InputUpdateType.Manual"/>, this method MUST be called for input to update in the
  2307. /// player. Not calling the method as part of the player loop may result in excessive memory
  2308. /// consumption and/or potential loss of input.
  2309. ///
  2310. /// Each update will flush out buffered input events and cause them to be processed. This in turn
  2311. /// will update the state of input devices (<see cref="InputDevice"/>) and trigger actions (<see cref="InputAction"/>)
  2312. /// that monitor affected device state.
  2313. /// </remarks>
  2314. /// <seealso cref="InputUpdateType"/>
  2315. /// <seealso cref="InputSettings.updateMode"/>
  2316. public static void Update()
  2317. {
  2318. s_Manager.Update();
  2319. }
  2320. internal static void Update(InputUpdateType updateType)
  2321. {
  2322. if (updateType != InputUpdateType.None && (s_Manager.updateMask & updateType) == 0)
  2323. throw new InvalidOperationException(
  2324. $"'{updateType}' updates are not enabled; InputSystem.settings.updateMode is set to '{settings.updateMode}'");
  2325. s_Manager.Update(updateType);
  2326. }
  2327. /// <summary>
  2328. /// Event that is fired before the input system updates.
  2329. /// </summary>
  2330. /// <remarks>
  2331. /// The input system updates in sync with player loop and editor updates. Input updates
  2332. /// are run right before the respective script update. For example, an input update for
  2333. /// <see cref="InputUpdateType.Dynamic"/> is run before <c>MonoBehaviour.Update</c> methods
  2334. /// are executed.
  2335. ///
  2336. /// The update callback itself is triggered before the input system runs its own update and
  2337. /// before it flushes out its event queue. This means that events queued from a callback will
  2338. /// be fed right into the upcoming update.
  2339. /// </remarks>
  2340. /// <seealso cref="onAfterUpdate"/>
  2341. /// <seealso cref="Update"/>
  2342. public static event Action onBeforeUpdate
  2343. {
  2344. add
  2345. {
  2346. lock (s_Manager)
  2347. s_Manager.onBeforeUpdate += value;
  2348. }
  2349. remove
  2350. {
  2351. lock (s_Manager)
  2352. s_Manager.onBeforeUpdate -= value;
  2353. }
  2354. }
  2355. /// <summary>
  2356. /// Event that is fired after the input system has completed an update and processed all pending events.
  2357. /// </summary>
  2358. /// <seealso cref="onBeforeUpdate"/>
  2359. /// <seealso cref="Update"/>
  2360. public static event Action onAfterUpdate
  2361. {
  2362. add
  2363. {
  2364. lock (s_Manager)
  2365. s_Manager.onAfterUpdate += value;
  2366. }
  2367. remove
  2368. {
  2369. lock (s_Manager)
  2370. s_Manager.onAfterUpdate -= value;
  2371. }
  2372. }
  2373. #endregion
  2374. #region Settings
  2375. /// <summary>
  2376. /// The current configuration of the input system.
  2377. /// </summary>
  2378. /// <value>Global configuration object for the input system.</value>
  2379. /// <remarks>
  2380. /// The input system can be configured on a per-project basis. Settings can either be created and
  2381. /// installed on the fly or persisted as assets in the project.
  2382. /// </remarks>
  2383. /// <exception cref="ArgumentNullException">Value is null when setting the property.</exception>
  2384. public static InputSettings settings
  2385. {
  2386. get => s_Manager.settings;
  2387. set
  2388. {
  2389. if (value == null)
  2390. throw new ArgumentNullException(nameof(value));
  2391. if (s_Manager.m_Settings == value)
  2392. return;
  2393. // In the editor, we keep track of the settings asset through EditorBuildSettings.
  2394. #if UNITY_EDITOR
  2395. if (!string.IsNullOrEmpty(AssetDatabase.GetAssetPath(value)))
  2396. {
  2397. EditorBuildSettings.AddConfigObject(InputSettingsProvider.kEditorBuildSettingsConfigKey,
  2398. value, true);
  2399. }
  2400. #endif
  2401. s_Manager.settings = value;
  2402. }
  2403. }
  2404. /// <summary>
  2405. /// Event that is triggered if any of the properties in <see cref="settings"/> changes or if
  2406. /// <see cref="settings"/> is replaced entirely with a new <see cref="InputSettings"/> object.
  2407. /// </summary>
  2408. /// <seealso cref="settings"/>
  2409. /// <seealso cref="InputSettings"/>
  2410. public static event Action onSettingsChange
  2411. {
  2412. add => s_Manager.onSettingsChange += value;
  2413. remove => s_Manager.onSettingsChange -= value;
  2414. }
  2415. #endregion
  2416. #region Actions
  2417. /// <summary>
  2418. /// Event that is signalled when the state of enabled actions in the system changes or
  2419. /// when actions are triggered.
  2420. /// </summary>
  2421. /// <remarks>
  2422. /// The object received by the callback is either an <see cref="InputAction"/>,
  2423. /// <see cref="InputActionMap"/>, or <see cref="InputActionAsset"/> depending on whether the
  2424. /// <see cref="InputActionChange"/> affects a single action, an entire action map, or an
  2425. /// entire action asset.
  2426. ///
  2427. /// For <see cref="InputActionChange.BoundControlsAboutToChange"/> and <see cref="InputActionChange.BoundControlsChanged"/>,
  2428. /// the given object is an <see cref="InputAction"/> if the action is not part of an action map,
  2429. /// an <see cref="InputActionMap"/> if the the actions are part of a map but not part of an asset, and an
  2430. /// <see cref="InputActionAsset"/> if the actions are part of an asset. In other words, the notification is
  2431. /// sent for the topmost object in the hierarchy.
  2432. /// </remarks>
  2433. /// <example>
  2434. /// <code>
  2435. /// InputSystem.onActionChange +=
  2436. /// (obj, change) =>
  2437. /// {
  2438. /// if (change == InputActionChange.ActionPerformed)
  2439. /// {
  2440. /// var action = (InputAction)obj;
  2441. /// var control = action.activeControl;
  2442. /// //...
  2443. /// }
  2444. /// else if (change == InputActionChange.ActionMapEnabled)
  2445. /// {
  2446. /// var actionMap = (InputActionMap)obj;
  2447. /// //...
  2448. /// }
  2449. /// else if (change == InputActionChange.BoundControlsChanged)
  2450. /// {
  2451. /// // This is one way to deal with the fact that obj may be an InputAction
  2452. /// // InputActionMap, or InputActionAsset and may be part of an InputActionAsset or not.
  2453. /// var action = obj as InputAction;
  2454. /// var actionMap = action?.actionMap ?? obj as InputActionMap;
  2455. /// var actionAsset = actionMap?.asset ?? obj as InputActionAsset;
  2456. ///
  2457. /// // Note that if bound controls are changed on any map in an asset, there *will*
  2458. /// // be a BoundControlsChanged notification for the entire asset.
  2459. ///
  2460. /// //...
  2461. /// }
  2462. /// };
  2463. /// </code>
  2464. /// </example>
  2465. /// <seealso cref="InputAction.controls"/>
  2466. public static event Action<object, InputActionChange> onActionChange
  2467. {
  2468. add
  2469. {
  2470. if (value == null)
  2471. throw new ArgumentNullException(nameof(value));
  2472. lock (s_Manager)
  2473. if (!InputActionState.s_OnActionChange.Contains(value))
  2474. InputActionState.s_OnActionChange.Append(value);
  2475. }
  2476. remove
  2477. {
  2478. if (value == null)
  2479. throw new ArgumentNullException(nameof(value));
  2480. lock (s_Manager)
  2481. InputActionState.s_OnActionChange.Remove(value);
  2482. }
  2483. }
  2484. /// <summary>
  2485. /// Register a new type of interaction with the system.
  2486. /// </summary>
  2487. /// <param name="type">Type that implements the interaction. Must support <see cref="InputInteraction"/>.</param>
  2488. /// <param name="name">Name to register the interaction with. This is used in bindings to refer to the interaction
  2489. /// (e.g. an interactions called "Tap" can be added to a binding by listing it in its <see cref="InputBinding.interactions"/>
  2490. /// property). If no name is supplied, the short name of <paramref name="type"/> is used (with "Interaction" clipped off
  2491. /// the name if the type name ends in that).</param>
  2492. /// <example>
  2493. /// <code>
  2494. /// // Interaction that is performed when control resets to default state.
  2495. /// public class ResetInteraction : InputInteraction
  2496. /// {
  2497. /// public void Process(ref InputInteractionContext context)
  2498. /// {
  2499. /// if (context.isWaiting && !context.controlHasDefaultValue)
  2500. /// context.Started();
  2501. /// else if (context.isStarted && context.controlHasDefaultValue)
  2502. /// context.Performed();
  2503. /// }
  2504. /// }
  2505. ///
  2506. /// // Make interaction globally available on bindings.
  2507. /// // "Interaction" suffix in type name will get dropped automatically.
  2508. /// InputSystem.RegisterInteraction(typeof(ResetInteraction));
  2509. ///
  2510. /// // Set up action with binding that has the 'reset' interaction applied to it.
  2511. /// var action = new InputAction(binding: "/&lt;Gamepad>/buttonSouth", interactions: "reset");
  2512. /// </code>
  2513. /// </example>
  2514. /// <seealso cref="IInputInteraction"/>
  2515. /// <seealso cref="RegisterInteraction{T}"/>
  2516. public static void RegisterInteraction(Type type, string name = null)
  2517. {
  2518. if (type == null)
  2519. throw new ArgumentNullException(nameof(type));
  2520. if (string.IsNullOrEmpty(name))
  2521. {
  2522. name = type.Name;
  2523. if (name.EndsWith("Interaction"))
  2524. name = name.Substring(0, name.Length - "Interaction".Length);
  2525. }
  2526. s_Manager.interactions.AddTypeRegistration(name, type);
  2527. }
  2528. public static void RegisterInteraction<T>(string name = null)
  2529. {
  2530. RegisterInteraction(typeof(T), name);
  2531. }
  2532. ////REVIEW: can we move the getters and listers somewhere else? maybe `interactions` and `processors` properties and such?
  2533. public static Type TryGetInteraction(string name)
  2534. {
  2535. if (string.IsNullOrEmpty(name))
  2536. throw new ArgumentNullException(nameof(name));
  2537. return s_Manager.interactions.LookupTypeRegistration(name);
  2538. }
  2539. public static IEnumerable<string> ListInteractions()
  2540. {
  2541. return s_Manager.interactions.names;
  2542. }
  2543. public static void RegisterBindingComposite(Type type, string name)
  2544. {
  2545. if (type == null)
  2546. throw new ArgumentNullException(nameof(type));
  2547. if (string.IsNullOrEmpty(name))
  2548. {
  2549. name = type.Name;
  2550. if (name.EndsWith("Composite"))
  2551. name = name.Substring(0, name.Length - "Composite".Length);
  2552. }
  2553. s_Manager.composites.AddTypeRegistration(name, type);
  2554. }
  2555. public static void RegisterBindingComposite<T>(string name = null)
  2556. {
  2557. RegisterBindingComposite(typeof(T), name);
  2558. }
  2559. public static Type TryGetBindingComposite(string name)
  2560. {
  2561. if (string.IsNullOrEmpty(name))
  2562. throw new ArgumentNullException(nameof(name));
  2563. return s_Manager.composites.LookupTypeRegistration(name);
  2564. }
  2565. /// <summary>
  2566. /// Disable all actions (and implicitly all action sets) that are currently enabled.
  2567. /// </summary>
  2568. /// <seealso cref="ListEnabledActions()"/>
  2569. /// <seealso cref="InputAction.Disable"/>
  2570. public static void DisableAllEnabledActions()
  2571. {
  2572. InputActionState.DisableAllActions();
  2573. }
  2574. /// <summary>
  2575. /// Return a list of all the actions that are currently enabled in the system.
  2576. /// </summary>
  2577. /// <returns>A new list instance containing all currently enabled actions.</returns>
  2578. /// <remarks>
  2579. /// To avoid allocations, use <see cref="ListEnabledActions(List{UnityEngine.InputSystem.InputAction})"/>.
  2580. /// </remarks>
  2581. /// <seealso cref="InputAction.enabled"/>
  2582. public static List<InputAction> ListEnabledActions()
  2583. {
  2584. var result = new List<InputAction>();
  2585. ListEnabledActions(result);
  2586. return result;
  2587. }
  2588. /// <summary>
  2589. /// Add all actions that are currently enabled in the system to the given list.
  2590. /// </summary>
  2591. /// <param name="actions">List to add actions to.</param>
  2592. /// <returns>The number of actions added to the list.</returns>
  2593. /// <exception cref="ArgumentNullException"><paramref name="actions"/> is null.</exception>
  2594. /// <remarks>
  2595. /// If the capacity of the given list is large enough, this method will not allocate memory.
  2596. /// </remarks>
  2597. public static int ListEnabledActions(List<InputAction> actions)
  2598. {
  2599. if (actions == null)
  2600. throw new ArgumentNullException(nameof(actions));
  2601. return InputActionState.FindAllEnabledActions(actions);
  2602. }
  2603. #endregion
  2604. #region Remoting
  2605. /// <summary>
  2606. /// The local InputRemoting instance which can mirror local input to a remote
  2607. /// input system or can make input in a remote system available locally.
  2608. /// </summary>
  2609. /// <remarks>
  2610. /// In the editor, this is always initialized. In players, this will be null
  2611. /// if remoting is disabled (which it is by default in release players).
  2612. /// </remarks>
  2613. public static InputRemoting remoting => s_Remote;
  2614. #endregion
  2615. /// <summary>
  2616. /// The current version of the input system package.
  2617. /// </summary>
  2618. /// <value>Current version of the input system.</value>
  2619. public static Version version => Assembly.GetExecutingAssembly().GetName().Version;
  2620. ////REVIEW: restrict metrics to editor and development builds?
  2621. /// <summary>
  2622. /// Get various up-to-date metrics about the input system.
  2623. /// </summary>
  2624. /// <value>Up-to-date metrics on input system activity.</value>
  2625. public static InputMetrics metrics => s_Manager.metrics;
  2626. internal static InputManager s_Manager;
  2627. internal static InputRemoting s_Remote;
  2628. #if DEVELOPMENT_BUILD || UNITY_EDITOR
  2629. internal static RemoteInputPlayerConnection s_RemoteConnection;
  2630. private static void SetUpRemoting()
  2631. {
  2632. Debug.Assert(s_Manager != null);
  2633. #if UNITY_EDITOR
  2634. s_Remote = new InputRemoting(s_Manager);
  2635. // NOTE: We use delayCall as our initial startup will run in editor initialization before
  2636. // PlayerConnection is itself ready. If we call Bind() directly here, we won't
  2637. // see any errors but the callbacks we register for will not trigger.
  2638. EditorApplication.delayCall += SetUpRemotingInternal;
  2639. #else
  2640. s_Remote = new InputRemoting(s_Manager);
  2641. SetUpRemotingInternal();
  2642. #endif
  2643. }
  2644. private static void SetUpRemotingInternal()
  2645. {
  2646. if (s_RemoteConnection == null)
  2647. {
  2648. #if UNITY_EDITOR
  2649. s_RemoteConnection = RemoteInputPlayerConnection.instance;
  2650. s_RemoteConnection.Bind(EditorConnection.instance, false);
  2651. #else
  2652. s_RemoteConnection = ScriptableObject.CreateInstance<RemoteInputPlayerConnection>();
  2653. s_RemoteConnection.Bind(PlayerConnection.instance, PlayerConnection.instance.isConnected);
  2654. #endif
  2655. }
  2656. s_Remote.Subscribe(s_RemoteConnection); // Feed messages from players into editor.
  2657. s_RemoteConnection.Subscribe(s_Remote); // Feed messages from editor into players.
  2658. }
  2659. #if !UNITY_EDITOR
  2660. private static bool ShouldEnableRemoting()
  2661. {
  2662. ////FIXME: is there a better way to detect whether we are running tests?
  2663. var isRunningTests = Application.productName == "UnityTestFramework";
  2664. if (isRunningTests)
  2665. return false; // Don't remote while running tests.
  2666. return true;
  2667. }
  2668. #endif
  2669. #endif // DEVELOPMENT_BUILD || UNITY_EDITOR
  2670. // The rest here is internal stuff to manage singletons, survive domain reloads,
  2671. // and to support the reset ability for tests.
  2672. static InputSystem()
  2673. {
  2674. #if UNITY_EDITOR
  2675. InitializeInEditor();
  2676. #else
  2677. InitializeInPlayer();
  2678. #endif
  2679. }
  2680. ////FIXME: Unity is not calling this method if it's inside an #if block that is not
  2681. //// visible to the editor; that shouldn't be the case
  2682. [RuntimeInitializeOnLoadMethod(loadType: RuntimeInitializeLoadType.BeforeSceneLoad)]
  2683. private static void RunInitializeInPlayer()
  2684. {
  2685. // We're using this method just to make sure the class constructor is called
  2686. // so we don't need any code in here. When the engine calls this method, the
  2687. // class constructor will be run if it hasn't been run already.
  2688. // IL2CPP has a bug that causes the class constructor to not be run when
  2689. // the RuntimeInitializeOnLoadMethod is invoked. So we need an explicit check
  2690. // here until that is fixed (case 1014293).
  2691. #if !UNITY_EDITOR
  2692. if (s_Manager == null)
  2693. InitializeInPlayer();
  2694. #endif
  2695. }
  2696. #if UNITY_EDITOR
  2697. internal static InputSystemObject s_SystemObject;
  2698. internal static void InitializeInEditor(IInputRuntime runtime = null)
  2699. {
  2700. Profiling.Profiler.BeginSample("InputSystem.InitializeInEditor");
  2701. Reset(runtime: runtime);
  2702. var existingSystemObjects = Resources.FindObjectsOfTypeAll<InputSystemObject>();
  2703. if (existingSystemObjects != null && existingSystemObjects.Length > 0)
  2704. {
  2705. ////FIXME: does not preserve action map state
  2706. // We're coming back out of a domain reload. We're restoring part of the
  2707. // InputManager state here but we're still waiting from layout registrations
  2708. // that happen during domain initialization.
  2709. s_SystemObject = existingSystemObjects[0];
  2710. s_Manager.RestoreStateWithoutDevices(s_SystemObject.systemState.managerState);
  2711. InputDebuggerWindow.ReviveAfterDomainReload();
  2712. // Restore remoting state.
  2713. s_RemoteConnection = s_SystemObject.systemState.remoteConnection;
  2714. SetUpRemoting();
  2715. s_Remote.RestoreState(s_SystemObject.systemState.remotingState, s_Manager);
  2716. // Get manager to restore devices on first input update. By that time we
  2717. // should have all (possibly updated) layout information in place.
  2718. s_Manager.m_SavedDeviceStates = s_SystemObject.systemState.managerState.devices;
  2719. s_Manager.m_SavedAvailableDevices = s_SystemObject.systemState.managerState.availableDevices;
  2720. // Restore editor settings.
  2721. InputEditorUserSettings.s_Settings = s_SystemObject.systemState.userSettings;
  2722. // Get rid of saved state.
  2723. s_SystemObject.systemState = new State();
  2724. }
  2725. else
  2726. {
  2727. s_SystemObject = ScriptableObject.CreateInstance<InputSystemObject>();
  2728. s_SystemObject.hideFlags = HideFlags.HideAndDontSave;
  2729. // See if we have a remembered settings object.
  2730. if (EditorBuildSettings.TryGetConfigObject(InputSettingsProvider.kEditorBuildSettingsConfigKey,
  2731. out InputSettings settingsAsset))
  2732. {
  2733. if (s_Manager.m_Settings.hideFlags == HideFlags.HideAndDontSave)
  2734. ScriptableObject.DestroyImmediate(s_Manager.m_Settings);
  2735. s_Manager.m_Settings = settingsAsset;
  2736. s_Manager.ApplySettings();
  2737. }
  2738. InputEditorUserSettings.Load();
  2739. SetUpRemoting();
  2740. }
  2741. Debug.Assert(settings != null);
  2742. #if UNITY_EDITOR
  2743. Debug.Assert(EditorUtility.InstanceIDToObject(settings.GetInstanceID()) != null,
  2744. "InputSettings has lost its native object");
  2745. #endif
  2746. // If native backends for new input system aren't enabled, ask user whether we should
  2747. // enable them (requires restart). We only ask once per session and don't ask when
  2748. // running in batch mode.
  2749. if (!s_SystemObject.newInputBackendsCheckedAsEnabled &&
  2750. !EditorPlayerSettingHelpers.newSystemBackendsEnabled &&
  2751. !s_Manager.m_Runtime.isInBatchMode)
  2752. {
  2753. const string dialogText = "This project is using the new input system package but the native platform backends for the new input system are not enabled in the player settings. " +
  2754. "This means that no input from native devices will come through." +
  2755. "\n\nDo you want to enable the backends? Doing so will *RESTART* the editor and will *DISABLE* the old UnityEngine.Input APIs.";
  2756. if (EditorUtility.DisplayDialog("Warning", dialogText, "Yes", "No"))
  2757. {
  2758. EditorPlayerSettingHelpers.newSystemBackendsEnabled = true;
  2759. EditorPlayerSettingHelpers.oldSystemBackendsEnabled = false;
  2760. EditorHelpers.RestartEditorAndRecompileScripts();
  2761. }
  2762. }
  2763. s_SystemObject.newInputBackendsCheckedAsEnabled = true;
  2764. RunInitialUpdate();
  2765. Profiling.Profiler.EndSample();
  2766. }
  2767. internal static void OnPlayModeChange(PlayModeStateChange change)
  2768. {
  2769. ////REVIEW: should we pause haptics when play mode is paused and stop haptics when play mode is exited?
  2770. switch (change)
  2771. {
  2772. case PlayModeStateChange.ExitingEditMode:
  2773. s_SystemObject.settings = JsonUtility.ToJson(settings);
  2774. s_SystemObject.exitEditModeTime = InputRuntime.s_Instance.currentTime;
  2775. s_SystemObject.enterPlayModeTime = 0;
  2776. break;
  2777. case PlayModeStateChange.EnteredPlayMode:
  2778. s_SystemObject.enterPlayModeTime = InputRuntime.s_Instance.currentTime;
  2779. break;
  2780. ////TODO: also nuke all callbacks installed on InputActions and InputActionMaps
  2781. ////REVIEW: is there any other cleanup work we want to before? should we automatically nuke
  2782. //// InputDevices that have been created with AddDevice<> during play mode?
  2783. case PlayModeStateChange.EnteredEditMode:
  2784. // Nuke all InputActionMapStates. Releases their unmanaged memory.
  2785. InputActionState.DestroyAllActionMapStates();
  2786. // Restore settings.
  2787. if (!string.IsNullOrEmpty(s_SystemObject.settings))
  2788. {
  2789. JsonUtility.FromJsonOverwrite(s_SystemObject.settings, settings);
  2790. s_SystemObject.settings = null;
  2791. settings.OnChange();
  2792. }
  2793. break;
  2794. }
  2795. }
  2796. private static void OnProjectChange()
  2797. {
  2798. ////TODO: use dirty count to find whether settings have actually changed
  2799. // May have added, removed, moved, or renamed settings asset. Force a refresh
  2800. // of the UI.
  2801. InputSettingsProvider.ForceReload();
  2802. // Also, if the asset holding our current settings got deleted, switch back to a
  2803. // temporary settings object.
  2804. // NOTE: We access m_Settings directly here to make sure we're not running into asserts
  2805. // from the settings getter checking it has a valid object.
  2806. if (EditorUtility.InstanceIDToObject(s_Manager.m_Settings.GetInstanceID()) == null)
  2807. {
  2808. var newSettings = ScriptableObject.CreateInstance<InputSettings>();
  2809. newSettings.hideFlags = HideFlags.HideAndDontSave;
  2810. settings = newSettings;
  2811. }
  2812. }
  2813. #else
  2814. private static void InitializeInPlayer(IInputRuntime runtime = null, InputSettings settings = null)
  2815. {
  2816. if (settings == null)
  2817. settings = Resources.FindObjectsOfTypeAll<InputSettings>().FirstOrDefault() ?? ScriptableObject.CreateInstance<InputSettings>();
  2818. // No domain reloads in the player so we don't need to look for existing
  2819. // instances.
  2820. s_Manager = new InputManager();
  2821. s_Manager.Initialize(runtime ?? NativeInputRuntime.instance, settings);
  2822. #if !UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION
  2823. PerformDefaultPluginInitialization();
  2824. #endif
  2825. // Automatically enable remoting in development players.
  2826. #if DEVELOPMENT_BUILD
  2827. if (ShouldEnableRemoting())
  2828. SetUpRemoting();
  2829. #endif
  2830. RunInitialUpdate();
  2831. }
  2832. #endif // UNITY_EDITOR
  2833. private static void RunInitialUpdate()
  2834. {
  2835. // Request an initial Update so that user methods such as Start and Awake
  2836. // can access the input devices.
  2837. //
  2838. // NOTE: We use InputUpdateType.None here to run a "null" update. InputManager.OnBeforeUpdate()
  2839. // and InputManager.OnUpdate() will both early out when comparing this to their update
  2840. // mask but will still restore devices. This means we're not actually processing input,
  2841. // but we will force the runtime to push its devices.
  2842. Update(InputUpdateType.None);
  2843. }
  2844. #if !UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION
  2845. private static void PerformDefaultPluginInitialization()
  2846. {
  2847. UISupport.Initialize();
  2848. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA || UNITY_IOS
  2849. XInputSupport.Initialize();
  2850. #endif
  2851. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_PS4 || UNITY_WSA || UNITY_IOS
  2852. DualShockSupport.Initialize();
  2853. #endif
  2854. #if UNITY_EDITOR || UNITY_STANDALONE || UNITY_WSA
  2855. HIDSupport.Initialize();
  2856. #endif
  2857. #if UNITY_EDITOR || UNITY_ANDROID
  2858. Android.AndroidSupport.Initialize();
  2859. #endif
  2860. #if UNITY_EDITOR || UNITY_IOS || UNITY_TVOS
  2861. iOS.iOSSupport.Initialize();
  2862. #endif
  2863. #if UNITY_EDITOR || UNITY_WEBGL
  2864. WebGL.WebGLSupport.Initialize();
  2865. #endif
  2866. #if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WSA
  2867. Switch.SwitchSupportHID.Initialize();
  2868. #endif
  2869. #if (UNITY_EDITOR || UNITY_STANDALONE || UNITY_ANDROID || UNITY_IOS || UNITY_WSA) && UNITY_INPUT_SYSTEM_ENABLE_XR
  2870. XR.XRSupport.Initialize();
  2871. #endif
  2872. #if UNITY_EDITOR || UNITY_STANDALONE_LINUX
  2873. Linux.LinuxSupport.Initialize();
  2874. #endif
  2875. #if UNITY_EDITOR || UNITY_ANDROID || UNITY_IOS || UNITY_TVOS || UNITY_WSA
  2876. OnScreen.OnScreenSupport.Initialize();
  2877. #endif
  2878. #if (UNITY_EDITOR || UNITY_STANDALONE) && UNITY_ENABLE_STEAM_CONTROLLER_SUPPORT
  2879. Steam.SteamSupport.Initialize();
  2880. #endif
  2881. }
  2882. #endif // UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION
  2883. // For testing, we want the ability to push/pop system state even in the player.
  2884. // However, we don't want it in release players.
  2885. #if DEVELOPMENT_BUILD || UNITY_EDITOR
  2886. /// <summary>
  2887. /// Return the input system to its default state.
  2888. /// </summary>
  2889. private static void Reset(bool enableRemoting = false, IInputRuntime runtime = null)
  2890. {
  2891. Profiling.Profiler.BeginSample("InputSystem.Reset");
  2892. // Some devices keep globals. Get rid of them by pretending the devices
  2893. // are removed.
  2894. if (s_Manager != null)
  2895. {
  2896. foreach (var device in s_Manager.devices)
  2897. device.NotifyRemoved();
  2898. s_Manager.UninstallGlobals();
  2899. }
  2900. // Create temporary settings. In the tests, this is all we need. But outside of tests,d
  2901. // this should get replaced with an actual InputSettings asset.
  2902. var settings = ScriptableObject.CreateInstance<InputSettings>();
  2903. settings.hideFlags = HideFlags.HideAndDontSave;
  2904. #if UNITY_EDITOR
  2905. s_Manager = new InputManager();
  2906. s_Manager.Initialize(runtime ?? NativeInputRuntime.instance, settings);
  2907. s_Manager.m_Runtime.onPlayModeChanged = OnPlayModeChange;
  2908. s_Manager.m_Runtime.onProjectChange = OnProjectChange;
  2909. InputEditorUserSettings.s_Settings = new InputEditorUserSettings.SerializedState();
  2910. if (enableRemoting)
  2911. SetUpRemoting();
  2912. #if !UNITY_DISABLE_DEFAULT_INPUT_PLUGIN_INITIALIZATION
  2913. PerformDefaultPluginInitialization();
  2914. #endif
  2915. #else
  2916. InitializeInPlayer(runtime, settings);
  2917. #endif
  2918. InputUser.ResetGlobals();
  2919. Profiling.Profiler.EndSample();
  2920. }
  2921. /// <summary>
  2922. /// Destroy the current setup of the input system.
  2923. /// </summary>
  2924. /// <remarks>
  2925. /// NOTE: This also de-allocates data we're keeping in unmanaged memory!
  2926. /// </remarks>
  2927. private static void Destroy()
  2928. {
  2929. // NOTE: Does not destroy InputSystemObject. We want to destroy input system
  2930. // state repeatedly during tests but we want to not create InputSystemObject
  2931. // over and over.
  2932. InputActionState.ResetGlobals();
  2933. s_Manager.Destroy();
  2934. if (s_RemoteConnection != null)
  2935. Object.DestroyImmediate(s_RemoteConnection);
  2936. #if UNITY_EDITOR
  2937. EditorInputControlLayoutCache.Clear();
  2938. InputDeviceDebuggerWindow.s_OnToolbarGUIActions.Clear();
  2939. InputEditorUserSettings.s_Settings = new InputEditorUserSettings.SerializedState();
  2940. #endif
  2941. s_Manager = null;
  2942. s_RemoteConnection = null;
  2943. s_Remote = null;
  2944. }
  2945. /// <summary>
  2946. /// Snapshot of the state used by the input system.
  2947. /// </summary>
  2948. /// <remarks>
  2949. /// Can be taken across domain reloads.
  2950. /// </remarks>
  2951. [Serializable]
  2952. internal struct State
  2953. {
  2954. [NonSerialized] public InputManager manager;
  2955. [NonSerialized] public InputRemoting remote;
  2956. [SerializeField] public RemoteInputPlayerConnection remoteConnection;
  2957. [SerializeField] public InputManager.SerializedState managerState;
  2958. [SerializeField] public InputRemoting.SerializedState remotingState;
  2959. #if UNITY_EDITOR
  2960. [SerializeField] public InputEditorUserSettings.SerializedState userSettings;
  2961. [SerializeField] public string systemObject;
  2962. #endif
  2963. ////REVIEW: preserve InputUser state? (if even possible)
  2964. }
  2965. private static Stack<State> s_SavedStateStack;
  2966. internal static State GetSavedState()
  2967. {
  2968. return s_SavedStateStack.Peek();
  2969. }
  2970. /// <summary>
  2971. /// Push the current state of the input system onto a stack and
  2972. /// reset the system to its default state.
  2973. /// </summary>
  2974. /// <remarks>
  2975. /// The save stack is not able to survive domain reloads. It is intended solely
  2976. /// for use in tests.
  2977. /// </remarks>
  2978. internal static void SaveAndReset(bool enableRemoting = false, IInputRuntime runtime = null)
  2979. {
  2980. if (s_SavedStateStack == null)
  2981. s_SavedStateStack = new Stack<State>();
  2982. ////FIXME: does not preserve global state in InputActionState
  2983. ////TODO: preserve InputUser state
  2984. s_SavedStateStack.Push(new State
  2985. {
  2986. manager = s_Manager,
  2987. remote = s_Remote,
  2988. remoteConnection = s_RemoteConnection,
  2989. managerState = s_Manager.SaveState(),
  2990. remotingState = s_Remote?.SaveState() ?? new InputRemoting.SerializedState(),
  2991. #if UNITY_EDITOR
  2992. userSettings = InputEditorUserSettings.s_Settings,
  2993. systemObject = JsonUtility.ToJson(s_SystemObject),
  2994. #endif
  2995. });
  2996. Reset(enableRemoting, runtime ?? InputRuntime.s_Instance); // Keep current runtime.
  2997. }
  2998. ////FIXME: this method doesn't restore things like InputDeviceDebuggerWindow.onToolbarGUI
  2999. /// <summary>
  3000. /// Restore the state of the system from the last state pushed with <see cref="SaveAndReset"/>.
  3001. /// </summary>
  3002. internal static void Restore()
  3003. {
  3004. Debug.Assert(s_SavedStateStack != null && s_SavedStateStack.Count > 0);
  3005. // Nuke what we have.
  3006. Destroy();
  3007. // Load back previous state.
  3008. var state = s_SavedStateStack.Pop();
  3009. s_Manager = state.manager;
  3010. s_Remote = state.remote;
  3011. s_RemoteConnection = state.remoteConnection;
  3012. InputUpdate.Restore(state.managerState.updateState);
  3013. s_Manager.InstallRuntime(s_Manager.m_Runtime);
  3014. s_Manager.InstallGlobals();
  3015. s_Manager.ApplySettings();
  3016. #if UNITY_EDITOR
  3017. InputEditorUserSettings.s_Settings = state.userSettings;
  3018. JsonUtility.FromJsonOverwrite(state.systemObject, s_SystemObject);
  3019. #endif
  3020. // Get devices that keep global lists (like Gamepad) to re-initialize them
  3021. // by pretending the devices have been added.
  3022. foreach (var device in devices)
  3023. device.NotifyAdded();
  3024. }
  3025. #endif
  3026. }
  3027. }