Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Notification-EventBus sample on Unity.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Notification {
static Notification _Instance;
public static Notification Instance {
get {
if (_Instance == null) {
_Instance = new Notification();
}
return _Instance;
}
}
private Notification() {
}
// -----------------------------------------
// Notification/EventBus Sample (通知時の引数無し)
// -----------------------------------------
public delegate void OnSomeEvent();
event OnSomeEvent _OnSomeEvent;
/// <summary>
/// 通知購読
/// </summary>
/// <param name="onEvent"></param>
public void SubscriveSomeEvent(OnEvent onEvent) {
_OnEvent += onEvent;
}
/// <summary>
/// 通知購読解除
/// </summary>
/// <param name="onEvent"></param>
public void UnSubscribeSomeEvent(OnEvent onEvent) {
_OnEvent -= onEvent;
}
/// <summary>
/// 通知
/// </summary>
public void NotifySomeEvent() {
if (_OnSomeEvent != null) {
_OnSomeEvent();
}
}
// -----------------------------------------
// Notification/EventBus Sample (通知時の引数あり)
// -----------------------------------------
public delegate void OnSomeEventWithArgs(string text);
event OnSomeEventWithArgs _OnSomeEventWithArgs;
/// <summary>
/// 通知購読
/// </summary>
/// <param name="onEvent"></param>
public void SubscriveSomeEventWithArgs(OnEventWithArgs onEventWithArgs) {
_OnEventWithArgs += onEventWithArgs;
}
/// <summary>
/// 通知購読解除
/// </summary>
/// <param name="onEvent"></param>
public void UnSubscribeSomeEventWithArgs(OnEventWithArgs onEventWithArgs) {
_OnEventWithArgs -= onEventWithArgs;
}
/// <summary>
/// 通知
/// 通知のタイミングでstringを渡します
/// </summary>
public void NotifySomeEventWithArgs(string text) {
if (_OnSomeEventWithArgs != null) {
_OnSomeEventWithArgs(text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment