-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceLocator.cs
More file actions
39 lines (30 loc) · 886 Bytes
/
ServiceLocator.cs
File metadata and controls
39 lines (30 loc) · 886 Bytes
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
using System;
using System.Collections.Generic;
using UnityEngine;
public static class ServiceLocator
{
private static readonly Dictionary<Type, object> _services = new();
public static void Register<T>(T service)
{
var type = typeof(T);
if (_services.ContainsKey(type))
{
Debug.LogWarning($"ServiceLocator: {type.Name} zaten kayıtlı.");
return;
}
_services[type] = service;
}
public static T Get<T>()
{
var type = typeof(T);
if (_services.TryGetValue(type, out var service))
return (T)service;
throw new Exception($"ServiceLocator: {type.Name} servisi bulunamadı.");
}
public static void Unregister<T>()
{
var type = typeof(T);
_services.Remove(type);
}
public static void Clear() => _services.Clear();
}