change: input dialogue on player spawn
This commit is contained in:
@@ -1,14 +1,20 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Reset.Core.Tools;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.InputSystem.Users;
|
||||
|
||||
namespace Reset{
|
||||
public static class GameManager{
|
||||
public static GameObject UI;
|
||||
public static GameObject Camera;
|
||||
public static GameObject Input;
|
||||
|
||||
[RuntimeInitializeOnLoadMethodAttribute]
|
||||
public static SessionManager Session;
|
||||
|
||||
public static GameObject Player;
|
||||
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void PopulateSceneReferences(){
|
||||
try {
|
||||
UI = GameObject.Find("UICanvas");
|
||||
@@ -18,8 +24,29 @@ namespace Reset{
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public static void AttachControllerToPlayer(InputDevice device){
|
||||
if (!Player) {
|
||||
throw new Exception(message: "There is no player to attach this new input device to.");
|
||||
}
|
||||
|
||||
InputUser playerUser = Player.GetComponent<PlayerInput>().user;
|
||||
playerUser = InputUser.PerformPairingWithDevice(device, playerUser, InputUserPairingOptions.UnpairCurrentDevicesFromUser);
|
||||
}
|
||||
|
||||
public static void RequestNewController(){
|
||||
try {
|
||||
GameObject.Find("Input Selector").GetComponent<InputFinder>().AwaitNewInput();
|
||||
} catch (Exception e) {
|
||||
Debug.LogError($"Can't request a new controller: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void ClearCurrentController(){
|
||||
InputUser playerUser = Player.GetComponent<PlayerInput>().user;
|
||||
playerUser.UnpairDevices();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
35
Assets/Scripts/Core/InputFinder.cs
Normal file
35
Assets/Scripts/Core/InputFinder.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Reset;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.UIElements;
|
||||
|
||||
public class InputFinder : MonoBehaviour{
|
||||
public InputActionMap actionMap;
|
||||
|
||||
void Start(){
|
||||
actionMap.actions[0].performed += ctx => { InputPressed(ctx); };
|
||||
|
||||
GetComponent<UIDocument>().enabled = false;
|
||||
}
|
||||
|
||||
public void AwaitNewInput(){
|
||||
GameManager.ClearCurrentController();
|
||||
|
||||
GetComponent<UIDocument>().enabled = true;
|
||||
actionMap.Enable();
|
||||
}
|
||||
|
||||
void InputPressed(InputAction.CallbackContext context){
|
||||
try {
|
||||
GameManager.AttachControllerToPlayer(context.control.device);
|
||||
Debug.Log(context.control.device);
|
||||
} catch (Exception e) {
|
||||
Debug.LogError($"Failed to set the new device to the player: {e.Message}");
|
||||
return;
|
||||
}
|
||||
|
||||
GetComponent<UIDocument>().enabled = false;
|
||||
actionMap.Disable();
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/Core/InputFinder.cs.meta
Normal file
2
Assets/Scripts/Core/InputFinder.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8fc1a1d631088814c846ef223c98f932
|
||||
@@ -31,7 +31,9 @@ public class SessionManager : MonoBehaviour{
|
||||
var allocation = await RelayService.Instance.CreateAllocationAsync(maxConnections);
|
||||
NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(AllocationUtils.ToRelayServerData(allocation, connectionType));
|
||||
var joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
|
||||
return NetworkManager.Singleton.StartHost() ? joinCode : null;
|
||||
|
||||
Debug.Log(joinCode);
|
||||
return NetworkManager.Singleton.StartClient() ? joinCode : null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user