Skip to content

Instantly share code, notes, and snippets.

@dugdaniels
Created September 28, 2017 17:00
Show Gist options
  • Save dugdaniels/bf67ea2be6b84dcdf44431cf87cd0059 to your computer and use it in GitHub Desktop.
Save dugdaniels/bf67ea2be6b84dcdf44431cf87cd0059 to your computer and use it in GitHub Desktop.
The Command pattern in Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public interface Command {
void Execute();
}
public class Jump : Command {
string messageToLog;
public Jump(string messageToLog) {
this.messageToLog = messageToLog;
}
public void Execute(){
Debug.Log(messageToLog);
}
}
public class Commands : MonoBehaviour {
Command spaceCommand;
void Start () {
// Methods to assign commands
spaceCommand = new Jump("Boing!");
}
void Update () {
if (Input.GetKeyDown(KeyCode.Space)) {
spaceCommand.Execute();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment