Last active
November 20, 2015 00:29
-
-
Save MartinKnopf/875e4bfb63f126da2195 to your computer and use it in GitHub Desktop.
Custom events using CSharp
This file contains 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 UnityEngine; | |
using System; | |
public class EventContainer : MonoBehaviour { | |
public class EmptyEvent { | |
public delegate void EmptyDelegate(); | |
public event EmptyDelegate Event; | |
public void Emit() { if(Event != null) Event(); } | |
} | |
public class IntEvent { | |
public delegate void IntDelegate(int value); | |
public event IntDelegate Event; | |
public void Emit(int value) { if(Event != null) Event(value); } | |
} | |
public static EmptyEvent OnScore; | |
public static IntEvent OnGameOver; | |
void Awake() { | |
OnScore = new EmptyEvent(); | |
OnGameOver = new IntEvent(); | |
} | |
} |
This file contains 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 UnityEngine; | |
public class Score : MonoBehaviour { | |
static int score; | |
void Start() { | |
EventContainer.OnScore.Event += IncScore; | |
} | |
void IncScore() { | |
score++; | |
} | |
} |
This file contains 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 UnityEngine; | |
public class ScorePrinter : MonoBehaviour { | |
void Start() { | |
EventContainer.OnGameOver.Event += ShowScore; | |
} | |
void ShowScore(int score) { | |
print("" + score); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment