Use EventSystem for key-pressing events

前端 未结 2 1415
礼貌的吻别
礼貌的吻别 2021-01-12 06:02

Context: say you\'re checking whether \"W\" is pressed on the keyboard, the most common way to check this is through the following code:

void Update(){
    i         


        
2条回答
  •  清酒与你
    2021-01-12 06:43

    I created an simples script to trigger simple event. I used OnguiGUI instead of Update. See it bellow!

    using UnityEngine;
    using UnityEngine.Events;
    
    public class TriggerKey : MonoBehaviour
    {
    
        [Header("----Add key to trigger on pressed----")]
        public string key;
    
        // Unity event inspector
        public UnityEvent OnTriggerKey;
    
        public void OnGUI()
        {    // triiger event on trigger key
            if (Event.current.Equals(Event.KeyboardEvent(key)))
            {
                OnTriggerKey.Invoke();
                print("test trigger btn");
            }
    
        }
    }
    

提交回复
热议问题