在制作游戏的过程中,我们总要一次次的去测试一个或多个场景。当然整个游戏过程的速度也需要把控,因为为了达到想要的效果就必须不停地从头试玩游戏关卡。为了解决这个问题,我们需要一组灵活的用于作弊的子系统。 游戏中作弊是市面上很多游戏都在用的老方法。作弊系统能帮助开发和测试人员更好的测试游戏,而且最终发布时肯定会移除该系统,因为它能改变游戏的数值。移除已经实现的作弊系统可能会破坏整个产品的稳定性,因为它需要更改一些代码。还有一些称为作弊码的手段,甚至在某些网站上就能激活。
通常在游戏帮助中不会找到游戏内作弊的方法,因为游戏作者并不愿玩家作弊。但你可以在杂志或网上找到相关的作弊码。
实现游戏作弊子系统
下面来讲讲如何实现一个简单的作弊系统,适用于PC和移动平台。主要需求就是不用键盘也能使用。方便起见我们把触摸和鼠标事件做同样的处理。然后创建任意类型的GUI。这里会用到有点过时但还能用的IMGUI。
激活作弊码列表
让激活作弊码列表的流程简单点,但绝不是随随便便就能做到的。这里我们设计为在2秒之内点击或触摸右上角5次即可激活。
using UnityEngine;
using System.Collections;
using System;
public class Cheats : MonoBehaviour
{
// Activate corner area size by screen width percentage
public float ActivateAreaSize = 0.1f;
// How many clicks the player should do before cheats list will be visible
public int ClicksCount = 5;
// How many seconds player have to click/touch the screen
public float WaitTime = 2;
private float[] _clickTimes;
private int _clickTimesIndex;
private bool _active = false;
void Start()
{
// create clicks array and reset it with float.MinValue
_clickTimes = new float[ClicksCount];
ResetClicks();
}
private void ResetClicks()
{
for (int i = 0; i < ClicksCount; i++)
{
_clickTimes = float.MinValue;
}
}
void Update()
{
// check for click or touch and register it
if (CheckClickOrTouch())
{
// click will be registered at time since level load
_clickTimes[_clickTimesIndex] = Time.timeSinceLevelLoad;
// each next click will be written on next array index or 0 if overflow
_clickTimesIndex = (_clickTimesIndex + 1) % ClicksCount;
}
// check if cheat list should be activated
if (ShouldActivate())
{
_active = true;
ResetClicks();
}
}
// checks if cheat list should be activated
private bool ShouldActivate()
{
// check if all click/touches were made within WaitTime
foreach(float clickTime in _clickTimes)
{
if (clickTime < Time.timeSinceLevelLoad - WaitTime)
{
// return false if any of click/touch times has been done earlier
return false;
}
}
// if we are here, cheat should be activated
return true;
}
// returns true if there's click or touch within the activate area
private bool CheckClickOrTouch()
{
// convert activation area to pixels
float sizeInPixels = ActivateAreaSize * Screen.width;
// get the click/touch position
Vector2? position = ClickOrTouchPoint();
if (position.HasValue) // position.HasValue returns true if there is a click or touch
{
// check if withing the range
if (position.Value.x >= Screen.width - sizeInPixels && Screen.height - position.Value.y 0) // one or more touch
{
// check only the first touch
Touch touch = Input.touches[0];
// it should react only when the touch has just began
if (touch.phase == TouchPhase.Began) {
return touch.position;
}
}
// null if there's no click or touch
return null;
}
void OnGUI()
{
if (_active)
{
// display cheats list here...
}
}
}
上面的代码相当长,但这只是基本实现。主要功能是: 1、监听所有点击和触摸事件。 2、检测点击或触摸是否在有效的屏幕范围内。 3 、检测最后5次点击是否在指定时间内完成。 它还是可配置的!在OnGUI函数内加入绘制GUI以及激活作弊码的代码:
void OnGUI()
{
if (_active)
{
DisplayCheat("Close Cheat Menu", () => _active = false);
DisplayCheat("Test Cheat 1", () => Debug.Log("Test cheat Activated!"));
DisplayCheat("Inverse Gravity", () => Physics.gravity = -Physics.gravity);
}
}
private void DisplayCheat(string cheatName, Action clickedCallback)
{
if (GUILayout.Button("Cheat: " + cheatName))
{
clickedCallback();
}
}
真的很简单。作弊列表显示为一排可点击的按钮。点击按钮后执行回调函数。第一个按钮用于关闭作弊列表。现在单击右上角5次即可看到作弊列表显示在屏幕上。
如果你想看看效果。。。
欢迎下载工程文件,其中包含上述所有脚本和一个测试场景。工程使用Unity5.3.0制作,确保使用Unity5.3或更高版本。
