Protected Game Time (Pro)
In Unity, frame-based calculations rely heavily on UnityEngine.Time.deltaTime. Unfortunately, this time calculation is vulnerable to manipulation by cheat or hacking tools that allow unauthorised acceleration, deceleration or interruption of your application. To minimise such risks, the implementation of AntiCheat ProtectedTime is essential.
How to use
By replacing your Time class with ProtectedTime, you gain direct access to a protected time instance that prevents possible changes and ensures the integrity of your application's time-based calculations.
// Replace your usage of the UnityEngine.Time class with GUPS.AntiCheat.Protected.Time.ProtectedTime.
public static class ProtectedTime
{
// The time in seconds it took to complete the last frame (Read Only).
public static float deltaTime { get; }
// The real time in seconds since the game started (Read Only).
public static float realtimeSinceStartup { get; }
// The time at the beginning of this frame (Read Only). This is the time in seconds since the start of the game.
public static float time { get; }
// The scale at which the time is passing. This can be used for slow motion effects.
public static float timeScale { get; set; }
// The time this frame has started (Read Only). This is the time in seconds since the last level has been loaded.
public static float timeSinceLevelLoad { get; }
// The timeScale-independent interval in seconds from the last frame to the current one (Read Only).
public static float unscaledDeltaTime { get; }
// The timeScale-independent time for this frame (Read Only). This is the time in seconds since the start of the game.
public static float unscaledTime { get; }
}
The protected game time consists of two key components: the monitor and the detector. The game monitor calculates the deviation between the elapsed "game time" and the elapsed "real time". It notifies the observers of any significant deviation detected.
The game time cheat detector records the time deviations observed by the game time monitor, assesses the probability of a cheat and notifies the observers ('AntiCheat-Monitor') accordingly. The detector also initiates countermeasures by calculating the game time based on the system ticks when a cheat is detected. This ensures accurate calculation and application of game time, even in the presence of cheating.
Drag and drop the prefab to the anti cheat monitor in your scene.
Custom Observer
If you would like to write a custom listener to the detector, you can attach an observer:
// Get the detector from the anti cheat monitor.
var detector = AntiCheatMonitor.Instance
.GetDetector<GameTimeCheatingDetector>();
// Subscribe as observer and get notified on cheating.
detector.Subscribe(myObserver);