Created
September 28, 2017 17:00
-
-
Save dugdaniels/bf67ea2be6b84dcdf44431cf87cd0059 to your computer and use it in GitHub Desktop.
The Command pattern in Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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