BITKit/Packages/Runtime/Entity/Core/IEntity.cs

42 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.AddressableAssets;
namespace BITKit.Entities
{
/// <summary>Entity接口用于复杂实体</summary>
public interface IEntity : IGenericEvent<string>, IDatabase, IProcessor, ICallback
{
public static IEntity LocalPlayer;
public static Action<IEntity> OnSpawnLocalPlayer;
IEntityComponent[] entityComponents { get; set; }
int Id { get; set; }
string AddressablePath => nameof(IEntity);
}
public class IEntityReader : NetMessageReader<IEntity>
{
public override IEntity ReadBinary(BinaryReader reader)
{
var id = reader.ReadInt32();
var path = reader.ReadString();
var entity = EntitiesManager.GetOrAdd(id, _id => Create(id, path));
return entity;
}
public override void WriteBinary(BinaryWriter writer, IEntity value)
{
writer.Write(value.Id);
writer.Write(value.AddressablePath);
}
IEntity Create(int id, string path)
{
var entity = Addressables
.LoadAssetAsync<GameObject>(path)
.WaitForCompletion()
.GetComponent<IEntity>();
entity.Id = id;
return entity;
}
}
}