unity3d输入与控制——键盘事件

在游戏中,玩家控制主角移动,按键攻击,选择行走。都需要在程序中监听玩家的输入。unity为开发者提供了input库,来支持键盘事件,鼠标事件以及触摸事件。本文主要回顾键盘事件,以后会逐文复习鼠标以及触摸事件。


键盘事件

一般的PC键盘有104个不同的按键,在程序中通过监听这些按键事件,从而进一步执行逻辑操作。如:射击游戏中,W表示前进,S表示后退,A表示左移,D表示右移。


按下事件

在脚本中,用input。GetKeyDown( )方法将按键值作为参数,监听此按键是否被按下。按下返回true,否者返回false。

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;    
  2. using System.Collections;    
  3.     
  4. public class Script_07_01 : MonoBehaviour    
  5. {    
  6.     
  7.     void Update ()     
  8.     {    
  9.         if (Input.GetKeyDown (KeyCode.W))    
  10.         {    
  11.             Debug.Log("您按下了W键");    
  12.         }    
  13.             
  14.         if (Input.GetKeyDown (KeyCode.S))    
  15.         {    
  16.             Debug.Log("您按下了S键");    
  17.         }    
  18.             
  19.         if (Input.GetKeyDown (KeyCode.A))    
  20.         {    
  21.             Debug.Log("您按下了A键");    
  22.         }    
  23.             
  24.         if (Input.GetKeyDown (KeyCode.D))    
  25.         {    
  26.             Debug.Log("您按下了D键");    
  27.         }    
  28.             
  29.         if (Input.GetKeyDown (KeyCode.Space))    
  30.         {    
  31.             Debug.Log("您按下了空格键");    
  32.         }    
  33.     }    
  34. }    


运行:




抬起事件

抬起事件完全依赖与按下事件,因为只有按下才有抬起。我们用Input.GetKeyUp( )方法监听抬起事件,按键抬起后,返回true,否则返回false。

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;    
  2. using System.Collections;    
  3.     
  4. public class Script_07_02 : MonoBehaviour    
  5. {    
  6.     
  7.     void Update ()     
  8.     {    
  9.         //按下事件    
  10.         if (Input.GetKeyDown (KeyCode.W))    
  11.         {    
  12.             Debug.Log("您按下了W键");    
  13.         }    
  14.             
  15.         if (Input.GetKeyDown (KeyCode.S))    
  16.         {    
  17.             Debug.Log("您按下了S键");    
  18.         }    
  19.             
  20.         if (Input.GetKeyDown (KeyCode.A))    
  21.         {    
  22.             Debug.Log("您按下了A键");    
  23.         }    
  24.             
  25.         if (Input.GetKeyDown (KeyCode.D))    
  26.         {    
  27.             Debug.Log("您按下了D键");    
  28.         }    
  29.             
  30.         if (Input.GetKeyDown (KeyCode.Space))    
  31.         {    
  32.             Debug.Log("您按下了空格键");    
  33.         }    
  34.             
  35.         //抬起按键    
  36.         if (Input.GetKeyUp (KeyCode.W))    
  37.         {    
  38.             Debug.Log("您抬起了W键");    
  39.         }    
  40.             
  41.         if (Input.GetKeyUp (KeyCode.S))    
  42.         {    
  43.             Debug.Log("您抬起了S键");    
  44.         }    
  45.             
  46.         if (Input.GetKeyUp (KeyCode.A))    
  47.         {    
  48.             Debug.Log("您抬起了A键");    
  49.         }    
  50.             
  51.         if (Input.GetKeyUp (KeyCode.D))    
  52.         {    
  53.             Debug.Log("您抬起了D键");    
  54.         }    
  55.             
  56.         if (Input.GetKeyUp (KeyCode.Space))    
  57.         {    
  58.             Debug.Log("您抬起了空格键");    
  59.         }    
  60.         
  61.     }    
  62. }    


运行:




