Skip to content

Instantly share code, notes, and snippets.

@yuw-unknown
Created December 10, 2017 06:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yuw-unknown/8aca16fd3d48f2ca05c8ae9233efca3d to your computer and use it in GitHub Desktop.
Save yuw-unknown/8aca16fd3d48f2ca05c8ae9233efca3d to your computer and use it in GitHub Desktop.
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