66 lines
1.9 KiB
C#
66 lines
1.9 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using BITKit.Entities;
|
|
using BITKit.Mod;
|
|
using Cysharp.Threading.Tasks;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging;
|
|
using UnityEngine;
|
|
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
|
|
|
namespace BITKit.IO
|
|
{
|
|
public class ScriptableEntitiesService : IDisposable
|
|
{
|
|
private readonly IEntitiesService _entitiesService;
|
|
|
|
public string Tags = "scriptable_object";
|
|
|
|
private readonly List<IEntity> _registeredEntities = new();
|
|
|
|
public ScriptableEntitiesService(IEntitiesService entitiesService)
|
|
{
|
|
_entitiesService = entitiesService;
|
|
}
|
|
|
|
public async UniTask InitializeAsync(ILogger logger = null)
|
|
{
|
|
logger?.LogInformation("正在查找所有ScriptableObject...");
|
|
var objs = await ModService.LoadAssets<ScriptableObject>(Tags);
|
|
logger?.LogInformation($"找到{objs.Count}个资源,正在加载中...");
|
|
for (var index = 0; index < objs.Count; index++)
|
|
{
|
|
var scriptableObject = objs[index];
|
|
var entity = new Entity();
|
|
|
|
var idComponent = new IdComponent();
|
|
|
|
entity.ServiceCollection.AddSingleton(idComponent);
|
|
|
|
var type = scriptableObject.GetType();
|
|
|
|
entity.ServiceCollection.AddSingleton(type, scriptableObject);
|
|
|
|
_entitiesService.Register(entity);
|
|
|
|
logger?.LogInformation($"已加载:{scriptableObject.name}:{type.Name},剩余:{index + 1}/{objs.Count}");
|
|
_registeredEntities.Add(entity);
|
|
}
|
|
|
|
logger?.LogInformation("加载完成");
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
foreach (var x in _registeredEntities)
|
|
{
|
|
_entitiesService.UnRegister(x);
|
|
}
|
|
|
|
_registeredEntities.Clear();
|
|
}
|
|
}
|
|
|
|
}
|