Documentation for the Unity C# Library
Loading...
Searching...
No Matches
ServiceLocator.cs
Go to the documentation of this file.
1using System.Collections.Generic;
2using System.Linq;
3
4namespace PixoVR.Core
5{
9 public static class ServiceLocator
10 {
11 private static readonly HashSet<object> _registeredObjects;
12
14 {
15 _registeredObjects = new HashSet<object>();
16 }
17
23 public static void Register<T>(T obj) where T : class
24 {
25 _registeredObjects.Add(obj);
26 }
27
28 public static void Unregister<T>(T obj) where T : class
29 {
30 _registeredObjects.Remove(obj);
31 }
32
38 public static T Resolve<T>() where T : class
39 {
40 var obj = _registeredObjects.SingleOrDefault(x => x is T);
41 if (obj != null)
42 return obj as T;
43
44 return null;
45 }
46
47 public static void UnregisterAll()
48 {
49 _registeredObjects.Clear();
50 }
51 }
52}
Unity components communication service.
static void Register< T >(T obj)
Registering a component for further reference to it.
static T Resolve< T >()
Get scene object by component type.
static void Unregister< T >(T obj)
static readonly HashSet< object > _registeredObjects