Advertisement
romi_fauzi

InputInterface

Aug 16th, 2020
3,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class InputInterface : MonoBehaviour
  6. {
  7.     BasicControl control;
  8.  
  9.     public Vector2 leftAnalog;
  10.     public Dictionary<string, bool> onButtonHold = new Dictionary<string, bool>();
  11.     public Dictionary<string, bool> onButtonDown = new Dictionary<string, bool>();
  12.     public Dictionary<string, bool> onButtonUp = new Dictionary<string, bool>();
  13.  
  14.     private void Awake()
  15.     {
  16.         control = new BasicControl();
  17.     }
  18.  
  19.     private void OnEnable()
  20.     {
  21.         control.Enable();
  22.     }
  23.  
  24.     private void OnDisable()
  25.     {
  26.         control.Disable();
  27.     }
  28.  
  29.     // Start is called before the first frame update
  30.     void Start()
  31.     {
  32.         foreach (var item in control.Basic.Get().actions)
  33.         {
  34.             onButtonHold.Add(item.name, false);
  35.             onButtonDown.Add(item.name, false);
  36.             onButtonUp.Add(item.name, false);
  37.  
  38.             item.performed += delegate { onButtonHold[item.name] = true; };
  39.             item.canceled += delegate { onButtonHold[item.name] = false; };
  40.  
  41.             item.canceled += delegate { onButtonUp[item.name] = true; };
  42.         }
  43.     }
  44.  
  45.     // Update is called once per frame
  46.     void Update()
  47.     {
  48.         leftAnalog = control.Basic.LeftAnalog.ReadValue<Vector2>();
  49.  
  50.         foreach (var item in control.Basic.Get().actions)
  51.         {
  52.             onButtonDown[item.name] = item.triggered;
  53.         }
  54.     }
  55.  
  56.     private void LateUpdate()
  57.     {
  58.         foreach (var item in control.Basic.Get().actions)
  59.         {
  60.             onButtonUp[item.name] = false;
  61.         }
  62.     }
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement