Skip to content

Adding TwoAxisVector2Composite for Legacy Joysticks with Twin Dual Axes#2363

Open
vulture-boy wants to merge 2 commits intoUnity-Technologies:developfrom
vulture-boy:develop
Open

Adding TwoAxisVector2Composite for Legacy Joysticks with Twin Dual Axes#2363
vulture-boy wants to merge 2 commits intoUnity-Technologies:developfrom
vulture-boy:develop

Conversation

@vulture-boy
Copy link

Description

This adds a new TwoAxisVector2Composite class, an ImputBindingComposite that converts two Axis values into a Vector2.
Some old Joystick devices don't support Vector2 output for a second Stick but do provide two Axis values representing it, so this class was made for such purposes. I am currently using it to support a PS2 controller hooked up to a third party PS2-to-PC adapter found at a thrift store; I imagine adding this class would contribute to improving developer accessibility associated with income brackets and help solve headaches sooner regarding broader controller compatibility with Unity products.

Testing status & QA

This has been tested in my unity project. It's based off the documented (and presumably tested) CustomComposite script here , with the only major change being the removal of the [InitializeOnLoad] attribute to match the pattern of other built-in composites already in this repository. I have not tested it as part of this base repository but based on comparable Composite scripts it seems unlikely to cause problems.

Overall Product Risks

Very low risk; this feature doesn't affect other aspects of the software dramatically and has very barebones functionality. It is an added utility that may require documentation updating however.

  • Complexity: Low
  • Halo Effect: Low

Comments to reviewers

N/A

Checklist

Before review:

  • Changelog entry added.
    • Explains the change in Changed, Fixed, Added sections.
    • For API change contains an example snippet and/or migration example.
    • JIRA ticket linked, example (case %%). If it is a private issue, just add the case ID without a link.
    • Jira port for the next release set as "Resolved".
  • Tests added/changed, if applicable.
    • Functional tests Area_CanDoX, Area_CanDoX_EvenIfYIsTheCase, Area_WhenIDoX_AndYHappens_ThisIsTheResult.
    • Performance tests.
    • Integration tests.
  • Docs for new/changed API's.
    • Xmldoc cross references are set correctly.
    • Added explanation how the API works.
    • Usage code examples added.
    • The manual is updated, if needed.

During merge:

  • Commit message for squash-merge is prefixed with one of the list:
    • NEW: ___.
    • FIX: ___.
    • DOCS: ___.
    • CHANGE: ___.
    • RELEASE: 1.1.0-preview.3.

@cla-assistant-unity
Copy link

cla-assistant-unity bot commented Mar 1, 2026

CLA assistant check
All committers have signed the CLA.

@u-pr
Copy link
Contributor

u-pr bot commented Mar 1, 2026

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪

Small PR, but the new composite file likely has build/compile-configuration issues that need validation across Editor and Player.

🏅 Score: 70

The feature is straightforward, but the current #if UNITY_EDITOR wrapping of using directives will likely break non-Editor builds.

🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Build Break

The using directives needed for Vector2, InputBindingComposite<T>, and InputSystem are wrapped in #if UNITY_EDITOR, so Player/non-Editor builds may fail to compile; only using UnityEditor; should be Editor-guarded (or fully-qualify types).

#if UNITY_EDITOR
using UnityEditor;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Layouts;
using UnityEngine.InputSystem.Utilities;


#endif

namespace UnityEngine.InputSystem.Composites
{
Composite Contract

Consider whether the composite should also override EvaluateMagnitude(ref InputBindingCompositeContext) (commonly used by the Input System for actuation/magnitude decisions) to ensure correct behavior in interactions that depend on magnitude.

public class TwoAxisVector2Composite : InputBindingComposite<Vector2>
{
    [InputControl(layout = "Axis")]
    public int xAxis;

    [InputControl(layout = "Axis")]
    public int yAxis;

    public override Vector2 ReadValue(ref InputBindingCompositeContext context)
    {
        var firstPartValue = context.ReadValue<float>(xAxis);
        var secondPartValue = context.ReadValue<float>(yAxis);

        return new(firstPartValue, secondPartValue);
    }
Changelog Quality

The changelog entry is very minimal; consider aligning it with repo conventions (punctuation, link/reference if required, and briefly stating intent/usage) so it’s useful for release notes.

### Changed

### Added
- Added TwoAxisVector2Composite
  • Update review

🤖 Helpful? Please react with 👍/👎 | Questions❓Please reach out in Slack #ask-u-pr

@u-pr
Copy link
Contributor

u-pr bot commented Mar 1, 2026

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
Possible issue
Fix player-build compilation

The #if UNITY_EDITOR guard currently removes essential using directives in player
builds, which will break compilation (e.g., Vector2 will be unresolved). Keep only
using UnityEditor; behind the editor guard (or remove it entirely) and move the
runtime using directives outside the conditional.

Packages/com.unity.inputsystem/InputSystem/Actions/Composites/TwoAxisVector2Composite.cs [1-9]

-#if UNITY_EDITOR
-using UnityEditor;
-using UnityEngine;
+using UnityEngine;
 using UnityEngine.InputSystem;
 using UnityEngine.InputSystem.Layouts;
-using UnityEngine.InputSystem.Utilities;
 
-
+#if UNITY_EDITOR
+using UnityEditor;
 #endif
Suggestion importance[1-10]: 9

__

Why: The current code wraps essential namespaces like UnityEngine and UnityEngine.InputSystem inside a #if UNITY_EDITOR block. This will cause compilation errors in standalone builds where UNITY_EDITOR is not defined, as types like Vector2 and InputBindingComposite will be missing.

High
Prevent stripping and ensure registration

This composite can be stripped in IL2CPP/managed-stripping builds because it may
only be referenced by name in bindings; then registration never runs and bindings
fail at runtime. Mark the type as preserved and register in Init() directly so the
runtime initialization path is explicit and survives stripping.

Packages/com.unity.inputsystem/InputSystem/Actions/Composites/TwoAxisVector2Composite.cs [22-45]

+[UnityEngine.Scripting.Preserve]
 public class TwoAxisVector2Composite : InputBindingComposite<Vector2>
 {
     [InputControl(layout = "Axis")]
     public int xAxis;
 
     [InputControl(layout = "Axis")]
     public int yAxis;
 
     public override Vector2 ReadValue(ref InputBindingCompositeContext context)
     {
         var firstPartValue = context.ReadValue<float>(xAxis);
         var secondPartValue = context.ReadValue<float>(yAxis);
 
-        return new(firstPartValue, secondPartValue);
+        return new Vector2(firstPartValue, secondPartValue);
     }
 
-    static TwoAxisVector2Composite()
+    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
+    [UnityEngine.Scripting.Preserve]
+    static void Init()
     {
         InputSystem.RegisterBindingComposite<TwoAxisVector2Composite>();
     }
-
-    [RuntimeInitializeOnLoadMethod]
-    static void Init() { } // Trigger static constructor.
 }
Suggestion importance[1-10]: 7

__

Why: Adding [Preserve] and moving the registration to an explicit Init method helps prevent Unity's managed code stripping from removing the class in IL2CPP builds, ensuring the composite is correctly registered at runtime.

Medium
  • More suggestions

🤖 Helpful? Please react with 👍/👎 | Questions❓Please reach out in Slack #ask-u-pr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant