CustomComposite.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. using UnityEngine.InputSystem.Layouts;
  4. using UnityEngine.InputSystem.Utilities;
  5. #if UNITY_EDITOR
  6. using UnityEditor;
  7. using UnityEngine.InputSystem.Editor;
  8. #endif
  9. // Let's say we want to have a composite that takes an axis and uses
  10. // it's value to multiply the length of a vector from a stick. This could
  11. // be used, for example, to have the right trigger on the gamepad act as
  12. // a strength multiplier on the value of the left stick.
  13. //
  14. // We start by creating a class that is based on InputBindingComposite<>.
  15. // The type we give it is the type of value that we will compute. In this
  16. // case, we will consume a Vector2 from the stick so that is the type
  17. // of value we return.
  18. //
  19. // NOTE: By advertising the type of value we return, we also allow the
  20. // input system to filter out our composite if it is not applicable
  21. // to a specific type of action. For example, if an action is set
  22. // to "Value" as its type and its "Control Type" is set to "Axis",
  23. // our composite will not be shown as our value type (Vector2) is
  24. // incompatible with the value type of Axis (float).
  25. //
  26. // Also, we need to register our composite with the input system. And we
  27. // want to do it in a way that makes the composite visible in the action
  28. // editor of the input system.
  29. //
  30. // For that to happen, we need to call InputSystem.RegisterBindingComposite
  31. // sometime during startup. We make that happen by using [InitializeOnLoad]
  32. // in the editor and [RuntimeInitializeOnLoadMethod] in the player.
  33. #if UNITY_EDITOR
  34. [InitializeOnLoad]
  35. #endif
  36. // We can customize the way display strings are formed for our composite by
  37. // annotating it with DisplayStringFormatAttribute. The string is simply a
  38. // list with elements to be replaced enclosed in curly braces. Everything
  39. // outside those will taken verbatim. The fragments inside the curly braces
  40. // in this case refer to the binding composite parts by name. Each such
  41. // instance is replaced with the display text for the corresponding
  42. // part binding.
  43. [DisplayStringFormat("{multiplier}*{stick}")]
  44. public class CustomComposite : InputBindingComposite<Vector2>
  45. {
  46. // In the editor, the static class constructor will be called on startup
  47. // because of [InitializeOnLoad].
  48. #if UNITY_EDITOR
  49. static CustomComposite()
  50. {
  51. // Trigger our RegisterBindingComposite code in the editor.
  52. Initialize();
  53. }
  54. #endif
  55. // In the player, [RuntimeInitializeOnLoadMethod] will make sure our
  56. // initialization code gets called during startup.
  57. [RuntimeInitializeOnLoadMethod]
  58. private static void Initialize()
  59. {
  60. // This registers the composite with the input system. After calling this
  61. // method, we can have bindings reference the composite. Also, the
  62. // composite will show up in the action editor.
  63. //
  64. // NOTE: We don't supply a name for the composite here. The default logic
  65. // will take the name of the type ("CustomComposite" in our case)
  66. // and snip off "Composite" if used as a suffix (which is the case
  67. // for us) and then use that as the name. So in our case, we are
  68. // registering a composite called "Custom" here.
  69. //
  70. // If we were to use our composite with the AddCompositeBinding API,
  71. // for example, it would look like this:
  72. //
  73. // myAction.AddCompositeBinding("Custom")
  74. // .With("Stick", "<Gamepad>/leftStick")
  75. // .With("Multiplier", "<Gamepad>/rightTrigger");
  76. InputSystem.RegisterBindingComposite<CustomComposite>();
  77. }
  78. // So, we need two parts for our composite. The part that delivers the stick
  79. // value and the part that delivers the axis multiplier. Note that each part
  80. // may be bound to multiple controls. The input system handles that for us
  81. // by giving us an integer identifier for each part that reads a single value
  82. // from however many controls are bound to the part.
  83. //
  84. // In our case, this could be used, for example, to bind the "multiplier" part
  85. // to both the left and the right trigger on the gamepad.
  86. // To tell the input system of a "part" binding that we need for a composite,
  87. // we add a public field with an "int" type and annotated with an [InputControl]
  88. // attribute. We set the "layout" property on the attribute to tell the system
  89. // what kind of control we expect to be bound to the part.
  90. //
  91. // NOTE: These part binding need to be *public fields* for the input system
  92. // to find them.
  93. //
  94. // So this is introduces a part to the composite called "multiplier" and
  95. // expecting an "Axis" control. The value of the field will be set by the
  96. // input system. It will be some internal, unique numeric ID for the part
  97. // which we can then use with InputBindingCompositeContext.ReadValue to
  98. // read out the value of just that part.
  99. [InputControl(layout = "Axis")]
  100. public int multiplier;
  101. // The other part we need is for the stick.
  102. //
  103. // NOTE: We could use "Stick" here but "Vector2" is a little less restrictive.
  104. [InputControl(layout = "Vector2")]
  105. public int stick;
  106. // We may also expose "parameters" on our composite. These can be configured
  107. // graphically in the action editor and also through AddCompositeBinding.
  108. //
  109. // Let's say we want to allow the user to specify an additional scale factor
  110. // to apply to the value of "multiplier". We can do so by simply adding a
  111. // public field of type float. Any public field that is not annotated with
  112. // [InputControl] will be treated as a possible parameter.
  113. //
  114. // If we added a composite with AddCompositeBinding, we could configure the
  115. // parameter like so:
  116. //
  117. // myAction.AddCompositeBinding("Custom(scaleFactor=0.5)"
  118. // .With("Multiplier", "<Gamepad>/rightTrigger")
  119. // .With("Stick", "<Gamepad>/leftStick");
  120. public float scaleFactor = 1;
  121. // Ok, so now we have all the configuration in place. The final piece we
  122. // need is the actual logic that reads input from "multiplier" and "stick"
  123. // and computes a final input value.
  124. //
  125. // We can do that by defining a ReadValue method which is the actual workhorse
  126. // for our composite.
  127. public override Vector2 ReadValue(ref InputBindingCompositeContext context)
  128. {
  129. // We read input from the parts we have by simply
  130. // supplying the part IDs that the input system has set up
  131. // for us to ReadValue.
  132. //
  133. // NOTE: Vector2 is a less straightforward than primitive value types
  134. // like int and float. If there are multiple controls bound to the
  135. // "stick" part, we need to tell the input system which one to pick.
  136. // We do so by giving it an IComparer. In this case, we choose
  137. // Vector2MagnitudeComparer to return the Vector2 with the greatest
  138. // length.
  139. var stickValue = context.ReadValue<Vector2, Vector2MagnitudeComparer>(stick);
  140. var multiplierValue = context.ReadValue<float>(multiplier);
  141. // The rest is simple. We just scale the vector we read by the
  142. // multiple from the axis and apply our scale factor.
  143. return stickValue * (multiplierValue * scaleFactor);
  144. }
  145. }
  146. // Our custom composite is complete and fully functional. We could stop here and
  147. // call it a day. However, for the sake of demonstration, let's say we also want
  148. // to customize how the parameters for our composite are edited. We have "scaleFactor"
  149. // so let's say we want to replace the default float inspector with a slider.
  150. //
  151. // We can replace the default UI by simply deriving a custom InputParameterEditor
  152. // for our composite.
  153. #if UNITY_EDITOR
  154. public class CustomCompositeEditor : InputParameterEditor<CustomComposite>
  155. {
  156. public override void OnGUI()
  157. {
  158. // Using the 'target' property, we can access an instance of our composite.
  159. var currentValue = target.scaleFactor;
  160. // The easiest way to lay out our UI is to simply use EditorGUILayout.
  161. // We simply assign the changed value back to the 'target' object. The input
  162. // system will automatically detect a change in value.
  163. target.scaleFactor = EditorGUILayout.Slider(m_ScaleFactorLabel, currentValue, 0, 2);
  164. }
  165. private GUIContent m_ScaleFactorLabel = new GUIContent("Scale Factor");
  166. }
  167. #endif