长按事件
长按事件是监听某一按键是否处于一直按下的状态,通过Input.GetKey( )来判断键盘中某一按键是否被一直按着。

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;    
  2. using System.Collections;    
  3.     
  4. public class Script_07_03 : MonoBehaviour     
  5. {    
  6.     //记录某按键按下的帧数    
  7.     int keyFrame = 0;    
  8.         
  9.     
  10.     void Update ()    
  11.     {    
  12.         
  13.         if (Input.GetKeyDown (KeyCode.A))    
  14.         {    
  15.             Debug.Log("A按下一次");    
  16.         }       
  17.         if (Input.GetKey (KeyCode.A))    
  18.         {    
  19.             //记录按下的帧数    
  20.             keyFrame++;    
  21.             Debug.Log("A连按:" + keyFrame+"帧");    
  22.         }    
  23.         if (Input.GetKeyUp (KeyCode.A))    
  24.         {    
  25.             //抬起后清空帧数    
  26.             keyFrame=0;    
  27.             Debug.Log("A按键抬起");    
  28.         }       
  29.     }    
  30. }    

运行:





任意键事件
在程序中还可以监听按键中的任意按键是否被按下,常见于加载完游戏后,按任意键进入。
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;    
  2. using System.Collections;    
  3.     
  4. public class Script_07_04 : MonoBehaviour     
  5. {    
  6.     
  7.     //记录某按键按下的帧数    
  8.     int keyFrame = 0;    
  9.         
  10.     // Update is called once per frame    
  11.     void Update ()    
  12.     {    
  13.         if(Input.anyKeyDown)    
  14.         {    
  15.             //清空按下帧数    
  16.             keyFrame=0;    
  17.             Debug.Log("任意键被按下");    
  18.         }    
  19.             
  20.             
  21.         if(Input.anyKey)    
  22.         {    
  23.             keyFrame++;    
  24.             Debug.Log("任意键被长按"+keyFrame+"帧");    
  25.         }    
  26.         
  27.     }    
  28. }    


运行:



实例——组合按键

