54 lines
1.2 KiB
C#
54 lines
1.2 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Main;
|
|
using UnityEngine;
|
|
|
|
public static class PointCloudService
|
|
{
|
|
private static readonly Dictionary<Type, object> Services = new Dictionary<Type, object>();
|
|
|
|
static PointCloudService()
|
|
{
|
|
Services.Clear();
|
|
|
|
}
|
|
public static void RegisterService<T>(T service)
|
|
{
|
|
var type = typeof(T);
|
|
if (Services.ContainsKey(type))
|
|
{
|
|
Services[type] = service;
|
|
}
|
|
else
|
|
{
|
|
Services.Add(type, service);
|
|
}
|
|
}
|
|
|
|
public static T GetService<T>()
|
|
{
|
|
var type = typeof(T);
|
|
if (Services.TryGetValue(type, out var service))
|
|
{
|
|
return (T)service;
|
|
}
|
|
else
|
|
{
|
|
// 抛出异常或返回空服务
|
|
throw new Exception($"Service of type 【{type}】 not found, please check if the service is registered.");
|
|
}
|
|
}
|
|
public static void UnregisterService<T>()
|
|
{
|
|
var type = typeof(T);
|
|
if (Services.ContainsKey(type))
|
|
{
|
|
Services.Remove(type);
|
|
}
|
|
}
|
|
public static void ClearServices()
|
|
{
|
|
Services.Clear();
|
|
}
|
|
} |