Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class ChangeControlScheme : MonoBehaviour
- {
- [SerializeField] public GameObject Spark;
- [SerializeField] public GameObject Kling;
- private PlayerInput klingInput;
- private PlayerInput sparkInput;
- private int sparkPos;
- private int klingPos;
- private protected void Awake()
- {
- //Get player input of Spark
- sparkInput = GetPlayerInput(Spark);
- //Get player input of Kling
- klingInput = GetPlayerInput(Kling);
- //Kling will always be the second controller and Spark the first one
- klingPos = 1;
- sparkPos = 0;
- OnDeviceChange();
- }
- private static PlayerInput GetPlayerInput(GameObject character)
- {
- Transform InputTransform = character.transform.Find("Smooth Player Controller");
- return InputTransform.GetComponent<PlayerInput>();
- }
- private void ChangeControlSchemeRuntime(PlayerInput playerInput, bool isUsingKeyboard, bool isSpark)
- {
- if (isUsingKeyboard)
- {
- playerInput.SwitchCurrentControlScheme("Keyboard&Mouse", Keyboard.current, Mouse.current);
- }
- else
- {
- if (Gamepad.all.Count == 1)
- {
- playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[0]);
- }
- else
- {
- if (isSpark)
- {
- if (Gamepad.all[0].name.EndsWith("1"))
- {
- playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[1]);
- }
- else
- {
- playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[0]);
- }
- }
- else
- {
- if (Gamepad.all[0].name.EndsWith("1"))
- {
- playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[0]);
- }
- else
- {
- playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[1]);
- }
- }
- }
- }
- }
- public void OnDeviceChange()
- {
- StartCoroutine(CheckDevices());
- }
- IEnumerator CheckDevices()
- {
- yield return new WaitForSecondsRealtime(0.3f);
- if (klingInput.devices.Count is 0 or 2 && sparkInput.devices.Count is 1 or 3) //Spark on Pad - Kling on M&K
- {
- ChangeControlSchemeRuntime(klingInput, true, false);
- ChangeControlSchemeRuntime(sparkInput, false,true);
- }
- else if (sparkInput.devices.Count is 0 or 2 && klingInput.devices.Count is 1 or 3) //Spark on M&K - Kling on Pad
- {
- ChangeControlSchemeRuntime(klingInput, false, false);
- ChangeControlSchemeRuntime(sparkInput, true, true);
- klingPos = 0;
- }
- else if(sparkInput.devices.Count is 1 or 3 && klingInput.devices.Count is 1 or 3) //Both on pad
- {
- ChangeControlSchemeRuntime(sparkInput, false, true);
- ChangeControlSchemeRuntime(klingInput, false, false);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment