Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support SteamInput #6

Merged
merged 12 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,27 @@
- 各機能は別々のパッケージ・コンポーネントに分離しています。
- 例えば、キャラクターの移動処理とカメラ処理は別なので、ゲームに合わせてFPSカメラとTPSカメラを選べます。
- 他にもボタンのインタラクトやキャラクターアニメーションなども分離して実装予定です。
- 新しいInputSystemに対応していますが、古いInputManagerは未対応です。
- InputSystemパッケージをインストールすればデフォルト設定のままで動作します。

|入力パッケージ|動作||
|---|---|---|
|旧InputManager|×|
|InputSystem|○|
|SteamInput|○|SteamwWorksのInit処理は別途必要|

# パッケージ一覧 (UPMでインポートできます)
### CharacterControls v0.6.1
### CharacterControls v0.7.0
キャラクターの歩行・ジャンプ。
```
https://github.com/eviltwo/ActionGameCore.git?path=src/ActionGameCore/Assets/CharacterControls
```

### FPSCameraControls v1.2.0
### FPSCameraControls v1.3.0
FPS視点のカメラ。
```
https://github.com/eviltwo/ActionGameCore.git?path=src/ActionGameCore/Assets/FPSCameraControls
```

### TPSCameraControls v1.2.0
### TPSCameraControls v1.3.0
TPS視点のカメラ。
```
https://github.com/eviltwo/ActionGameCore.git?path=src/ActionGameCore/Assets/TPSCameraControls
Expand Down
4 changes: 4 additions & 0 deletions src/ActionGameCore/Assets/CharacterControls/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.7.0] - 2024-06-28
### Changed
- Support SteamInput.

## [0.6.1] - 2024-03-06
### Fixed
- Fix input action enabled.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"rootNamespace": "",
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:9cb36de80f6aa0e4f925e2361fb85950"
"GUID:9cb36de80f6aa0e4f925e2361fb85950",
"GUID:68bd7fdb68ef2684e982e8a9825b18a5"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -17,6 +18,11 @@
"name": "com.unity.inputsystem",
"expression": "",
"define": "SUPPORT_INPUTSYSTEM"
},
{
"name": "com.rlabrecque.steamworks.net",
"expression": "",
"define": "SUPPORT_STEAMWORKS"
}
],
"noEngineReferences": false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,73 @@
#if SUPPORT_INPUTSYSTEM
using CharacterControls.Movements;
using UnityEngine;

#if SUPPORT_INPUTSYSTEM
using UnityEngine.InputSystem;
#endif

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
using Steamworks;
#endif

namespace CharacterControls.Inputs
{
public class CharacterMoveInput : MonoBehaviour
{
#if SUPPORT_INPUTSYSTEM
[Header("InputSystem")]
[SerializeField]
private InputActionReference _moveActionReference = null;

[SerializeField]
private InputActionReference _jumpActionReference = null;
#endif

#if SUPPORT_STEAMWORKS
[Header("SteamInput")]
[SerializeField]
private string _steamMoveActionName = "Move";

[SerializeField]
private string _steamJumpActionName = "Jump";
#endif

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
private bool _steamInitialized;
private InputAnalogActionHandle_t _steamMoveActionHandle;
private InputDigitalActionHandle_t _steamJumpActionHandle;
private int _connectedControllerCount = 0;
private InputHandle_t[] _connectedControllerInputHandles = new InputHandle_t[Constants.STEAM_INPUT_MAX_COUNT];
#endif

public IMoveController MoveController { get; set; }

private void Start()
{
#if SUPPORT_INPUTSYSTEM
_moveActionReference?.action.Enable();
_jumpActionReference?.action.Enable();
MoveController = GetComponent<IMoveController>();
#endif

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
// You need to call SteamAPI.Init(), SteamInput.Init() and SteamInput.ActivateActionSet() in other class.
try
{
if (!string.IsNullOrEmpty(_steamMoveActionName))
{
_steamMoveActionHandle = SteamInput.GetAnalogActionHandle(_steamMoveActionName);
}
if (!string.IsNullOrEmpty(_steamJumpActionName))
{
_steamJumpActionHandle = SteamInput.GetDigitalActionHandle(_steamJumpActionName);
}
_steamInitialized = true;
}
catch (System.Exception e)
{
Debug.LogError($"Failed to initialize Steam Input: {e.Message}");
}
#endif
}

private void Update()
Expand All @@ -29,16 +77,68 @@ private void Update()
return;
}

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
if (_steamInitialized)
{
// Get controllers
_connectedControllerCount = SteamInput.GetConnectedControllers(_connectedControllerInputHandles);
}
#endif

UpdateMoveInput();
UpdateJumpInput();
}

private void UpdateMoveInput()
{
var value = Vector2.zero;

#if SUPPORT_INPUTSYSTEM
if (_moveActionReference != null)
{
MoveController.SetMoveInput(_moveActionReference.action.ReadValue<Vector2>());
var v = _moveActionReference.action.ReadValue<Vector2>();
value = v.sqrMagnitude > value.sqrMagnitude ? v : value;
}
#endif

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
if (_steamInitialized && _steamMoveActionHandle != null)
{
for (int i = 0; i < _connectedControllerCount; i++)
{
var data = SteamInput.GetAnalogActionData(_connectedControllerInputHandles[i], _steamMoveActionHandle);
var v = new Vector2(data.x, data.y);
value = v.sqrMagnitude > value.sqrMagnitude ? v : value;
}
}
#endif

MoveController.SetMoveInput(value);
}

private void UpdateJumpInput()
{
var value = false;

#if SUPPORT_INPUTSYSTEM
value |= _jumpActionReference != null && _jumpActionReference.action.WasPressedThisFrame();
#endif

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
if (_steamInitialized && _steamJumpActionHandle != null)
{
for (int i = 0; i < _connectedControllerCount; i++)
{
var data = SteamInput.GetDigitalActionData(_connectedControllerInputHandles[i], _steamJumpActionHandle);
value |= data.bState > 0;
}
}
#endif

if (_jumpActionReference != null && _jumpActionReference.action.WasPressedThisFrame())
if (value)
{
MoveController.SetJumpInput(1.0f);
MoveController.SetJumpInput(1);
}
}
}
}
#endif
2 changes: 1 addition & 1 deletion src/ActionGameCore/Assets/CharacterControls/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.eviltwo.character-controls",
"displayName": "Character Controls",
"version": "0.6.1",
"version": "0.7.0",
"unity": "2022.3",
"description": "Base movements for character of 3D action game.",
"author": {
Expand Down
5 changes: 5 additions & 0 deletions src/ActionGameCore/Assets/FPSCameraControls/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
# Changelog

## [1.3.0] - 2024-06-28
### Changed
- Support SteamInput.

## [1.2.0] - 2024-05-16
### Changed
- Match camera sensitivity of mouse and controller.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#if SUPPORT_INPUTSYSTEM
using System.Collections.Generic;
using UnityEngine;

#if SUPPORT_INPUTSYSTEM
using UnityEngine.InputSystem;
#endif

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
using Steamworks;
#endif

namespace FPSCameraControls
{
public class FPSCameraController : MonoBehaviour
{
[SerializeField]
private InputActionReference _deltaActionReference = null;

[SerializeField]
private InputActionReference _continuousActionreference = null;

[SerializeField]
public Transform Target = null;

Expand All @@ -34,14 +34,53 @@ public class FPSCameraController : MonoBehaviour
[SerializeField]
private bool _lockAndHideCursor = true;

#if SUPPORT_INPUTSYSTEM
[Header("InputSystem")]
[SerializeField]
private InputActionReference _deltaActionReference = null;

[SerializeField]
private InputActionReference _continuousActionreference = null;
#endif

#if SUPPORT_STEAMWORKS
[Header("SteamInput")]
[SerializeField]
private string _steamCameraActionName = "Camera";
#endif

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
private bool _steamInitialized;
private InputAnalogActionHandle_t _steamCameraActionHandle;
private InputHandle_t[] _connectedControllerInputHandles = new InputHandle_t[Constants.STEAM_INPUT_MAX_COUNT];
#endif

private Vector3 _lookAngles;
private List<Vector2> _deltaPositions = new List<Vector2>();
private List<float> _deltaTimes = new List<float>();

private void Start()
{
#if SUPPORT_INPUTSYSTEM
_deltaActionReference?.action.Enable();
_continuousActionreference?.action.Enable();
#endif

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
try
{
if (!string.IsNullOrEmpty(_steamCameraActionName))
{
_steamCameraActionHandle = SteamInput.GetAnalogActionHandle(_steamCameraActionName);
}
_steamInitialized = true;
}
catch (System.Exception e)
{
Debug.LogError($"Failed to initialize Steam Input: {e.Message}");
}
#endif

if (_lockAndHideCursor)
{
Cursor.visible = false;
Expand All @@ -59,9 +98,33 @@ private void LateUpdate()
var position = Target.position + new Vector3(0, OffsetY, 0);

var deltaAngle = Vector2.zero;

var deltaPosition = Vector2.zero;

#if SUPPORT_INPUTSYSTEM
if (_deltaActionReference != null)
{
_deltaPositions.Add(_deltaActionReference.action.ReadValue<Vector2>());
var v = _deltaActionReference.action.ReadValue<Vector2>();
deltaPosition = v.sqrMagnitude > deltaPosition.sqrMagnitude ? v : deltaPosition;
}
#endif

#if SUPPORT_STEAMWORKS && !DISABLESTEAMWORKS
if (_steamInitialized && _steamCameraActionHandle != null)
{
var controllerCount = SteamInput.GetConnectedControllers(_connectedControllerInputHandles);
for (int i = 0; i < controllerCount; i++)
{
var inputHandle = _connectedControllerInputHandles[i];
var data = SteamInput.GetAnalogActionData(inputHandle, _steamCameraActionHandle);
var v = new Vector2(data.x, -data.y);
deltaPosition = v.sqrMagnitude > deltaPosition.sqrMagnitude ? v : deltaPosition;
}
}
#endif

{
_deltaPositions.Add(deltaPosition);
_deltaTimes.Add(Time.deltaTime);
SmoothingFrameCount = Mathf.Max(1, SmoothingFrameCount);
if (_deltaPositions.Count > SmoothingFrameCount)
Expand All @@ -82,14 +145,18 @@ private void LateUpdate()
const float DpiAverage = 96;
var dpi = Screen.dpi == 0 ? DpiAverage : Screen.dpi;
const float InchForTurn = 13;
deltaAngle += smoothDeltaPosition / dpi / InchForTurn * 180;
var v = smoothDeltaPosition / dpi / InchForTurn * 180;
deltaAngle = v.sqrMagnitude > deltaAngle.sqrMagnitude ? v : deltaAngle;
}

#if SUPPORT_INPUTSYSTEM
if (_continuousActionreference != null)
{
const float SecondsForTurn = 1.0f;
deltaAngle += _continuousActionreference.action.ReadValue<Vector2>() * Time.deltaTime / SecondsForTurn * 180;
var v = _continuousActionreference.action.ReadValue<Vector2>() * Time.deltaTime / SecondsForTurn * 180;
deltaAngle = v.sqrMagnitude > deltaAngle.sqrMagnitude ? v : deltaAngle;
}
#endif

_lookAngles.x = Mathf.Clamp(_lookAngles.x - deltaAngle.y * Sensitivity, -AngleMax, -AngleMin); // Look up and down
_lookAngles.y = (_lookAngles.y + deltaAngle.x * Sensitivity) % 360; // Look left and right
Expand All @@ -114,4 +181,3 @@ private void OnDrawGizmos()
}
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "FPSCameraControls",
"rootNamespace": "",
"references": [
"GUID:75469ad4d38634e559750d17036d5f7c"
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:68bd7fdb68ef2684e982e8a9825b18a5"
],
"includePlatforms": [],
"excludePlatforms": [],
Expand All @@ -16,6 +17,11 @@
"name": "com.unity.inputsystem",
"expression": "",
"define": "SUPPORT_INPUTSYSTEM"
},
{
"name": "com.rlabrecque.steamworks.net",
"expression": "",
"define": "SUPPORT_STEAMWORKS"
}
],
"noEngineReferences": false
Expand Down
Loading