VizKa

Change Control Scheme

May 5th, 2023
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.41 KB | Gaming | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEngine.InputSystem;
  7.  
  8. public class ChangeControlScheme : MonoBehaviour
  9. {
  10.     [SerializeField] public GameObject Spark;
  11.     [SerializeField] public GameObject Kling;
  12.    
  13.     private PlayerInput klingInput;
  14.     private PlayerInput sparkInput;
  15.  
  16.     private int sparkPos;
  17.     private int klingPos;
  18.  
  19.     private protected void Awake()
  20.     {
  21.         //Get player input of Spark
  22.         sparkInput = GetPlayerInput(Spark);
  23.        
  24.         //Get player input of Kling
  25.         klingInput = GetPlayerInput(Kling);
  26.  
  27.         //Kling will always be the second controller and Spark the first one
  28.         klingPos = 1;
  29.         sparkPos = 0;
  30.  
  31.         OnDeviceChange();
  32.        
  33.     }
  34.  
  35.     private static PlayerInput GetPlayerInput(GameObject character)
  36.     {
  37.         Transform InputTransform = character.transform.Find("Smooth Player Controller");
  38.         return InputTransform.GetComponent<PlayerInput>();
  39.     }
  40.    
  41.    
  42.     private void ChangeControlSchemeRuntime(PlayerInput playerInput, bool isUsingKeyboard, bool isSpark)
  43.     {
  44.         if (isUsingKeyboard)
  45.         {
  46.             playerInput.SwitchCurrentControlScheme("Keyboard&Mouse", Keyboard.current, Mouse.current);
  47.         }
  48.         else
  49.         {
  50.             if (Gamepad.all.Count == 1)
  51.             {
  52.                 playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[0]);
  53.             }
  54.             else
  55.             {
  56.                 if (isSpark)
  57.                 {
  58.                     if (Gamepad.all[0].name.EndsWith("1"))
  59.                     {
  60.                         playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[1]);
  61.                     }
  62.                     else
  63.                     {
  64.                         playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[0]);
  65.                     }
  66.                 }
  67.                 else
  68.                 {
  69.                     if (Gamepad.all[0].name.EndsWith("1"))
  70.                     {
  71.                         playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[0]);
  72.                     }
  73.                     else
  74.                     {
  75.                         playerInput.SwitchCurrentControlScheme("Gamepad", Gamepad.all[1]);
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.     }
  81.  
  82.     public void OnDeviceChange()
  83.     {
  84.         StartCoroutine(CheckDevices());
  85.     }
  86.  
  87.     IEnumerator CheckDevices()
  88.     {
  89.         yield return new WaitForSecondsRealtime(0.3f);
  90.        
  91.         if (klingInput.devices.Count is 0 or 2  && sparkInput.devices.Count is 1 or 3) //Spark on Pad - Kling on M&K
  92.         {
  93.             ChangeControlSchemeRuntime(klingInput, true, false);
  94.             ChangeControlSchemeRuntime(sparkInput, false,true);
  95.         }
  96.         else if (sparkInput.devices.Count is 0 or 2  && klingInput.devices.Count is 1 or 3) //Spark on M&K - Kling on Pad  
  97.         {
  98.             ChangeControlSchemeRuntime(klingInput, false, false);
  99.             ChangeControlSchemeRuntime(sparkInput, true, true);
  100.             klingPos = 0;
  101.         }
  102.         else if(sparkInput.devices.Count is 1 or 3 && klingInput.devices.Count is 1 or 3) //Both on pad
  103.         {
  104.             ChangeControlSchemeRuntime(sparkInput, false, true);
  105.             ChangeControlSchemeRuntime(klingInput, false, false);
  106.         }
  107.     }
  108.  
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment