48 lines
1.3 KiB
C#
48 lines
1.3 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 interface IEntitesManager
|
|
{
|
|
|
|
}
|
|
public class UnityEntitiesManager:MonoBehaviour
|
|
{
|
|
[RuntimeInitializeOnLoadMethod]
|
|
static void Reload()
|
|
{
|
|
Dictionary.Clear();
|
|
}
|
|
public static readonly ConcurrentDictionary<int, IEntity> Dictionary = new();
|
|
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)
|
|
{
|
|
var instance = Instantiate(Singleton.prefab);
|
|
instance.Id = id;
|
|
return GetOrAdd(id,x=>instance);
|
|
}
|
|
public static IEntity GetOrAdd(int id, Func<int, IEntity> createFacotry) => Dictionary.GetOrAdd(id, createFacotry);
|
|
private static UnityEntitiesManager Singleton;
|
|
public Entity prefab;
|
|
private void Awake()
|
|
{
|
|
Singleton = this;
|
|
}
|
|
}
|
|
} |