每天一个小技巧【2】·秘籍指令监听

打包测试的阶段,需要有一些辅助功能,类似开无敌、无限能量、资源满格等作弊器功能。而不同功能会涉及更多输入指令,为避免与现有按键冲突,其中一个做法是用特定的组合按键来实现。

此处用空格键作为秘籍开启按键,其实最好是用没有赋予功能的按键作为启动键。

其他可以执行作弊的地方调用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值