Skip to content

Commit

Permalink
Merge pull request #3 from eviltwo/1.0.0-dev
Browse files Browse the repository at this point in the history
1.0.0 dev
  • Loading branch information
eviltwo authored Jul 13, 2024
2 parents 5333fc0 + 89ffba8 commit 8856d21
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ Change controller input from Unity(InputSystem) to Steam(InputAction). You can g
This package is useful if you want to use the InputSystem but partially use SteamInput.
For example, you can generate button information to pass to `SteamInput.GetGlyphPNGForActionOrigin()`.

![image](https://github.com/eviltwo/UnitySteamInputAdapter/assets/7721151/73e78a15-4096-4467-8a72-d89027b821fb)
Check out [InputGlyphs](https://github.com/eviltwo/InputGlyphs) that use this package!

Please note that we have not tested this with all controllers. Xbox controllers and PS5 controllers have been tested.
![image](https://github.com/eviltwo/UnitySteamInputAdapter/assets/7721151/73e78a15-4096-4467-8a72-d89027b821fb)

# Require packages
- InputSystem (Unity)
Expand Down
2 changes: 2 additions & 0 deletions UnitySteamInputAdapter/Assets/Samples/Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public class Sample : MonoBehaviour
private void Awake()
{
SteamAPI.Init();
SteamInput.Init(false);
}

private void OnDestroy()
{
SteamInput.Shutdown();
SteamAPI.Shutdown();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [1.0.0] - 2024-07-13
### Fixed
- Fixed to recognize gamepads when the user has Steam Input enabled.

## [0.9.2] - 2024-07-03
### Added
- Convert steam input action from control path.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if SUPPORT_INPUTSYSTEM && SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
using System;
using Steamworks;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.DualShock;
using UnityEngine.InputSystem.Switch;
Expand Down Expand Up @@ -81,6 +83,11 @@ public static EInputActionOrigin GetSteamInputAction(InputDevice inputDevice, st
/// <returns>Steam InputType. If conversion fails, <see cref="ESteamInputType.k_ESteamInputType_Unknown"/> is returned.</returns>
public static ESteamInputType GetSteamInputDevice(InputDevice inputDevice)
{
if (TryGetHijackedSteamInputDevice(inputDevice, out var result))
{
return result;
}

switch (inputDevice)
{
case XInputController:
Expand All @@ -103,6 +110,76 @@ public static ESteamInputType GetSteamInputDevice(InputDevice inputDevice)
}
}

private static readonly InputHandle_t[] InputHandleBuffer = new InputHandle_t[Constants.STEAM_INPUT_MAX_COUNT];

[Serializable]
private class Capabilities
{
public const int InvalidValue = -1;
public int userIndex = InvalidValue;
}

/// <summary>
/// If the user enables Steam Input, all gamepads will be overridden to XInput.
/// This function retrieves the type of gamepad before it is overridden.
/// </summary>
/// <remarks>
/// If multiple gamepads are connected, the type of gamepad returned by this function might be swapped.
/// Only Steam can improve this, and there is nothing that Unity or we can do about it.
/// Users can resolve this issue by disabling Steam Input. Alternatively, restarting the game or unplugging and replugging all the gamepads may solve the problem.
/// </remarks>
public static bool TryGetHijackedSteamInputDevice(InputDevice inputDevice, out ESteamInputType result)
{
if (inputDevice is not XInputController)
{
result = ESteamInputType.k_ESteamInputType_Unknown;
return false;
}

var steamDeviceCount = SteamInput.GetConnectedControllers(InputHandleBuffer);
if (steamDeviceCount == 0)
{
result = ESteamInputType.k_ESteamInputType_Unknown;
return false;
}

var capabilities = inputDevice.description.capabilities;
if (string.IsNullOrEmpty(capabilities))
{
result = ESteamInputType.k_ESteamInputType_Unknown;
return false;
}

Capabilities capabilitiesValue;
try
{
capabilitiesValue = JsonUtility.FromJson<Capabilities>(capabilities);
}
catch (Exception e)
{
Debug.LogException(e);
result = ESteamInputType.k_ESteamInputType_Unknown;
return false;
}

if (capabilities != null && capabilitiesValue.userIndex != Capabilities.InvalidValue)
{
for (int i = 0; i < steamDeviceCount; i++)
{
var inputHandle = InputHandleBuffer[i];
var steamDeviceIndex = SteamInput.GetGamepadIndexForController(inputHandle);
if (steamDeviceIndex == capabilitiesValue.userIndex)
{
result = SteamInput.GetInputTypeForHandle(inputHandle);
return true;
}
}
}

result = ESteamInputType.k_ESteamInputType_Unknown;
return false;
}

/// <summary>
/// Get SteamInputActionOrigin from UnityInputControl for input translation.
/// Result is almost like XInput.
Expand Down Expand Up @@ -226,6 +303,10 @@ private static EInputActionOrigin GetBaseSteamInputAction(string controlLocalPat
case "touchpadButton":
return EInputActionOrigin.k_EInputActionOrigin_PS5_CenterPad_Click;

// Switch Pro controls
case "capture":
return EInputActionOrigin.k_EInputActionOrigin_Switch_Capture;

default:
return EInputActionOrigin.k_EInputActionOrigin_None;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.eviltwo.unity-steam-input-adapter",
"displayName": "Unity Steam Input Adapter",
"version": "0.9.2",
"version": "1.0.0",
"unity": "2022.3",
"description": "Change controller input from Unity(InputSystem) to Steam(InputAction)",
"author": {
Expand Down

0 comments on commit 8856d21

Please sign in to comment.