using UnityEngine; using System.Collections; public class Script_07_03 : MonoBehaviour { //記錄某按鍵按下的幀數 int keyFrame = 0; void Update () { if (Input.GetKeyDown (KeyCode.A)) { Debug.Log("A按下一次"); } if (Input.GetKey (KeyCode.A)) { //記錄按下的幀數 keyFrame++; Debug.Log("A連按:" + keyFrame+"幀"); } if (Input.GetKeyUp (KeyCode.A)) { //抬起后清空幀數 keyFrame=0; Debug.Log("A按鍵抬起"); } } }
運行:
using UnityEngine; using System.Collections; public class Script_07_04 : MonoBehaviour { //記錄某按鍵按下的幀數 int keyFrame = 0; // Update is called once per frame void Update () { if(Input.anyKeyDown) { //清空按下幀數 keyFrame=0; Debug.Log("任意鍵被按下"); } if(Input.anyKey) { keyFrame++; Debug.Log("任意鍵被長按"+keyFrame+"幀"); } } }

按下多個按鍵
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class manykey : MonoBehaviour {
private bool keydown;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.LeftShift))
{
keydown = true;
}
if (keydown)
{
if (Input.GetKeyDown(KeyCode.A))
{
Debug.Log("@@");
keydown = false;
}
}
}
}
如果需要設置許多案件的話,再添加幾個boolen變量就好
實例??組合按鍵
在經典的格斗游戲中,會有組合鍵發出牛逼的大招,而這個功能的事件思路其實不難:在玩家按下某一鍵后,便開始時間記數,在某一時間內按出所需要的鍵便發出大招。using UnityEngine;
using System.Collections.Generic;
using System;
public class Script_07_05 : 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;
//連續按鍵的事件限制
public const int FRAME_COUNT = 100;
//倉庫中儲存技能的數量
public const int SAMPLE_SIZE = 3;
//每組技能的按鍵數量
public const int SAMPLE_COUNT = 5;
//技能倉庫
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},
};
//記錄當前按下按鍵的鍵值
int currentkeyCode =0;
//標志是否開啟監聽按鍵
bool startFrame = false;
//記錄當前開啟監聽到現在的時間
int currentFrame = 0;
//保存一段時間內玩家輸入的按鍵組合
List<int> playerSample;
//標志完成操作
bool isSuccess= false;
void Start()
{
//初始話按鍵組合鏈表
playerSample = new List<int>();
}
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;
}
if(temp != null)
{
GUILayout.Label(temp);
}
}
if(isSuccess)
{
//顯示成功貼圖
GUILayout.Label(imageSuccess);
}
//默認提示信息
GUILayout.Label("連續組合按鍵1:下、前、下、前、拳");
GUILayout.Label("連續組合按鍵2:下、前、下、后、拳");
GUILayout.Label("連續組合按鍵2:下、后、下、后、拳");
}
void 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)//按鍵數不夠
{
}
else//如果玩家按下按鍵不小於設定按鍵,取后5位
{
for (int k=0;k<size-SAMPLE_COUNT;k++)
{
playerSample.Remove(playerSample[k]);
}
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; } } }按s,d,s,d,空格:
