1
This commit is contained in:
36
Unity/Scripts/Entity/Core/EntitiesManager.cs
Normal file
36
Unity/Scripts/Entity/Core/EntitiesManager.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user