如何将键码存储到数组中并执行它们?

时间:2022-07-05 21:29:08

I'm wondering if it's possible to store keyboard input history into a array and execute them at a later time? Basically I have a 3D grid game and a character moving around the grid. I want the user to enter all the movement instructions via right down left up keys while the player remains still, and once I press enter the player will begin to execute each instruction that is stored into the array. any suggestions?

我想知道是否可以将键盘输入历史存储到数组中并在以后执行它们?基本上我有一个3D网格游戏和一个围绕网格移动的角色。我希望用户在播放器保持静止时通过右下左上键输入所有移动指令,一旦我按下输入,播放器将开始执行存储在阵列中的每条指令。有什么建议?

1 个解决方案

#1


I would recommend using a List<T>

我建议使用List

Example:

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

public class Training : MonoBehaviour {

public List<KeyCode> previousActions;

void Update(){
    if(Input.GetKeyDown(KeyCode.A)){
        Debug.Log("Do something");
        previousActions.Add(KeyCode.A);
    }else if(Input.GetKeyDown(KeyCode.S)){
        Debug.Log("Do something else");
        previousActions.Add(KeyCode.S);
    //---Check the list--//
    }else if(Input.GetKeyDown(KeyCode.D)){
        Debug.Log("Check the list");
        for(int i = 0;i < previousActions.Count;i++){
            Debug.Log(previousActions[i]);
        }
    }
}

#1


I would recommend using a List<T>

我建议使用List

Example:

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

public class Training : MonoBehaviour {

public List<KeyCode> previousActions;

void Update(){
    if(Input.GetKeyDown(KeyCode.A)){
        Debug.Log("Do something");
        previousActions.Add(KeyCode.A);
    }else if(Input.GetKeyDown(KeyCode.S)){
        Debug.Log("Do something else");
        previousActions.Add(KeyCode.S);
    //---Check the list--//
    }else if(Input.GetKeyDown(KeyCode.D)){
        Debug.Log("Check the list");
        for(int i = 0;i < previousActions.Count;i++){
            Debug.Log(previousActions[i]);
        }
    }
}