实现自定义多个数的按键输入
//CustomKey.cs
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
/// <summary>
/// 自定义按键
/// </summary>
public class CustomKey : MonoBehaviour
{
//声明Button对象以进行拖拽
public Button resetBtn;
public Button jumpBtn;
public Button getDownBtn;
public Button leftHeadBtn;
public Button rightHeadBtn;
public Button musicSwitchBtn;
public CustomButton jump;
public CustomButton getDown;
public CustomButton leftHead;
public CustomButton rightHead;
public CustomButton musicSwitch;
void Awake()
{
resetBtn.onClick.AddListener(CustomButton.ResetBindKeys);
resetBtn.transform.FindChild("Text").GetComponent<Text>().text = "Reset";
jump = new CustomButton("Jump", jumpBtn, new KeyCode[]{ KeyCode.Space});
getDown = new CustomButton("GetDown", getDownBtn, new KeyCode[] { KeyCode.LeftControl});
leftHead = new CustomButton("LeftHead", leftHeadBtn, new KeyCode[] { KeyCode.Q});
rightHead = new CustomButton("RightHead", rightHeadBtn, new KeyCode[] { KeyCode.E});
musicSwitch = new CustomButton("musicSwitch", musicSwitchBtn, new KeyCode[] { KeyCode.LeftControl, KeyCode.M});
}
void Update()
{
if (CustomButton.isPlaying)
{
jump.Function();
getDown.Function();
leftHead.Function();
rightHead.Function();
musicSwitch.Function();
}
}
void OnGUI()
{
if (CustomButton.isWaitingForKey)
{
Event e = Event.current;
if (e.isKey && e.keyCode != KeyCode.None)
{
if (!CustomButton.tempList.Contains(e.keyCode)) //如果list中不包含该按键,则记录按键