123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class KeyboardShortcutManager : MonoBehaviour
- {
- public void AddKeyData(GameObject target, KeyCode keyCode, Action onKeyDown, Action onKeyStay, Action onKeyUp)
- {
- this.m_CommandDataList.Add(new KeyboardShortcutManager.CommandData(target, keyCode, onKeyDown, onKeyStay, onKeyUp));
- }
- private void Update()
- {
- this.m_NowInputKey = KeyCode.None;
- if (Input.anyKey)
- {
- IEnumerator enumerator = Enum.GetValues(typeof(KeyCode)).GetEnumerator();
- try
- {
- while (enumerator.MoveNext())
- {
- object obj = enumerator.Current;
- KeyCode keyCode = (KeyCode)obj;
- if (Input.GetKey(keyCode))
- {
- this.m_NowInputKey = keyCode;
- break;
- }
- }
- }
- finally
- {
- IDisposable disposable;
- if ((disposable = (enumerator as IDisposable)) != null)
- {
- disposable.Dispose();
- }
- }
- }
- this.UpdateKey();
- }
- private void UpdateKey()
- {
- if (this.m_CommandDataList == null)
- {
- return;
- }
- foreach (KeyboardShortcutManager.CommandData commandData in this.m_CommandDataList)
- {
- if (!(commandData.targetObject == null))
- {
- bool flag = this.m_BeforeInputKey == commandData.keyCode;
- bool flag2 = this.m_NowInputKey == commandData.keyCode;
- if (!flag && flag2)
- {
- commandData.onKeyDown();
- }
- else if (flag && flag2)
- {
- commandData.onKeyStay();
- }
- else if (flag && !flag2)
- {
- commandData.onKeyUp();
- }
- }
- }
- this.m_BeforeInputKey = this.m_NowInputKey;
- }
- private void OnApplicationQuit()
- {
- this.m_IsApplicationQuit = true;
- }
- private void OnDestroy()
- {
- if (this.m_IsApplicationQuit)
- {
- return;
- }
- this.m_CommandDataList.Clear();
- this.m_CommandDataList = null;
- }
- private HashSet<KeyboardShortcutManager.CommandData> m_CommandDataList = new HashSet<KeyboardShortcutManager.CommandData>();
- private KeyCode m_BeforeInputKey;
- private KeyCode m_NowInputKey;
- private bool m_IsApplicationQuit;
- public class CommandData
- {
- public CommandData(GameObject targetObject, KeyCode keyCode, Action onKeyDown, Action onKeyStay, Action onKeyUp)
- {
- this.targetObject = targetObject;
- this.keyCode = keyCode;
- this.onKeyDown = onKeyDown;
- this.onKeyStay = onKeyStay;
- this.onKeyUp = onKeyUp;
- }
- public GameObject targetObject { get; private set; }
- public KeyCode keyCode { get; set; }
- public Action onKeyDown { get; set; }
- public Action onKeyStay { get; set; }
- public Action onKeyUp { get; set; }
- }
- }
|