Last active
November 5, 2021 03:45
-
-
Save isuzu-shiranui/dbcc104e3f3277fb5ffa660e2bd47a56 to your computer and use it in GitHub Desktop.
ローカルマルチプレーヤー(改造したほうが使いやすいです)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using UnityEngine.InputSystem; | |
using UnityEngine.InputSystem.Controls; | |
using UnityEngine.InputSystem.LowLevel; | |
using UnityEngine.InputSystem.Users; | |
public class InputSystemManager : MonoBehaviour | |
{ | |
// ReSharper disable once InconsistentNaming | |
public static Func<InputDevice, bool> onUnpairedDeviceUsed; | |
private void Start() | |
{ | |
InputUser.onUnpairedDeviceUsed += OnUnpairedDeviceUsed; | |
} | |
private void OnDestroy() | |
{ | |
InputUser.onUnpairedDeviceUsed -= OnUnpairedDeviceUsed; | |
} | |
private static void OnUnpairedDeviceUsed(InputControl control, InputEventPtr eventPtr) | |
{ | |
if(control is not ButtonControl) return; | |
var isDeviceUsableWithPlayerActions = onUnpairedDeviceUsed?.Invoke(control.device); | |
if(!isDeviceUsableWithPlayerActions.HasValue) return; | |
if (!isDeviceUsableWithPlayerActions.Value) return; | |
InputUser.listenForUnpairedDeviceActivity--; | |
if (InputUser.listenForUnpairedDeviceActivity == 0) | |
{ | |
EndJoining(); | |
} | |
} | |
public static void BeginJoining() | |
{ | |
++InputUser.listenForUnpairedDeviceActivity; | |
} | |
private static void EndJoining() | |
{ | |
InputUser.listenForUnpairedDeviceActivity = 0; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEngine.InputSystem; | |
using UnityEngine.InputSystem.Users; | |
public class User : MonoBehaviour | |
{ | |
[SerializeField] private InputActionAsset inputActionAsset; | |
private InputActionAsset inputActionAssetInternal; | |
private InputUser currentUser; | |
private void Start() | |
{ | |
this.inputActionAssetInternal = Instantiate(this.inputActionAsset); | |
var actionMap = this.inputActionAssetInternal.FindActionMap("Test"); | |
var action = actionMap.FindAction("New action"); | |
action.performed += context => { Debug.Log($"{this.gameObject.name} Action performed."); }; | |
} | |
private void OnDestroy() | |
{ | |
this.inputActionAssetInternal.Disable(); | |
if(this.currentUser.valid) | |
this.currentUser.UnpairDevices(); | |
} | |
[ContextMenu("Pairing")] | |
public void Pairing() | |
{ | |
bool OnHandler(InputDevice device) | |
{ | |
if (!this.IsDeviceUsableWithPlayerActions(device)) return false; | |
this.currentUser = InputUser.PerformPairingWithDevice(device); | |
this.inputActionAssetInternal.Enable(); | |
this.currentUser.AssociateActionsWithUser(this.inputActionAssetInternal); | |
Debug.Log($"Pairing device : {device.displayName} [{device.deviceId}]"); | |
InputSystemManager.onUnpairedDeviceUsed -= OnHandler; | |
return true; | |
} | |
InputSystemManager.onUnpairedDeviceUsed += OnHandler; | |
InputSystemManager.BeginJoining(); | |
} | |
private bool IsDeviceUsableWithPlayerActions(InputDevice device) | |
{ | |
if (this.inputActionAssetInternal == null) return true; | |
if (this.inputActionAssetInternal.controlSchemes.Count > 0) | |
{ | |
using var unpairedDevices = InputUser.GetUnpairedInputDevices(); | |
return InputControlScheme.FindControlSchemeForDevices(unpairedDevices, this.inputActionAssetInternal.controlSchemes, device) != null; | |
} | |
// ReSharper disable once ForeachCanBeConvertedToQueryUsingAnotherGetEnumerator | |
foreach (var actionMap in this.inputActionAssetInternal.actionMaps) | |
if (actionMap.IsUsableWithDevice(device)) | |
return true; | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment