Skip to content

Instantly share code, notes, and snippets.

@beardordie
Created March 24, 2020 03:27
Show Gist options
  • Save beardordie/d51efaccd9bb278f7ffd96997ee93d6f to your computer and use it in GitHub Desktop.
Save beardordie/d51efaccd9bb278f7ffd96997ee93d6f to your computer and use it in GitHub Desktop.
A simple Unity Cheat Code system, optionally play a sound when a cheat code is used. Add cheats in the Inspector with UnityEvent, or through code using RegisterCheat method
using UnityEngine;
using System.Collections.Generic;
using System;
using UnityEngine.Events;
[RequireComponent(typeof(AudioSource))]
public class CheatCodeDetector : MonoBehaviour
{
[SerializeField] private List<Cheat> cheats = new List<Cheat>();
private AudioSource audioSource = null;
private Cheat cheatToRemoveThisUpdate = null;
[Serializable]
private class Cheat
{
public string cheatCode = string.Empty;
[Tooltip("Optional")] [SerializeField] private AudioClip audioClip = null;
[SerializeField] private bool deactivateCheatOnUse = false;
[SerializeField] private UnityEvent onUseCheat = null;
private Action onActivate = null;
private int cheatTypeIndex;
private CheatCodeDetector cheatCodeDetector = null;
public Cheat(string cheatCode, AudioClip audioClip = null, bool deactivateCheatOnUse = false, Action onActivate = null, UnityEvent onUseCheat = null)
{
this.cheatCode = cheatCode;
this.audioClip = audioClip;
this.deactivateCheatOnUse = deactivateCheatOnUse;
this.onActivate = onActivate;
this.onUseCheat = onUseCheat;
cheatTypeIndex = 0;
}
public void Update()
{
if (Input.inputString != "")
{
if (Input.inputString[0] == cheatCode[cheatTypeIndex])
{
cheatTypeIndex++;
if (cheatTypeIndex == cheatCode.Length)
{
onActivate?.Invoke();
onUseCheat?.Invoke();
PlaySoundOnActivate();
if (deactivateCheatOnUse)
DeactivateCheatCode();
cheatTypeIndex = 0;
}
}
else
{
cheatTypeIndex = 0;
}
}
}
private void PlaySoundOnActivate()
{
if (cheatCodeDetector == null)
cheatCodeDetector = FindObjectOfType<CheatCodeDetector>();
if (audioClip != null)
cheatCodeDetector.PlaySound(audioClip);
}
private void DeactivateCheatCode()
{
if (cheatCodeDetector == null)
{
cheatCodeDetector = FindObjectOfType<CheatCodeDetector>();
}
cheatCodeDetector.RemoveCheat(cheatCode);
}
}
protected void Update()
{
// We skip the update if we're removing a cheat so we don't get an error about
// changing a list while we're iterating through it
if (cheatToRemoveThisUpdate!=null)
{
cheats.Remove(cheatToRemoveThisUpdate);
cheatToRemoveThisUpdate = null;
}
else
{
cheats.ForEach(x => x.Update());
}
}
public void RegisterCheat(string cheatCode, Action onActivate, bool deactivateCheatOnUse = false, AudioClip audioClip = null)
{
cheats.Add(new Cheat(cheatCode, audioClip, deactivateCheatOnUse, onActivate));
}
public void RemoveCheat(string cheatCode)
{
var cheatToRemove = cheats.Find(x => x.cheatCode.Contains(cheatCode));
if (cheats.Contains(cheatToRemove))
{
cheatToRemoveThisUpdate = cheatToRemove;
}
else
{
Debug.Log("Attempted to remove a cheat code that isn't currently available");
}
}
public void PlaySound(AudioClip audioClip)
{
if (audioSource == null)
audioSource = GetComponent<AudioSource>();
if (audioSource != null && audioClip != null)
audioSource.PlayOneShot(audioClip);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment