From 7946cd68837181be47db90086e8f66d3b02bda61 Mon Sep 17 00:00:00 2001 From: "kaichi.oda" Date: Thu, 10 Oct 2019 15:31:05 +0900 Subject: [PATCH 1/4] add event notifyer --- Assets/TierIV.meta | 8 ++ Assets/TierIV/Event.meta | 8 ++ Assets/TierIV/Event/Scripts.meta | 8 ++ Assets/TierIV/Event/Scripts/EventNotifier.cs | 76 +++++++++++++++++++ .../Event/Scripts/EventNotifier.cs.meta | 11 +++ 5 files changed, 111 insertions(+) create mode 100644 Assets/TierIV.meta create mode 100644 Assets/TierIV/Event.meta create mode 100644 Assets/TierIV/Event/Scripts.meta create mode 100644 Assets/TierIV/Event/Scripts/EventNotifier.cs create mode 100644 Assets/TierIV/Event/Scripts/EventNotifier.cs.meta diff --git a/Assets/TierIV.meta b/Assets/TierIV.meta new file mode 100644 index 000000000..e88e79d4f --- /dev/null +++ b/Assets/TierIV.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9d04975e5ec319a4cbcf378088bf69de +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TierIV/Event.meta b/Assets/TierIV/Event.meta new file mode 100644 index 000000000..4a38d1de1 --- /dev/null +++ b/Assets/TierIV/Event.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 160b0111f376f4b45b482514732b26be +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TierIV/Event/Scripts.meta b/Assets/TierIV/Event/Scripts.meta new file mode 100644 index 000000000..436b91b41 --- /dev/null +++ b/Assets/TierIV/Event/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d4b25c5289f00ba4cacf32cefd800021 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TierIV/Event/Scripts/EventNotifier.cs b/Assets/TierIV/Event/Scripts/EventNotifier.cs new file mode 100644 index 000000000..cdc3b4f5b --- /dev/null +++ b/Assets/TierIV/Event/Scripts/EventNotifier.cs @@ -0,0 +1,76 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; + +namespace TierIV.Event +{ + public class EventNotifier + { + private static EventNotifier _instance = null; + + public static EventNotifier Instance + { + get + { + if (_instance == null) + { + _instance = new EventNotifier(); + } + + return _instance; + } + } + + private EventNotifier() + { + } + + public delegate void OnNotifyEvent(string text); + event OnNotifyEvent _notifyEvent; + Dictionary _evt; + + public void SubscribeEvent(OnNotifyEvent notifyEventHandler, string tag = "") + { + _evt[tag] += notifyEventHandler; + + _notifyEvent += notifyEventHandler; + } + + public void UnSubscribeEvent(OnNotifyEvent notifyEventHandler) + { + foreach (var v in _evt) + { + var eh = v.Value; + eh -= notifyEventHandler; + _evt[v.Key] = eh; + } + } + + public void BroadcastEvent(string value) + { + BroadcastEvent(null, value); + } + + public void BroadcastEvent(string tag, string value) + { + List handlerList = new List(); + + // tag == null is all message send + if (tag == null) + { + // + handlerList.AddRange(_evt.Values); + } + else + { + handlerList.Add(_evt[tag]); + } + + foreach (var handler in handlerList) + { + handler(value); + } + } + } +} diff --git a/Assets/TierIV/Event/Scripts/EventNotifier.cs.meta b/Assets/TierIV/Event/Scripts/EventNotifier.cs.meta new file mode 100644 index 000000000..9990dc72a --- /dev/null +++ b/Assets/TierIV/Event/Scripts/EventNotifier.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 79effb4bb5ee92e4e8310dffbe954d3f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From 2b120a3c0fa004eb698c0df0f1270b6b67288888 Mon Sep 17 00:00:00 2001 From: "kaichi.oda" Date: Thu, 10 Oct 2019 15:31:45 +0900 Subject: [PATCH 2/4] event notify sample impementation --- .../Event/Scripts/BatteryObjectEvent.cs | 28 ++++++++++++++++ .../Event/Scripts/BatteryObjectEvent.cs.meta | 11 +++++++ .../Event/Scripts/ChargeStationEvent.cs | 33 +++++++++++++++++++ .../Event/Scripts/ChargeStationEvent.cs.meta | 11 +++++++ 4 files changed, 83 insertions(+) create mode 100644 Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs create mode 100644 Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs.meta create mode 100644 Assets/TierIV/Event/Scripts/ChargeStationEvent.cs create mode 100644 Assets/TierIV/Event/Scripts/ChargeStationEvent.cs.meta diff --git a/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs b/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs new file mode 100644 index 000000000..3630b07de --- /dev/null +++ b/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs @@ -0,0 +1,28 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using TierIV.Event; + + +public class BatteryObjectEvent : MonoBehaviour +{ + // Start is called before the first frame update + void Start() + { + } + + void OnEnable() + { + EventNotifier.Instance.SubscribeEvent(OnReceiveChargeEvent, "Charge"); + } + + void OnDisable() + { + EventNotifier.Instance.UnSubscribeEvent(OnReceiveChargeEvent); + } + + void OnReceiveChargeEvent(string chargeEventJson) + { + // parse chargeEventJson to get an appropriate value ... + } +} diff --git a/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs.meta b/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs.meta new file mode 100644 index 000000000..bf67b3966 --- /dev/null +++ b/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6f3415d843c7eff44930a3d4858b8ab6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs b/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs new file mode 100644 index 000000000..c97dac3b2 --- /dev/null +++ b/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs @@ -0,0 +1,33 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using TierIV.Event; + +public class ChargeStationEvent : MonoBehaviour +{ + struct BatteryVolume + { + public float parcentage; + } + + BatteryVolume volume = new BatteryVolume { parcentage = 10f }; + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } + + void OnTriggerStay(Collider target) + { + // targetに通知 + EventNotifier.Instance.BroadcastEvent("Charge", JsonUtility.ToJson(volume)); + } + +} \ No newline at end of file diff --git a/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs.meta b/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs.meta new file mode 100644 index 000000000..d5bc79fbe --- /dev/null +++ b/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8fdb797a77ed1d2408e3240ea6ad6bfb +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: From baa2b5a8f7727ee9fb12a5e6bac0060fbc32f4c4 Mon Sep 17 00:00:00 2001 From: "kaichi.oda" Date: Thu, 10 Oct 2019 18:44:47 +0900 Subject: [PATCH 3/4] comment change CRLF to LF --- .../Event/Scripts/BatteryObjectEvent.cs | 56 +++---- .../Event/Scripts/ChargeStationEvent.cs | 64 ++++---- Assets/TierIV/Event/Scripts/EventNotifier.cs | 152 +++++++++--------- 3 files changed, 136 insertions(+), 136 deletions(-) diff --git a/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs b/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs index 3630b07de..40e1d0306 100644 --- a/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs +++ b/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs @@ -1,28 +1,28 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using TierIV.Event; - - -public class BatteryObjectEvent : MonoBehaviour -{ - // Start is called before the first frame update - void Start() - { - } - - void OnEnable() - { - EventNotifier.Instance.SubscribeEvent(OnReceiveChargeEvent, "Charge"); - } - - void OnDisable() - { - EventNotifier.Instance.UnSubscribeEvent(OnReceiveChargeEvent); - } - - void OnReceiveChargeEvent(string chargeEventJson) - { - // parse chargeEventJson to get an appropriate value ... - } -} +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using TierIV.Event; + + +public class BatteryObjectEvent : MonoBehaviour +{ + // Start is called before the first frame update + void Start() + { + } + + void OnEnable() + { + EventNotifier.Instance.SubscribeEvent(OnReceiveChargeEvent, "Charge"); + } + + void OnDisable() + { + EventNotifier.Instance.UnSubscribeEvent(OnReceiveChargeEvent); + } + + void OnReceiveChargeEvent(string chargeEventJson) + { + // parse chargeEventJson to get an appropriate value ... + } +} diff --git a/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs b/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs index c97dac3b2..02e90c6fc 100644 --- a/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs +++ b/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs @@ -1,33 +1,33 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using TierIV.Event; - -public class ChargeStationEvent : MonoBehaviour -{ - struct BatteryVolume - { - public float parcentage; - } - - BatteryVolume volume = new BatteryVolume { parcentage = 10f }; - - // Start is called before the first frame update - void Start() - { - - } - - // Update is called once per frame - void Update() - { - - } - - void OnTriggerStay(Collider target) - { - // targetに通知 - EventNotifier.Instance.BroadcastEvent("Charge", JsonUtility.ToJson(volume)); - } - +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using TierIV.Event; + +public class ChargeStationEvent : MonoBehaviour +{ + struct BatteryVolume + { + public float parcentage; + } + + BatteryVolume volume = new BatteryVolume { parcentage = 10f }; + + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } + + void OnTriggerStay(Collider target) + { + // send to event + EventNotifier.Instance.BroadcastEvent("Charge", JsonUtility.ToJson(volume)); + } + } \ No newline at end of file diff --git a/Assets/TierIV/Event/Scripts/EventNotifier.cs b/Assets/TierIV/Event/Scripts/EventNotifier.cs index cdc3b4f5b..a6ed2b84b 100644 --- a/Assets/TierIV/Event/Scripts/EventNotifier.cs +++ b/Assets/TierIV/Event/Scripts/EventNotifier.cs @@ -1,76 +1,76 @@ -using System.Collections; -using System.Collections.Generic; -using UnityEngine; -using UnityEngine.EventSystems; - -namespace TierIV.Event -{ - public class EventNotifier - { - private static EventNotifier _instance = null; - - public static EventNotifier Instance - { - get - { - if (_instance == null) - { - _instance = new EventNotifier(); - } - - return _instance; - } - } - - private EventNotifier() - { - } - - public delegate void OnNotifyEvent(string text); - event OnNotifyEvent _notifyEvent; - Dictionary _evt; - - public void SubscribeEvent(OnNotifyEvent notifyEventHandler, string tag = "") - { - _evt[tag] += notifyEventHandler; - - _notifyEvent += notifyEventHandler; - } - - public void UnSubscribeEvent(OnNotifyEvent notifyEventHandler) - { - foreach (var v in _evt) - { - var eh = v.Value; - eh -= notifyEventHandler; - _evt[v.Key] = eh; - } - } - - public void BroadcastEvent(string value) - { - BroadcastEvent(null, value); - } - - public void BroadcastEvent(string tag, string value) - { - List handlerList = new List(); - - // tag == null is all message send - if (tag == null) - { - // - handlerList.AddRange(_evt.Values); - } - else - { - handlerList.Add(_evt[tag]); - } - - foreach (var handler in handlerList) - { - handler(value); - } - } - } -} +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.EventSystems; + +namespace TierIV.Event +{ + public class EventNotifier + { + private static EventNotifier _instance = null; + + public static EventNotifier Instance + { + get + { + if (_instance == null) + { + _instance = new EventNotifier(); + } + + return _instance; + } + } + + private EventNotifier() + { + } + + public delegate void OnNotifyEvent(string text); + event OnNotifyEvent _notifyEvent; + Dictionary _evt; + + public void SubscribeEvent(OnNotifyEvent notifyEventHandler, string tag = "") + { + _evt[tag] += notifyEventHandler; + + _notifyEvent += notifyEventHandler; + } + + public void UnSubscribeEvent(OnNotifyEvent notifyEventHandler) + { + foreach (var v in _evt) + { + var eh = v.Value; + eh -= notifyEventHandler; + _evt[v.Key] = eh; + } + } + + public void BroadcastEvent(string value) + { + BroadcastEvent(null, value); + } + + public void BroadcastEvent(string tag, string value) + { + List handlerList = new List(); + + // tag == null is all message send + if (tag == null) + { + // + handlerList.AddRange(_evt.Values); + } + else + { + handlerList.Add(_evt[tag]); + } + + foreach (var handler in handlerList) + { + handler(value); + } + } + } +} From d8608e02e6feb077125b7ba7ff44154ef5ee1dc0 Mon Sep 17 00:00:00 2001 From: "kaichi.oda" Date: Wed, 16 Oct 2019 15:17:32 +0900 Subject: [PATCH 4/4] event receive arguments type set --- .../Event/Scripts/BatteryObjectEvent.cs | 14 +++++++++++-- .../Event/Scripts/ChargeStationEvent.cs | 4 ++-- Assets/TierIV/Event/Scripts/EventArgsBase.cs | 15 +++++++++++++ .../Event/Scripts/EventArgsBase.cs.meta | 11 ++++++++++ Assets/TierIV/Event/Scripts/EventNotifier.cs | 21 ++++++++++++------- 5 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 Assets/TierIV/Event/Scripts/EventArgsBase.cs create mode 100644 Assets/TierIV/Event/Scripts/EventArgsBase.cs.meta diff --git a/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs b/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs index 40e1d0306..713b13f37 100644 --- a/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs +++ b/Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs @@ -21,8 +21,18 @@ void OnDisable() EventNotifier.Instance.UnSubscribeEvent(OnReceiveChargeEvent); } - void OnReceiveChargeEvent(string chargeEventJson) + void OnReceiveChargeEvent(EventArgsBase chargeEvent) { - // parse chargeEventJson to get an appropriate value ... + if (typeof(ChargeStationEvent.BatteryVolume).GetHashCode() != chargeEvent.TypeHash) + { + throw new System.ArgumentException(string.Format("different class hashcode {0}({1}) / {2}({3})", + typeof(ChargeStationEvent.BatteryVolume).GetHashCode(), + typeof(EventArgsBase).Name, + chargeEvent.TypeHash, + chargeEvent.GetType().Name + )); + } + + // receive chargeEvent values to appropriate proces } } diff --git a/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs b/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs index 02e90c6fc..73278b93a 100644 --- a/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs +++ b/Assets/TierIV/Event/Scripts/ChargeStationEvent.cs @@ -5,7 +5,7 @@ public class ChargeStationEvent : MonoBehaviour { - struct BatteryVolume + public class BatteryVolume : EventArgsBase { public float parcentage; } @@ -27,7 +27,7 @@ void Update() void OnTriggerStay(Collider target) { // send to event - EventNotifier.Instance.BroadcastEvent("Charge", JsonUtility.ToJson(volume)); + EventNotifier.Instance.BroadcastEvent("Charge", volume); } } \ No newline at end of file diff --git a/Assets/TierIV/Event/Scripts/EventArgsBase.cs b/Assets/TierIV/Event/Scripts/EventArgsBase.cs new file mode 100644 index 000000000..2d65d9e52 --- /dev/null +++ b/Assets/TierIV/Event/Scripts/EventArgsBase.cs @@ -0,0 +1,15 @@ + +public class EventArgsBase +{ + private int typeHash; + + public int TypeHash + { + get { return typeHash; } + } + + public EventArgsBase() + { + typeHash = this.GetType().GetHashCode(); + } +} diff --git a/Assets/TierIV/Event/Scripts/EventArgsBase.cs.meta b/Assets/TierIV/Event/Scripts/EventArgsBase.cs.meta new file mode 100644 index 000000000..236449e1b --- /dev/null +++ b/Assets/TierIV/Event/Scripts/EventArgsBase.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1ef4d3fc5570c9c42bb6a1ef9aa224b2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/TierIV/Event/Scripts/EventNotifier.cs b/Assets/TierIV/Event/Scripts/EventNotifier.cs index a6ed2b84b..96fa39ce8 100644 --- a/Assets/TierIV/Event/Scripts/EventNotifier.cs +++ b/Assets/TierIV/Event/Scripts/EventNotifier.cs @@ -26,13 +26,21 @@ private EventNotifier() { } - public delegate void OnNotifyEvent(string text); + public delegate void OnNotifyEvent(EventArgsBase text); event OnNotifyEvent _notifyEvent; - Dictionary _evt; + Dictionary _evt = new Dictionary(); public void SubscribeEvent(OnNotifyEvent notifyEventHandler, string tag = "") { - _evt[tag] += notifyEventHandler; + Debug.LogFormat("SubscribeEvent({0},{1})", notifyEventHandler, tag); + if (_evt.ContainsKey(tag)) + { + _evt[tag] += notifyEventHandler; + } + else + { + _evt.Add(tag, notifyEventHandler); + } _notifyEvent += notifyEventHandler; } @@ -47,19 +55,18 @@ public void UnSubscribeEvent(OnNotifyEvent notifyEventHandler) } } - public void BroadcastEvent(string value) + public void BroadcastEvent(EventArgsBase value) { BroadcastEvent(null, value); } - public void BroadcastEvent(string tag, string value) + public void BroadcastEvent(string tag, EventArgsBase value) { List handlerList = new List(); - // tag == null is all message send + // tag == null is all handler message send if (tag == null) { - // handlerList.AddRange(_evt.Values); } else