打包测试的阶段,需要有一些辅助功能,类似开无敌、无限能量、资源满格等作弊器功能。而不同功能会涉及更多输入指令,为避免与现有按键冲突,其中一个做法是用特定的组合按键来实现。
此处用空格键作为秘籍开启按键,其实最好是用没有赋予功能的按键作为启动键。
其他可以执行作弊的地方调用canCheat作为判断。
测试阶段将该脚本放入场景中,正式发布时需要移除。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cheat : MonoBehaviour {
public KeyCode[] targetCheatNum;//设置若干按键,按空格键后有10秒时间输入
private List<KeyCode> cheatNum;
private bool startRecord;
private float timer;
public static bool canCheat; //是否可以作弊
void Update () {
if (Input.GetKeyDown(KeyCode.Space)&&!canCheat)
{
startRecord = true;
timer = 0;
cheatNum = new List<KeyCode>();
}
if (startRecord)
{
timer += Time.deltaTime;
if (timer >= 10)
{
startRecord = false;
timer = 0;
}
else
{
for (var i = 0; i < targetCheatNum.Length; i++)
{
if (Input.GetKeyDown(targetCheatNum[i]))
{
cheatNum.Add(targetCheatNum[i]);
}
}
CheckNum();
}
}
}
void CheckNum()
{
canCheat = true;
for(var i = 0; i < targetCheatNum.Length; i++)
{
if (cheatNum.Count >= targetCheatNum.Length)
{
Debug.Log(cheatNum[i]+";"+ targetCheatNum[i]);
if (cheatNum[i] != targetCheatNum[i])
{
canCheat = false;
startRecord = false;
}
}
else
{
canCheat = false;
}
}
if (canCheat)
{
startRecord = false;
}
}
}
返回目录:https://blog.csdn.net/yzy1987523/article/details/107739933