Skip to content

Draft/implement eventsystem #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Assets/TierIV.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/TierIV/Event.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Assets/TierIV/Event/Scripts.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
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(EventArgsBase chargeEvent)
{
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
}
}
11 changes: 11 additions & 0 deletions Assets/TierIV/Event/Scripts/BatteryObjectEvent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 33 additions & 0 deletions Assets/TierIV/Event/Scripts/ChargeStationEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TierIV.Event;

public class ChargeStationEvent : MonoBehaviour
{
public class BatteryVolume : EventArgsBase
{
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", volume);
}

}
11 changes: 11 additions & 0 deletions Assets/TierIV/Event/Scripts/ChargeStationEvent.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions Assets/TierIV/Event/Scripts/EventArgsBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

public class EventArgsBase
{
private int typeHash;

public int TypeHash
{
get { return typeHash; }
}

public EventArgsBase()
{
typeHash = this.GetType().GetHashCode();
}
}
11 changes: 11 additions & 0 deletions Assets/TierIV/Event/Scripts/EventArgsBase.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions Assets/TierIV/Event/Scripts/EventNotifier.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
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(EventArgsBase text);
event OnNotifyEvent _notifyEvent;
Dictionary<string, OnNotifyEvent> _evt = new Dictionary<string, OnNotifyEvent>();

public void SubscribeEvent(OnNotifyEvent notifyEventHandler, string tag = "")
{
Debug.LogFormat("SubscribeEvent({0},{1})", notifyEventHandler, tag);
if (_evt.ContainsKey(tag))
{
_evt[tag] += notifyEventHandler;
}
else
{
_evt.Add(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(EventArgsBase value)
{
BroadcastEvent(null, value);
}

public void BroadcastEvent(string tag, EventArgsBase value)
{
List<OnNotifyEvent> handlerList = new List<OnNotifyEvent>();

// tag == null is all handler message send
if (tag == null)
{
handlerList.AddRange(_evt.Values);
}
else
{
handlerList.Add(_evt[tag]);
}

foreach (var handler in handlerList)
{
handler(value);
}
}
}
}
11 changes: 11 additions & 0 deletions Assets/TierIV/Event/Scripts/EventNotifier.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.