Created
December 10, 2017 06:00
-
-
Save yuw-unknown/8aca16fd3d48f2ca05c8ae9233efca3d to your computer and use it in GitHub Desktop.
Notification-EventBus sample on Unity.
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 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