Skip to main content
added 82 characters in body
Source Link
Heisenbug
  • 2.2k
  • 3
  • 24
  • 33

You can use a Dictionary to map KeyCodeKeyCode to specific action, eventually you can change this at runtime:

using UnityEngine;
using System.Collections.Generic;
using System;

public class TestInput : MonoBehaviour {

    private Dictionary<KeyCode,Action> d;
    
    void Start () 
    {
        d = new Dictionary<KeyCode, Action>();
        d[KeyCode.A] = () => {Debug.Log("ACTION A");};
        d[KeyCode.S] = () => {Debug.Log("ACTION B");};
        d[KeyCode.I] = () => 
            {
                Action swap = d[KeyCode.A];
                d[KeyCode.A] = d[KeyCode.S];
                d[KeyCode.S] = swap;
                
            };
    }
    
    // Update is called once per frame
    void Update () {
    
        if (Input.GetKeyDown(KeyCode.A))
        {
            d[KeyCode.A]();
            
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
            d[KeyCode.S]();
        }
        else if (Input.GetKeyDown(KeyCode.I))
        {
            d[KeyCode.I]();
        }
    }
}

Btw it seems that Unity does't expose any method that returns a list of all pressed keys, so the code above isn't very elegant. Hope this help.

You can use a Dictionary to map KeyCode to specific action, eventually you can change this at runtime:

using UnityEngine;
using System.Collections.Generic;
using System;

public class TestInput : MonoBehaviour {

    private Dictionary<KeyCode,Action> d;
    
    void Start () 
    {
        d = new Dictionary<KeyCode, Action>();
        d[KeyCode.A] = () => {Debug.Log("ACTION A");};
        d[KeyCode.S] = () => {Debug.Log("ACTION B");};
        d[KeyCode.I] = () => 
            {
                Action swap = d[KeyCode.A];
                d[KeyCode.A] = d[KeyCode.S];
                d[KeyCode.S] = swap;
                
            };
    }
    
    // Update is called once per frame
    void Update () {
    
        if (Input.GetKeyDown(KeyCode.A))
        {
            d[KeyCode.A]();
            
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
            d[KeyCode.S]();
        }
        else if (Input.GetKeyDown(KeyCode.I))
        {
            d[KeyCode.I]();
        }
    }
}

Btw it seems that Unity does't expose any method that returns a list of all pressed keys, so the code above isn't very elegant. Hope this help.

You can use a Dictionary to map KeyCode to specific action, eventually you can change this at runtime:

using UnityEngine;
using System.Collections.Generic;
using System;

public class TestInput : MonoBehaviour {

    private Dictionary<KeyCode,Action> d;
    
    void Start () 
    {
        d = new Dictionary<KeyCode, Action>();
        d[KeyCode.A] = () => {Debug.Log("ACTION A");};
        d[KeyCode.S] = () => {Debug.Log("ACTION B");};
        d[KeyCode.I] = () => 
            {
                Action swap = d[KeyCode.A];
                d[KeyCode.A] = d[KeyCode.S];
                d[KeyCode.S] = swap;
                
            };
    }
    
    // Update is called once per frame
    void Update () {
    
        if (Input.GetKeyDown(KeyCode.A))
        {
            d[KeyCode.A]();
            
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
            d[KeyCode.S]();
        }
        else if (Input.GetKeyDown(KeyCode.I))
        {
            d[KeyCode.I]();
        }
    }
}

Btw it seems that Unity does't expose any method that returns a list of all pressed keys, so the code above isn't very elegant. Hope this help.

Source Link
Heisenbug
  • 2.2k
  • 3
  • 24
  • 33

You can use a Dictionary to map KeyCode to specific action, eventually you can change this at runtime:

using UnityEngine;
using System.Collections.Generic;
using System;

public class TestInput : MonoBehaviour {

    private Dictionary<KeyCode,Action> d;
    
    void Start () 
    {
        d = new Dictionary<KeyCode, Action>();
        d[KeyCode.A] = () => {Debug.Log("ACTION A");};
        d[KeyCode.S] = () => {Debug.Log("ACTION B");};
        d[KeyCode.I] = () => 
            {
                Action swap = d[KeyCode.A];
                d[KeyCode.A] = d[KeyCode.S];
                d[KeyCode.S] = swap;
                
            };
    }
    
    // Update is called once per frame
    void Update () {
    
        if (Input.GetKeyDown(KeyCode.A))
        {
            d[KeyCode.A]();
            
        }
        else if (Input.GetKeyDown(KeyCode.S))
        {
            d[KeyCode.S]();
        }
        else if (Input.GetKeyDown(KeyCode.I))
        {
            d[KeyCode.I]();
        }
    }
}

Btw it seems that Unity does't expose any method that returns a list of all pressed keys, so the code above isn't very elegant. Hope this help.