GeeItSomeLaldy

InputsToGlobals.cs

Jun 6th, 2021 (edited)
572
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.InputSystem;
  6. using UnityEngine.Localization.SmartFormat.GlobalVariables;
  7.  
  8. public class InputsToGlobals : MonoBehaviour
  9. {
  10.     public GlobalVariablesGroup InputGlobals;
  11.     public string HorizontalId, JumpId, QuickUseId, SprintId, InventoryId, ControlSchemeId;
  12.     public InputActionReference HorizontalAxis, Jump, QuickUse, Sprint, Inventory;
  13.     public TMPro.TMP_SpriteAsset ControllerButtonsSpriteAsset;
  14.     private void Start()
  15.     {
  16.         OnControlsChanged(GetComponent<PlayerInput>());
  17.     }
  18.     [ContextMenu("Rebuild")]
  19.     private void Rebuild()
  20.     {
  21.         OnControlsChanged(GetComponent<PlayerInput>());
  22.     }
  23.     public void OnControlsChanged(PlayerInput playerInput)
  24.     {
  25.         var controlScheme = playerInput.ResolveControlScheme();
  26.         Debug.Log("Controls changed, updating Globals");
  27.         SetGlobal(playerInput, HorizontalId, HorizontalAxis, (controlScheme == "Keyboard")? 2 : 1);
  28.         SetGlobal(playerInput, JumpId, Jump);
  29.         SetGlobal(playerInput, QuickUseId, QuickUse);
  30.         SetGlobal(playerInput, SprintId, Sprint);
  31.         SetGlobal(playerInput, InventoryId, Inventory);
  32.  
  33.         if (InputGlobals.TryGetValue(ControlSchemeId, out IGlobalVariable globalVariable))
  34.         {
  35.             if (globalVariable is GlobalVariable<string>)
  36.             {
  37.                 (globalVariable as GlobalVariable<string>).Value = controlScheme;
  38.             }
  39.             else
  40.             {
  41.                 Debug.LogError($"Global '{ControlSchemeId}' is not a String");
  42.             }
  43.         }
  44.         else
  45.         {
  46.             Debug.LogError($"Global '{ControlSchemeId}' not found");
  47.         }
  48.  
  49.     }
  50.     public string SpriteFormat = "<sprite={0}>";
  51.     public string KeyFormat = "<color='blue'>[{0}]</color>";
  52.     public void SetGlobal(PlayerInput playerInput, string Key, InputActionReference Input, int maximumIndices = 1)
  53.     {
  54.         string controlScheme = playerInput.ResolveControlScheme();
  55.         var action = Input.action;
  56.         List<string> bindingStrings = new List<string>();
  57.  
  58.         for (int bindingIndex = 0; bindingIndex < maximumIndices; bindingIndex++)
  59.         {
  60.             ControllerButtonsSpriteAsset.GetSpriteIndicesForAction(action, controlScheme, out int[] indices, out string[] keys, bindingIndex);
  61.             for (int i = 0; i < indices.Length; i++)
  62.             {
  63.                 if (indices[i] == -1)
  64.                 {
  65.                     bindingStrings.Add(string.Format(KeyFormat, keys[i]));
  66.                 }
  67.                 else
  68.                 {
  69.                     bindingStrings.Add(string.Format(SpriteFormat, indices[i], keys[i]));
  70.                 }
  71.             }
  72.         }
  73.         var value = bindingStrings.Aggregate((a, b) => $"{a}{b}");
  74.         if(InputGlobals.TryGetValue(Key,out IGlobalVariable globalVariable) )
  75.         {
  76.             if (globalVariable is GlobalVariable<string>)
  77.             {
  78.                 (globalVariable as GlobalVariable<string>).Value = value;
  79.             } else
  80.             {
  81.                 Debug.LogError($"Global '{Key}' is not a String");
  82.             }
  83.         } else
  84.         {
  85.             Debug.LogError($"Global '{Key}' not found");
  86.         }
  87.  
  88.     }
  89. }
  90.  
RAW Paste Data