在经典的格斗游戏中,会有组合键发出牛逼的大招,而这个功能的事件思路其实不难:在玩家按下某一键后,便开始时间记数,在某一时间内按出所需要的键便发出大招。
[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;    
  2. using System.Collections.Generic;    
  3. using System;    
  4.     
  5. public class Script_07_05 : MonoBehaviour     
  6. {    
  7.     
  8.     //方向键上的贴图    
  9.     public Texture imageUp;    
  10.     //方向键下的贴图    
  11.     public Texture imageDown;    
  12.     //方向键左的贴图    
  13.     public Texture imageLeft;    
  14.     //方向键右的贴图    
  15.     public Texture imageRight;    
  16.     //按键成功的贴图    
  17.     public Texture imageSuccess;    
  18.         
  19.     //自定义方向键的储存值    
  20.     public const int KEY_UP = 0;    
  21.     public const int KEY_DOWN = 1;    
  22.     public const int KEY_LEFT = 2;    
  23.     public const int KEY_RIGHT = 3;    
  24.     public const int KEY_FIRT = 4;    
  25.         
  26.     //连续按键的事件限制    
  27.     public const int FRAME_COUNT = 100;    
  28.     //仓库中储存技能的数量    
  29.     public const int SAMPLE_SIZE = 3;    
  30.     //每组技能的按键数量    
  31.     public const int SAMPLE_COUNT = 5;    
  32.     //技能仓库    
  33.     int[,] Sample =     
  34.     {    
  35.         //下 + 前 + 下 + 前 + 拳    
  36.         {KEY_DOWN,KEY_RIGHT,KEY_DOWN,KEY_RIGHT,KEY_FIRT},    
  37.         //下 + 前 + 下 + 后 + 拳    
  38.         {KEY_DOWN,KEY_RIGHT,KEY_DOWN,KEY_LEFT,KEY_FIRT},    
  39.         //下 + 后 + 下 + 后 + 拳    
  40.         {KEY_DOWN,KEY_LEFT,KEY_DOWN,KEY_LEFT,KEY_FIRT},    
  41.     };    
  42.         
  43.     //记录当前按下按键的键值    
  44.     int  currentkeyCode =0;    
  45.     //标志是否开启监听按键    
  46.     bool startFrame = false;    
  47.     //记录当前开启监听到现在的时间    
  48.     int  currentFrame = 0;    
  49.     //保存一段时间内玩家输入的按键组合    
  50.     List<int> playerSample;    
  51.     //标志完成操作    
  52.     bool isSuccess= false;    
  53.         
  54.     void Start()    
  55.     {    
  56.         //初始话按键组合链表    
  57.         playerSample  = new List<int>();    
  58.     }    
  59.         
  60.     void OnGUI()    
  61.     {    
  62.         //获得按键组合链表中储存按键的数量    
  63.         int size = playerSample.Count;    
  64.         //遍历该按键组合链表    
  65.         for(int i = 0; i< size; i++)    
  66.         {    
  67.             //将按下按键对应的图片显示在屏幕中    
  68.             int key = playerSample[i];    
  69.             Texture temp = null;    
  70.             switch(key)    
  71.             {    
  72.             case KEY_UP:    
  73.                 temp = imageUp;    
  74.                 break;    
  75.             case KEY_DOWN:    
  76.                 temp = imageDown;    
  77.                 break;    
  78.             case KEY_LEFT:    
  79.                 temp = imageLeft;    
  80.                 break;    
  81.             case KEY_RIGHT:    
  82.                 temp = imageRight;    
  83.                 break;    
  84.             }    
  85.             if(temp != null)    
  86.             {    
  87.                 GUILayout.Label(temp);    
  88.             }    
  89.         }    
  90.             
  91.         if(isSuccess)    
  92.         {    
  93.             //显示成功贴图    
  94.             GUILayout.Label(imageSuccess);    
  95.         }    
  96.         //默认提示信息    
  97.         GUILayout.Label("连续组合按键1:下、前、下、前、拳");    
  98.         GUILayout.Label("连续组合按键2:下、前、下、后、拳");    
  99.         GUILayout.Label("连续组合按键2:下、后、下、后、拳");    
  100.     }    
  101.         
  102.     void Update ()     
  103.     {    
  104.         //更新按键    
  105.         UpdateKey();    
  106.             
  107.         if(Input.anyKeyDown)    
  108.         {    
  109.                 
  110.             if(isSuccess)    
  111.             {    
  112.                 //按键成功后重置    
  113.                 isSuccess = false;    
  114.                 Reset();    
  115.             }    
  116.                 
  117.             if(!startFrame)    
  118.             {    
  119.                 //启动时间计数器    
  120.                 startFrame = true;    
  121.             }    
  122.                 
  123.             //将按键值添加如链表中    
  124.             playerSample.Add(currentkeyCode);    
  125.             //遍历链表    
  126.             int size = playerSample.Count;    
  127.             if(size == SAMPLE_COUNT)    
  128.             {    
  129.                 for(int i = 0; i< SAMPLE_SIZE; i++)    
  130.                 {    
  131.                     int SuccessCount = 0;    
  132.                     for(int j = 0; j< SAMPLE_COUNT; j++)    
  133.                     {    
  134.                         int temp = playerSample[j];    
  135.                         if(temp== Sample[i,j]){    
  136.                             SuccessCount++;    
  137.                         }    
  138.                     }    
  139.                     //玩家按下的组合按键与仓库中的按键组合相同表示释放技能成功    
  140.                     if(SuccessCount ==SAMPLE_COUNT)    
  141.                     {    
  142.                         isSuccess = true;    
  143.                         break;    
  144.                     }    
  145.                 }    
  146.                     
  147.             }    
  148.         }    
  149.             
  150.         if(startFrame)    
  151.         {    
  152.             //计数器++    
  153.             currentFrame++;    
  154.         }    
  155.             
  156.         if(currentFrame >= FRAME_COUNT)    
  157.         {    
  158.             //计数器超时    
  159.             if(!isSuccess)    
  160.             {    
  161.                 Reset();    
  162.             }    
  163.         }    
  164.     }    
  165.         
  166.      void Reset ()    
  167.      {    
  168.         //重置按键相关信息    
  169.         currentFrame = 0;    
  170.         startFrame = false;    
  171.         playerSample.Clear();    
  172.      }    
  173.         
  174.     void UpdateKey()    
  175.     {    
  176.         //获取当前键盘的按键信息    
  177.         if (Input.GetKeyDown (KeyCode.W))    
  178.         {    
  179.             currentkeyCode = KEY_UP;    
  180.         }    
  181.         if (Input.GetKeyDown (KeyCode.S))    
  182.         {    
  183.             currentkeyCode = KEY_DOWN;    
  184.         }    
  185.         if (Input.GetKeyDown (KeyCode.A))    
  186.         {    
  187.             currentkeyCode = KEY_LEFT;    
  188.         }    
  189.         if (Input.GetKeyDown (KeyCode.D))    
  190.         {    
  191.             currentkeyCode = KEY_RIGHT;    
  192.         }    
  193.         if (Input.GetKeyDown (KeyCode.Space))    
  194.         {    
  195.             currentkeyCode = KEY_FIRT;    
  196.         }    
  197.     }    
  198.         
  199. }  
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值