BITKit/Src/Unity/Scripts/Entity/Core/IUnityEntity.cs

40 lines
1.3 KiB
C#
Raw Normal View History

2023-06-05 19:57:17 +08:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
2023-11-06 01:17:23 +08:00
using BITKit.Entities;
2023-06-05 19:57:17 +08:00
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace BITKit.Entities
{
/// <summary>Entity接口用于复杂实体</summary>
2023-11-06 01:17:23 +08:00
public interface IUnityEntity :Entities.IEntity,IGenericEvent<string>, IDatabase
2023-06-05 19:57:17 +08:00
{
2023-11-06 01:17:23 +08:00
void AddService<T>(T service);
void AddService(object service);
void AddService(Type type, object service);
2023-06-05 19:57:17 +08:00
}
2023-11-06 01:17:23 +08:00
public class IEntityReader : NetMessageReader<IUnityEntity>
2023-06-05 19:57:17 +08:00
{
2023-11-06 01:17:23 +08:00
public override IUnityEntity ReadBinary(BinaryReader reader)
2023-06-05 19:57:17 +08:00
{
var id = reader.ReadInt32();
var path = reader.ReadString();
2023-06-29 14:57:11 +08:00
var entity = DI.Get<IEntitiesService>().Entities[id];
2023-11-06 01:17:23 +08:00
return (IUnityEntity)entity;
2023-06-05 19:57:17 +08:00
}
2023-11-06 01:17:23 +08:00
public override void WriteBinary(BinaryWriter writer, IUnityEntity value)
2023-06-05 19:57:17 +08:00
{
writer.Write(value.Id);
2023-08-23 01:59:26 +08:00
writer.Write(value.Id);
2023-06-05 19:57:17 +08:00
}
2023-11-06 01:17:23 +08:00
private IUnityEntity Create(int id, string path)
2023-06-05 19:57:17 +08:00
{
var entity = Addressables
.LoadAssetAsync<GameObject>(path)
.WaitForCompletion()
2023-11-06 01:17:23 +08:00
.GetComponent<IUnityEntity>();
2023-06-05 19:57:17 +08:00
return entity;
}
}
}