-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLazyService.cs
52 lines (41 loc) · 1017 Bytes
/
LazyService.cs
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
using UnityEngine;
namespace RenderHeads.Services
{
public struct LazyService<T> where T: Service
{
private LazyService(T service)
{
_value = service;
}
public void ForceGetService()
{
_value = ServiceLocator.GetService<T>();
}
public bool IsAssigned =>_value != null;
private T _value;
public T Value
{
get
{
if (_value == null)
{
_value = ServiceLocator.GetService<T>();
}
Debug.Assert(_value != null, $"[LazyService<{typeof(T)}>] Could not get service. Either it does not exist in any loaded scene, or it has not been subscribed to the ServiceLocator. This could happen if Value is called before OnEnable or the objet that holds the Service has been disabled");
return _value;
}
set
{
_value = value;
}
}
public static implicit operator T(LazyService<T> lazy)
{
return lazy.Value;
}
//public static implicit operator LazyService<T>(T service)
//{
// return new LazyService<T>(service);
//}
}
}