Unity3D之组合按键

来源:互联网 发布:淘宝投诉失败 编辑:程序博客网 时间:2024/06/04 23:36

实例——组合按键

在经典的格斗游戏中,会有组合键发出牛逼的大招,而这个功能的事件思路其实不难:在玩家按下某一键后,便开始时间记数,在某一时间内按出所需要的键便发出大招。

using UnityEngine;using System.Collections;using System.Collections.Generic;public class KeyTest : MonoBehaviour {    //方向键上的贴图      public Texture imageUp;    //方向键下的贴图      public Texture imageDown;    //方向键左的贴图      public Texture imageLeft;    //方向键右的贴图      public Texture imageRight;    //按键成功的贴图      public Texture imageSuccess;    //自定义方向键的储存值      public const int KEY_UP = 0;    public const int KEY_DOWN = 1;    public const int KEY_LEFT = 2;    public const int KEY_RIGHT = 3;    public const int KEY_FIRT = 4;    //技能仓库      int[,] Sample =       {          //下 + 前 + 下 + 前 + 拳          {KEY_DOWN,KEY_RIGHT,KEY_DOWN,KEY_RIGHT,KEY_FIRT},          //下 + 前 + 下 + 后 + 拳          {KEY_DOWN,KEY_RIGHT,KEY_DOWN,KEY_LEFT,KEY_FIRT},          //下 + 后 + 下 + 后 + 拳          {KEY_DOWN,KEY_LEFT,KEY_DOWN,KEY_LEFT,KEY_FIRT},      };    //连续按键的事件限制      public const int FRAME_COUNT = 100;    //仓库中储存技能的数量      public const int SAMPLE_SIZE = 3;    //每组技能的按键数量      public const int SAMPLE_COUNT = 5;     //保存一段时间内玩家输入的按键组合      List<int> playerSample;    //标志完成操作      bool isSuccess = false;    //记录当前按下按键的键值      int currentkeyCode = 0;    //标志是否开启监听按键      bool startFrame = false;    //记录当前开启监听到现在的时间      int currentFrame = 0;  // Use this for initializationvoid Start () {        //初始话按键组合链表          playerSample = new List<int>(); }// Update is called once per framevoid Update ()     {        //更新按键        UpdateKey();        if(Input.anyKeyDown)        {            if (isSuccess)            {                //按键成功后重置                  isSuccess = false;                Reset();            }             if(!startFrame)            {                //启动时间计数器                startFrame = true;            }            //将按键值添加链接中            playerSample.Add(currentkeyCode);            //遍历链表            int size = playerSample.Count;            if(size == SAMPLE_COUNT)            {                for (int i = 0; i < SAMPLE_SIZE; i++)                {                    int successCount = 0;                    for (int j = 0; j < SAMPLE_COUNT; j++)                    {                        int temp = playerSample[j];                        if(temp == Sample[i,j])                        {                            successCount++;                        }                    }                    if(successCount == SAMPLE_COUNT)                    {                        isSuccess = true;                        break;                    }                }            }        }        if(startFrame)        {            //计数器++            currentFrame++;        }        if(currentFrame >= FRAME_COUNT)        {            //计数器超时            if(!isSuccess)            {                Reset();            }        }}    void Reset()    {        //重置按键相关信息          currentFrame = 0;        startFrame = false;        playerSample.Clear();    }      void UpdateKey()    {        //获取当前键盘的按键信息          if (Input.GetKeyDown(KeyCode.W))        {            currentkeyCode = KEY_UP;        }        if (Input.GetKeyDown(KeyCode.S))        {            currentkeyCode = KEY_DOWN;        }        if (Input.GetKeyDown(KeyCode.A))        {            currentkeyCode = KEY_LEFT;        }        if (Input.GetKeyDown(KeyCode.D))        {            currentkeyCode = KEY_RIGHT;        }        if (Input.GetKeyDown(KeyCode.Space))        {            currentkeyCode = KEY_FIRT;        }      }    void OnGUI()    {        //获得按键组合链表中储存按键的数量          int size = playerSample.Count;        //遍历组合列表        for (int i = 0; i < size; i++)        {            //将按下的键对应的图片显示在屏幕中            int key = playerSample[i];            Texture temp = null;            switch (key)            {                case KEY_UP:                    temp = imageUp;                    break;                case KEY_DOWN:                    temp = imageDown;                    break;                case KEY_LEFT:                    temp = imageLeft;                    break;                case KEY_RIGHT:                    temp = imageRight;                    break;                  default:                    break;            }            if(temp != null)            {                GUILayout.Label(temp);            }        }        if(isSuccess)        {            //显示成功贴图              GUILayout.Label(imageSuccess);         }        //默认提示信息          GUILayout.Label("连续组合按键1:下、前、下、前、拳");        GUILayout.Label("连续组合按键2:下、前、下、后、拳");        GUILayout.Label("连续组合按键3:下、后、下、后、拳");      }}

按s,d,s,d,空格:


原创粉丝点击