-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuFrameComponent.cs
More file actions
89 lines (75 loc) · 2.79 KB
/
uFrameComponent.cs
File metadata and controls
89 lines (75 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;
using System.Collections;
using UniRx;
using UnityEngine;
namespace uFrame.Kernel
{
/// <summary>
/// The uFrameComponent is a simple class that extends from MonoBehaviour, and is directly plugged into the kernel.
/// Use this component when creating any components manually or if you need to plug existing libraries into the uFrame system.
/// <example>
/// public class MyComponent : uFrameComponent {
/// }
/// </example></summary>
/// <example>
/// <para>public class MyComponent : uFrameComponent {</para>
/// <para> public override void KernelLoaded() {</para>
/// <para> this.Publish(new MyComponentCreatedEvent() { Instance = this });</para>
/// <para> }</para>
/// <para>}</para>
/// </example>
public class uFrameComponent : MonoBehaviour, IDisposableContainer
{
private CompositeDisposable _disposer;
CompositeDisposable IDisposableContainer.Disposer
{
get { return _disposer ?? (_disposer = new CompositeDisposable()); }
set { _disposer = value; }
}
protected virtual void OnDestroy()
{
if (_disposer != null)
{
_disposer.Dispose();
}
}
protected IEventAggregator EventAggregator
{
get { return uFrameKernel.EventAggregator; }
}
/// <summary>Wait for an Event to occur on the global event aggregator.</summary>
/// <example>
/// this.OnEvent<MyEventClass>().Subscribe(myEventClassInstance=>{ DO_SOMETHING_HERE });
/// </example>
public IObservable<TEvent> OnEvent<TEvent>()
{
return EventAggregator.GetEvent<TEvent>();
}
/// <summary>Publishes a command to the event aggregator. Publish the class data you want, and let any "OnEvent" subscriptions handle them.</summary>
/// <example>
/// this.Publish(new MyEventClass() { Message = "Hello World" });
/// </example>
public void Publish(object eventMessage)
{
EventAggregator.Publish(eventMessage);
}
protected virtual IEnumerator Start()
{
KernelLoading();
while (!uFrameKernel.IsKernelLoaded) yield return null;
KernelLoaded();
}
/// <summary>
/// Before we wait for the kernel to load, even if the kernel is already loaded it will still invoke this before it attempts to wait.
/// </summary>
public virtual void KernelLoading()
{
}
/// <summary>
/// The first method to execute when we are sure the kernel has completed loading.
/// </summary>
public virtual void KernelLoaded()
{
}
}
}