目錄
六、Action Phase
七、Input Action Asset文件
1.Bindings Mode?
1>Binding
2>Positive\Negative Binding
3>Up\Down\Left\Right Composite
4>Up\Down\Left\Right\Forward\Backward?Composite
5>Binding with one modifier
6>Binding with two?modifier
2.Binding Path
3.Action Type
4.Initial State Check
5.Interaction
1>Default Interaction
2>Press Interaction
3>Hold Interaction
4>Tap Interaction
5>Slow Tap Interaction
6>Multi Tap Interaction
6.Processors
1>Clamp
2>Invert
3>Invert Vector 2
4>Invert Vector 3
5>Normalize
6>Normalize Vector 2
7>Normalize Vector 3
8>Scale
9>?Scale Vector 2
10>Scale Vector 3
11>Axis deadzone
12>Stick deadzone
六、Action Phase
Action有五個(gè)狀態(tài):
Disabled???Waiting???Started???Performed ???Canceled
- Disabled:未被啟用
- Waiting:等待輸入
- Started:接收到輸入,但Action尚未被觸發(fā)
- Performed:Action被觸發(fā)
- Canceled:輸入取消
其中Started、Performed和Canceled都在API中有對應(yīng)的事件
可以通過打印phase查詢當(dāng)前的狀態(tài)
using UnityEngine;
using UnityEngine.InputSystem;
public class Test : MonoBehaviour
{
private MyPlayerAction inputActions;
private void Start()
{
inputActions = new MyPlayerAction();
inputActions.Enable();
}
void Update()
{
Debug.Log(GetComponent<PlayerInput>().actions["New Action"].phase);
Debug.Log(inputActions.Player.NewAction.phase);
}
}
七、Input Action Asset文件
Actions旁邊的“+”可以新建Action,雙擊名稱進(jìn)行重命名。
?
?點(diǎn)擊Action旁邊的“+”號可以新建按鍵綁定
?
1.Bindings Mode?
1>Binding
可以綁定單個(gè)按鍵或者搖桿
2>Positive\Negative Binding
綁定兩個(gè)按鍵來創(chuàng)建一維的虛擬軸,返回float類型,范圍為[-1,1]。
需要設(shè)置兩個(gè)按鍵:Negative和Positive
當(dāng)Positive被按下 -> 返回1
當(dāng)Negative被按下 -> 返回-1
否則 -> 返回0
?
3>Up\Down\Left\Right Composite
綁定四個(gè)按鍵來創(chuàng)建二維的虛擬軸,返回Vector2類型。
在這個(gè)模式下,需要綁定四個(gè)按鍵,Up、Down、Left、Right,按鍵激活時(shí)對應(yīng)的返回值:
Up -> (0,1)
Down -> (0,-1)
Left -> (-1,0)
Right -> (1,0)
無激活 -> (0,0)
?
4>Up\Down\Left\Right\Forward\Backward?Composite
綁定六個(gè)按鍵來創(chuàng)建三維的虛擬軸,返回Vector3類型。
Up -> (0,1,0)
Down -> (0,-1,0)
Left -> (-1,0,0)
Right -> (1,0,0)
Forward ->(0,0,1)
BackWard ->(0,0,-1)
無激活 -> (0,0,0)
5>Binding with one modifier
需要綁定兩個(gè)按鍵,一個(gè)為Modifier,一個(gè)為Binding。比如?ctrl + z ,ctrl 是modifier ,z 是binding。
6>Binding with two?modifier
與上面的類似,只是多了一個(gè)Modifier。
2.Binding Path
選擇綁定模式后,點(diǎn)擊新建的Binding,在右側(cè)的Binding Properties設(shè)置Path,點(diǎn)擊下拉三角,然后點(diǎn)擊listen監(jiān)聽正在觸發(fā)的按鍵可以進(jìn)行快速綁定。
?
3.Action Type
?選中新建的Action,右側(cè)可以看到對應(yīng)的Action Properties
?
?Action Type有三種類型:Value(值)、Button(按鈕)、Pass Through
?
1>Value
監(jiān)測按鍵數(shù)值的持續(xù)變化
2>Button
對按下按鍵做出響應(yīng)
3>Pass Through
在Value和Button這兩個(gè)模式下,當(dāng)一個(gè)Action有多個(gè)設(shè)備的Bindings同時(shí)輸入的時(shí)候,Action只會(huì)被觸發(fā)程度最高的輸入設(shè)備觸發(fā)。(比如Move Action綁定了兩個(gè)手柄的搖桿,當(dāng)一個(gè)手柄向左,一個(gè)手柄向右的時(shí)候,Move Action只會(huì)響應(yīng)搖桿偏移更大的手柄,忽略另一個(gè)手柄的輸入。)
而在Pass Through模式下,當(dāng)一個(gè)Action有多個(gè)設(shè)備的Bindings同時(shí)輸入的時(shí)候,每個(gè)輸入設(shè)備都會(huì)觸發(fā)一次。(比如Move Action綁定了兩個(gè)手柄的搖桿,當(dāng)一個(gè)手柄向左,一個(gè)手柄向右的時(shí)候,Move Action會(huì)響應(yīng)一次向左,響應(yīng)一次向右,即會(huì)響應(yīng)所有手柄的輸入。)
4.Initial State Check
Action只有在Enabled之后才能被觸發(fā),但Action的Bindings可能在Enabled之前就處在非默認(rèn)狀態(tài)(比如在Action Enabled之前,綁定的按鍵已經(jīng)被按下),這時(shí)候:
- 開啟了Initial State Check:在Action Enabled后的下一個(gè)Update循環(huán)中Action被觸發(fā)
- 未開啟Initial State Check:Action不被觸發(fā)
Value模式下Initial State Check默認(rèn)開啟,無需手動(dòng)設(shè)置。
Button和Pass Through模式下Initial State Check需要手動(dòng)勾選。
例子:
我們增加兩個(gè)Action:Test1和Test2,Action Type都為Button,其中Test1不勾選Initial State Check,Test2勾選
?在游戲物體上掛載以下代碼
using UnityEngine;
public class InitialCheckTest : MonoBehaviour
{
private MyAction inputActions;
private int i;
void Start()
{
inputActions = new MyAction();
}
void Update()
{
if(i == 250)
{
inputActions.Player.Enable();
Debug.Log("Enabled!");
}
if(i == 251)
{
Debug.Log("Test1:" + inputActions.Player.Test1.triggered);
Debug.Log("Test2:" + inputActions.Player.Test2.triggered);
}
i++;
}
}
點(diǎn)擊運(yùn)行并按著K和L鍵,5秒后,控制臺(tái)輸出結(jié)果如下:
Test1未勾選Initial State Check,所以無法識別出按鍵已按下
5.Interaction
?
Action Properties和Binding Properties里面都可以設(shè)置Interaction。
在Action Properties里面設(shè)置Interaction,會(huì)影響到Action下面的所有Bindings,
在Binding Properties里面設(shè)置Interaction只會(huì)影響到這個(gè)Binding自身
請查閱:Interactions | Input System | 1.3.0 (unity3d.com)
1>Default Interaction
當(dāng)不添加Interactions的時(shí)候,會(huì)使用Default Interaction
對于Value Type:
- 檢查到有輸入:Waiting??Started
- 值改變:Started??Performed??Started
- 回到默認(rèn)值,無輸入:Started??Canceled??Waiting
using UnityEngine;
public class InteractionTest : MonoBehaviour
{
private MyAction inputActions;
private void Start()
{
inputActions = new MyAction();
inputActions.Player.Test1.started += Test1_started;
inputActions.Player.Test1.performed += Test1_performed;
inputActions.Player.Test1.canceled += Test1_canceled;
inputActions.Player.Enable();
}
private void Update()
{
Debug.Log(inputActions.Player.Test1.phase);
}
private void Test1_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test1: canceled");
}
private void Test1_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test1: performed");
}
private void Test1_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test1: started");
}
}
控制臺(tái)輸出結(jié)果:
對于Button Type:
- 檢查到有輸入:Waiting??Started
- 輸入超過按鍵閾值:Started??Performed
- 回到默認(rèn)值,無輸入:Performed??Canceled??Waiting
using UnityEngine;
public class InteractionTest : MonoBehaviour
{
private MyAction inputActions;
private void Start()
{
inputActions = new MyAction();
inputActions.Player.Test2.started += Test2_started;
inputActions.Player.Test2.performed += Test2_performed;
inputActions.Player.Test2.canceled += Test2_canceled;
inputActions.Player.Enable();
}
private void Update()
{
Debug.Log(inputActions.Player.Test2.phase);
}
private void Test2_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test2: canceled");
}
private void Test2_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test2: performed");
}
private void Test2_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test2: started");
}
}
控制臺(tái)輸出結(jié)果:
?對于Pass Through Type:
- 輸入改變:Waiting??Performed(沒有Started狀態(tài))
- 回到默認(rèn)值,無輸入:Performed??Canceled??Waiting
using UnityEngine;
public class InteractionTest : MonoBehaviour
{
private MyAction inputActions;
private void Start()
{
inputActions = new MyAction();
inputActions.Player.Test3.started += Test3_started;
inputActions.Player.Test3.performed += Test3_performed;
inputActions.Player.Test3.canceled += Test3_canceled;
inputActions.Player.Enable();
}
private void Update()
{
Debug.Log(inputActions.Player.Test3.phase);
}
private void Test3_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test3: canceled");
}
private void Test3_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test3: performed");
}
private void Test3_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test3: started");
}
}
控制臺(tái)輸出結(jié)果:
2>Press Interaction
?Press Interaction有三種可選的Behavior:Press Only、Release Only和Press And Release
選擇Press Only時(shí):
- 輸入達(dá)到Press Point:Waiting??Started ??Performed
- 松手低于Press Point:Performed??Canceled??Waiting
using UnityEngine;
public class InteractionTest : MonoBehaviour
{
private MyAction inputActions;
private void Start()
{
inputActions = new MyAction();
inputActions.Player.Test4.started += Test4_started;
inputActions.Player.Test4.performed += Test4_performed;
inputActions.Player.Test4.canceled += Test4_canceled;
inputActions.Player.Enable();
}
private void Update()
{
Debug.Log(inputActions.Player.Test4.phase);
}
private void Test4_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test4: canceled");
}
private void Test4_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test4: performed");
}
private void Test4_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test4: started");
}
}
?控制臺(tái)輸出結(jié)果:
選擇Release Only時(shí):
- 輸入達(dá)到Press Point:Waiting??Started
- 松手低于Press Point:Started??Performed??Canceled??Waiting
?修改Test4的Behavior為Release Only,運(yùn)行后控制臺(tái)輸出結(jié)果:
?
?選擇Press and Release時(shí):
- 輸入達(dá)到Press Point:Waiting??Started??Performed
- 松手低于Press Point:Performed??Canceled??Waiting(注意在這里Performed事件會(huì)再觸發(fā)一次)
?修改Test4的Behavior為Press and Release,運(yùn)行后控制臺(tái)輸出結(jié)果:
Performed事件一共觸發(fā)兩次:按鍵超過Press Point觸發(fā)一次,松手低于Press Point再觸發(fā)一次
3>Hold Interaction
- 輸入達(dá)到Press Point:Waiting??Started
- 持續(xù)按鍵超過設(shè)定的時(shí)長:Started??Performed
- (未達(dá)到設(shè)定時(shí)長就松開按鍵:?Started??Canceled??Waiting)
- 松手低于Press Point:Performed??Canceled??Waiting
using UnityEngine;
public class InteractionTest : MonoBehaviour
{
private MyAction inputActions;
private void Start()
{
inputActions = new MyAction();
inputActions.Player.Test5.started += Test5_started;
inputActions.Player.Test5.performed += Test5_performed;
inputActions.Player.Test5.canceled += Test5_canceled;
inputActions.Player.Enable();
}
private void Update()
{
Debug.Log(inputActions.Player.Test5.phase);
}
private void Test5_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test5: canceled");
}
private void Test5_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test5: performed");
}
private void Test5_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test5: started");
}
}
控制臺(tái)輸出結(jié)果(成功Hold):
控制臺(tái)輸出結(jié)果(按鍵未維持足夠時(shí)長):
?
4>Tap Interaction
- 輸入達(dá)到Press Point:Waiting??Started
- 在要求的時(shí)間間隔內(nèi)松開:?Started??Performed??Waiting
- (按鍵時(shí)間過長:?Started???Canceled??Waiting)
using UnityEngine;
public class InteractionTest : MonoBehaviour
{
private MyAction inputActions;
private void Start()
{
inputActions = new MyAction();
inputActions.Player.Test6.started += Test6_started;
inputActions.Player.Test6.performed += Test6_performed;
inputActions.Player.Test6.canceled += Test6_canceled;
inputActions.Player.Enable();
}
private void Update()
{
Debug.Log(inputActions.Player.Test6.phase);
}
private void Test6_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test6: canceled");
}
private void Test6_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test6: performed");
}
private void Test6_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test6: started");
}
}
控制臺(tái)輸出結(jié)果(成功Tap):
控制臺(tái)輸出結(jié)果(按鍵時(shí)間過長):
5>Slow Tap Interaction
- 輸入達(dá)到Press Point:Waiting??Started
- 長按足夠時(shí)長后松開:?Started??Performed??Waiting
- (按鍵時(shí)間過短:?Started???Canceled??Waiting)
using UnityEngine;
public class InteractionTest : MonoBehaviour
{
private MyAction inputActions;
private void Start()
{
inputActions = new MyAction();
inputActions.Player.Test7.started += Test7_started;
inputActions.Player.Test7.performed += Test7_performed;
inputActions.Player.Test7.canceled += Test7_canceled;
inputActions.Player.Enable();
}
private void Update()
{
Debug.Log(inputActions.Player.Test7.phase);
}
private void Test7_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test7: canceled");
}
private void Test7_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test7: performed");
}
private void Test7_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test7: started");
}
}
?控制臺(tái)輸出結(jié)果(成功Slow Tap):
控制臺(tái)輸出結(jié)果(按鍵時(shí)間過短) :
6>Multi Tap Interaction
- ?輸入達(dá)到Press Point:Waiting??Started
- 在設(shè)定的時(shí)間間隔內(nèi)連續(xù)按鍵達(dá)到足夠次數(shù):?Started??Performed??Waiting
- (不滿足:?Started???Canceled??Waiting)
?
using UnityEngine;
public class InteractionTest : MonoBehaviour
{
private MyAction inputActions;
private void Start()
{
inputActions = new MyAction();
inputActions.Player.Test8.started += Test8_started;
inputActions.Player.Test8.performed += Test8_performed;
inputActions.Player.Test8.canceled += Test8_canceled;
inputActions.Player.Enable();
}
private void Update()
{
Debug.Log(inputActions.Player.Test8.phase);
}
private void Test8_canceled(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test8: canceled");
}
private void Test8_performed(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test8: performed");
}
private void Test8_started(UnityEngine.InputSystem.InputAction.CallbackContext obj)
{
Debug.Log("Test8: started");
}
}
?控制臺(tái)輸出結(jié)果(成功Multi Tap):
?控制臺(tái)輸出結(jié)果(不滿足Multi Tap):
6.Processors
請查閱:Processors | Input System | 1.3.0 (unity3d.com)
1>Clamp
將值限定在Min~Max的范圍內(nèi)
- 小于min:返回min
- 處于min~max:返回自身
- 大于max:返回max?
2>Invert
將值乘以-1
3>Invert Vector 2
勾選了Invert X則向量中的x分量乘以-1
勾選了Invert Y則向量中的y分量乘以-1
4>Invert Vector 3
類似Invert Vector 2
5>Normalize
當(dāng)設(shè)定的Min>=Zero,將在Min~Max范圍內(nèi)的值標(biāo)準(zhǔn)化到0~1
當(dāng)設(shè)定的Min<Zero,將在Min~Max范圍內(nèi)的值標(biāo)準(zhǔn)化到-1~1
6>Normalize Vector 2
維持向量方向不變,將向量的長度標(biāo)準(zhǔn)化為1
7>Normalize Vector 3
類似Normalize Vector 2
8>Scale
乘以設(shè)定好的Factor值
9>?Scale Vector 2
向量中的X,Y分量分別乘以設(shè)定好的值
10>Scale Vector 3
類似Scale Vector 2
11>Axis deadzone
?
設(shè)定最小和最大值
當(dāng)值小于最小的時(shí)候,視為0,即視同沒有輸入
當(dāng)值的絕對值大于最大的時(shí)候,視為-1或1文章來源:http://www.zghlxwxcb.cn/news/detail-769528.html
12>Stick deadzone
對二維向量,與Axis deadzone類似文章來源地址http://www.zghlxwxcb.cn/news/detail-769528.html
到了這里,關(guān)于【Unity_Input System】Input System新輸入系統(tǒng)(二)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!