筆記13 輸入
鍵鼠輸入
任何按鍵按下
一直監聽anyKey(每幀運行一次。很少用)
//返回很多次。很少用
if (Input.anyKey)
{
Debug.Log("anyKey");
}
監聽1次anyKeyDown(按鍵按下運行一次。偶爾用)
//監聽1次。偶爾用
if (Input.anyKeyDown)
{
Debug.Log("anyKeyDown");
}
示意圖
監聽按鍵(用的特別多)
KeyCode是個枚舉,把所有的按鍵當成一個值。例如:Space空格、Alpha1數字1、Alpha12數字2。
如果不寫枚舉,直接在括號裏雙引號某個按鍵也是可以的。例如:按鍵a,寫爲:“a”
按下空格監聽一次
//表示:按下空格監聽一次。
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("按下空格");
}
按下空格,一直監聽
//按下空格,一直監聽
if (Input.GetKey(KeyCode.Space))
{
Debug.Log("按鍵中……");
}
擡起空格,調用一次
//擡起按鍵,調用
if (Input.GetKeyUp(KeyCode.Space))
{
Debug.Log("擡起空格");
}
鼠標
鼠標:0左鍵、1右鍵、2中建(滾輪)
點擊了左鍵
//點擊了左鍵
if (Input.GetMouseButtonDown(0)) ;
{
Debug.Log("點擊了左鍵");
}
按左鍵中
//按左鍵中
if (Input.GetMouseButton(0)) ;
{
Debug.Log("按左鍵中……");
}
擡起了左鍵
//擡起了左鍵
if (Input.GetMouseButtonUp(0)) ;
{
Debug.Log("擡起了左鍵");
}
實例:連續技
//連續技。例如連續按:下S、右D、拳U,出一個技能。
public class InputTest : MonoBehaviour
{
//釋放技能
//KeyCode是枚舉,給它寫個數組,叫技能skill。
private KeyCode[] skill = { KeyCode.S, KeyCode.D, KeyCode.U };
//按鍵時間。此處,限定1秒之內按完這三個鍵
private float skillTime = 1;
//計時器。計算當前過了多長時間了
private float timer = 0;
//按的鍵。目前按了哪幾個鍵。用了List(動態數組),就說明是時刻添加我們按的鍵
private List<KeyCode> InputBuff;
// Use this for initialization在初始化時使用此選項
void Start ()
{
//初始化
InputBuff = new List<KeyCode>();
}
// Update is called once per frame每幀更新一次
void Update ()
{
//計算計時器的時間。
/*Time.deltaTime;是每幀的時間,那麼timer表示把所有幀的時間都加起來,
即從遊戲開始到現在的時間(遊戲運行時間)。*/
timer += Time.deltaTime;
if (Input.GetKeyDown(KeyCode.S))
{
Debug.Log("開始輸入了");
timer = 0;
//清空按鍵
InputBuff.Clear();
//添加按鍵
InputBuff.Add(KeyCode.S);
}
if(Input.GetKeyDown(KeyCode.D))
{
InputBuff.Add(KeyCode.D);
}
if (Input.GetKeyDown(KeyCode.U))
{
InputBuff.Add(KeyCode.U);
}
//時間要小於1秒,按鍵要至少等於規定的三個按鍵
if(timer < skillTime && InputBuff.Count >= skill.Length)
{
//用布爾值臨時保存一下:能否放技能
bool isSkill = false;
//for循環
for (int i = 0; i < skill.Length; i++)
{
if(InputBuff[i] == skill[i])
{
isSkill = true;
}else
{
isSkill = false;
break; //跳出循環
}
}
//如果布爾值爲真
if (isSkill)
{
//放出技能
Debug.Log("下右拳!");
//清空計時器
timer = 0;
//清空按鍵
InputBuff.Clear();
}
}
}
}
真正的格鬥遊戲,一般不會要求一個總時間說1秒之內按那三個鍵,
而會規定每個按鍵多長時間,例如S0.2秒,D0.2秒。
手機觸摸輸入
與按鍵不同的是手機可以同時有兩個點、甚至三個點觸摸。默認的是單點觸摸
public class touchTest : MonoBehaviour {
// Use this for initialization
void Start () {
//開啓多點觸摸
Input.multiTouchEnabled = true;
}
// Update is called once per frame
void Update () {
//判斷單點觸摸。touchCount觸摸數量
if (Input.touchCount == 1)
{
//打印出觸摸位置。你按下的點事保存在touches這個數組裏的
Debug.Log(Input.touches[0].position);
//觸摸位置
switch(Input.touches[0].phase)
{
//它的值是枚舉值(共5個),直接用case,注意後面是冒號!
//開始觸摸,調用一次
case TouchPhase.Began:
break;
//觸摸中,並卻移動
case TouchPhase.Moved:
break;
//觸摸結束
case TouchPhase.Ended:
break;
//觸摸取消(其他突發事件導致的)
case TouchPhase.Canceled:
break;
//觸摸中不移動
case TouchPhase.Stationary:
break;
}
}
}
}
//判斷多點觸摸。例如兩點觸摸
if(Input.touchCount == 2)
{
}
瞭解虛擬軸
水平軸
void Update ()
{
/*水平軸。返回值:負軸-1、0、正軸1。而且這個返回值是過渡的,
比如現在是0,按了→或者d之後不是立馬變成1,而是0.1、0.2……逐漸到1。到1就到頭了。*/
//horizontal這個名字隨便起,Horizontal這個要和unity裏一樣。
float horizontal = Input.GetAxis("Horizontal");
Debug.Log(horizontal);
}
虛擬鍵
//虛擬按鍵
if (Input.GetButtonDown("Firel"))
{
Debug.Log("開火");
}
監聽鼠標移動
//監聽鼠標移動
float mouseX = Input.GetAxis("Mouse X");
Debug.Log(mouseX);
相機
清除標識、深度
渲染、視圖、視野、目標紋理
目標紋理之小地圖的製作方法
耳朵組件