36 lines
1.0 KiB
C#
36 lines
1.0 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.AddressableAssets;
|
|
using BITKit;
|
|
using System.Collections.Concurrent;
|
|
namespace BITKit.Entities
|
|
{
|
|
public class EntitiesManager
|
|
{
|
|
[RuntimeInitializeOnLoadMethod]
|
|
static void Reload()
|
|
{
|
|
Dictionary.Clear();
|
|
}
|
|
public static ConcurrentDictionary<int, IEntity> Dictionary = new();
|
|
public static Func<int, IEntity> CreateFactory;
|
|
public static IEntity Get(int id)
|
|
{
|
|
if (Dictionary.TryGetValue(id, out var entity))
|
|
{
|
|
return entity;
|
|
}
|
|
else
|
|
{
|
|
throw new System.NullReferenceException($"没有找到id为{id}的iEntity");
|
|
}
|
|
}
|
|
public static IEntity GetOrAdd(int id)
|
|
{
|
|
return GetOrAdd(id, CreateFactory);
|
|
}
|
|
public static IEntity GetOrAdd(int id, Func<int, IEntity> createFacotry) => Dictionary.GetOrAdd(id, createFacotry);
|
|
}
|
|
} |