Skip to content

Instantly share code, notes, and snippets.

@MartinKnopf
Last active November 20, 2015 00:29
Show Gist options
  • Save MartinKnopf/875e4bfb63f126da2195 to your computer and use it in GitHub Desktop.
Save MartinKnopf/875e4bfb63f126da2195 to your computer and use it in GitHub Desktop.
Custom events using CSharp
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();
}
}
using UnityEngine;
public class Score : MonoBehaviour {
static int score;
void Start() {
EventContainer.OnScore.Event += IncScore;
}
void IncScore() {
score++;
}
}
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