This commit is contained in:
CortexCore
2024-06-18 17:57:21 +08:00
parent 82294e44ee
commit d3fd104900
10 changed files with 105 additions and 43 deletions

View File

@@ -5,6 +5,21 @@ namespace BITKit
{ {
public static class UnityMathematicsSupportExtensions public static class UnityMathematicsSupportExtensions
{ {
#region float2
public static float2 ReadFloat2(this BinaryReader reader)
{
return new float2()
{
x = reader.ReadSingle(),
y = reader.ReadSingle(),
};
}
public static void WriteFloat2(this BinaryWriter writer, float2 value)
{
writer.Write(value.x);
writer.Write(value.y);
}
#endregion
#region float3 #region float3
public static float3 ReadFloat3(this BinaryReader reader) public static float3 ReadFloat3(this BinaryReader reader)
{ {

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 39a2cf28bc8a59c4ab975f015c66d39d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,18 @@
using System;
namespace BITKit.Entities
{
[Serializable]
public struct EntitiesNetSyncCommand
{
public byte[] Data;
}
[Serializable]
public struct EntitiesNetSyncBatchCommand
{
public int Length;
public int[] Ids;
public ulong[] AddressablePaths;
public EntitiesNetSyncCommand[] Commands;
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 714df46758a64d36a9ecdea0ac29329a
timeCreated: 1718692935

View File

@@ -51,21 +51,26 @@ namespace BITKit
BIT4Log.Log<AddressableHelper>($"开始注册:{obj}的资源"); BIT4Log.Log<AddressableHelper>($"开始注册:{obj}的资源");
var package = YooAssets.GetPackage(obj); var package = YooAssets.GetPackage(obj);
var assetInfos = package.GetAssetInfos(nameof(IAddressable)); var assetInfos = package.GetAssetInfos(nameof(IAddressable));
BIT4Log.Log<AddressableHelper>($"正在解析:{assetInfos.Length}个资源");
var reportBuilder = new StringBuilder(); var reportBuilder = new StringBuilder();
foreach (var x in assetInfos) foreach (var x in assetInfos)
{ {
var asyncHandle = YooAssets.LoadAssetAsync(x.AssetPath); var asyncHandle = YooAssets.LoadAssetAsync(x.AssetPath);
await asyncHandle; await asyncHandle;
if (asyncHandle.AssetObject is IAddressable addressable) var addressable = asyncHandle.AssetObject switch
{ {
if (PathsById.TryAdd(addressable.AddressableId, addressable.AddressablePath) is false) IAddressable a=>a,
{ MonoBehaviour m=>m.GetComponent<IAddressable>(),
var existing = PathsById[addressable.AddressableId]; GameObject g=>g.GetComponent<IAddressable>(),
BIT4Log.Warning<AddressableHelper>($"{addressable.AddressablePath}的Id:{addressable.AddressableId}已被注册为{existing}"); _ => null
} };
reportBuilder.AppendLine($"{addressable.AddressablePath}的Id:{addressable.AddressableId}"); if (addressable is null) continue;
if (PathsById.TryAdd(addressable.AddressableId, addressable.AddressablePath) is false)
{
var existing = PathsById[addressable.AddressableId];
BIT4Log.Warning<AddressableHelper>($"{addressable.AddressablePath}的Id:{addressable.AddressableId}已被注册为{existing}");
} }
reportBuilder.AppendLine($"{addressable.AddressablePath}的Id:{addressable.AddressableId}");
} }
BIT4Log.Log<AddressableHelper>($"注册:{obj}的资源完成,耗时:{stopWatcher.ElapsedMilliseconds}ms"); BIT4Log.Log<AddressableHelper>($"注册:{obj}的资源完成,耗时:{stopWatcher.ElapsedMilliseconds}ms");
BIT4Log.Log<AddressableHelper>(reportBuilder.ToString()); BIT4Log.Log<AddressableHelper>(reportBuilder.ToString());

View File

@@ -18,9 +18,12 @@ namespace BITKit.Entities
public class Entity : MonoBehaviour, IUnityEntity public class Entity : MonoBehaviour, IUnityEntity
{ {
private readonly GenericEvent genericEvent = new(); private readonly GenericEvent genericEvent = new();
[SerializeField,ReadOnly] private int id;
public int Id
public int Id { get; set; } {
get => id;
set => id = value;
}
public CancellationToken CancellationToken { get; private set; } public CancellationToken CancellationToken { get; private set; }
public IEntityBehavior[] Behaviors { get;private set; } public IEntityBehavior[] Behaviors { get;private set; }
public IEntityComponent[] Components => Behaviors.Cast<IEntityComponent>().ToArray(); public IEntityComponent[] Components => Behaviors.Cast<IEntityComponent>().ToArray();
@@ -251,18 +254,18 @@ namespace BITKit.Entities
public void Set<T>(T value) => genericEvent.Set<T>(value); public void Set<T>(T value) => genericEvent.Set<T>(value);
public void Set<T>(string key = Constant.System.Internal, T value = default) => genericEvent.Set<T>(key, value); public void Set<T>(string key = Constant.System.Internal, T value = default) => genericEvent.Set<T>(key, value);
} }
#if UNITY_EDITOR // #if UNITY_EDITOR
[UnityEditor.CustomEditor(typeof(Entity))] // [UnityEditor.CustomEditor(typeof(Entity))]
public class EntityInspector : BITInspector<Entity> // public class EntityInspector : BITInspector<Entity>
{ // {
public override VisualElement CreateInspectorGUI() // public override VisualElement CreateInspectorGUI()
{ // {
FillDefaultInspector(); // FillDefaultInspector();
CreateSubTitle("Identity"); // CreateSubTitle("Identity");
var idLabel = root.Create<Label>(); // var idLabel = root.Create<Label>();
idLabel.text = agent.Id.ToString(); // idLabel.text = agent.Id.ToString();
return root; // return root;
} // }
} // }
#endif // #endif
} }

View File

@@ -9,6 +9,14 @@ namespace BITKit.Entities
[CustomType(typeof(IEntityBinaryHeader))] [CustomType(typeof(IEntityBinaryHeader))]
public class EntityBinaryHeader : EntityBehavior,IEntityBinaryHeader public class EntityBinaryHeader : EntityBehavior,IEntityBinaryHeader
{ {
[Header(Constant.Header.Debug)]
[SerializeField]
[ReadOnly]private Vector2 traffic;
[SerializeField]
[ReadOnly]private string upTraffic;
[SerializeField]
[ReadOnly]private string downTraffic;
public int Id => (int)Entity.Id; public int Id => (int)Entity.Id;
public IDictionary<int, IEntityBinaryComponent> ComponentDictionary { get; } = public IDictionary<int, IEntityBinaryComponent> ComponentDictionary { get; } =
new Dictionary<int, IEntityBinaryComponent>(); new Dictionary<int, IEntityBinaryComponent>();
@@ -44,7 +52,9 @@ namespace BITKit.Entities
var bytes = ms.ToArray(); var bytes = ms.ToArray();
writer.Write(bytes.Length); writer.Write(bytes.Length);
writer.Write(bytes); writer.Write(bytes);
traffic.x += bytes.Length;
} }
upTraffic = NetUtils.GetFileSize((long)traffic.x);
} }
public void Deserialize(BinaryReader reader) public void Deserialize(BinaryReader reader)
{ {
@@ -67,7 +77,9 @@ namespace BITKit.Entities
using var binaryReader = new BinaryReader(stream); using var binaryReader = new BinaryReader(stream);
//BIT4Log.Log<EntityBinaryHeader>($"id:{id},length:{length},bytes:\n{JsonHelper.Get(bytes)}"); //BIT4Log.Log<EntityBinaryHeader>($"id:{id},length:{length},bytes:\n{JsonHelper.Get(bytes)}");
ComponentDictionary[id].Deserialize(binaryReader); ComponentDictionary[id].Deserialize(binaryReader);
traffic.y += length;
} }
downTraffic = NetUtils.GetFileSize((long)traffic.y);
} }
} }
} }

View File

@@ -10,19 +10,7 @@ using UnityEngine;
namespace BITKit.Entities namespace BITKit.Entities
{ {
[Serializable]
public struct EntitiesNetSyncCommand
{
public byte[] Data;
}
[Serializable]
public struct EntitiesNetSyncBatchCommand
{
public int Length;
public int[] Ids;
public ulong[] AddressablePaths;
public EntitiesNetSyncCommand[] Commands;
}
public class EntitiesNetService : MonoBehaviour public class EntitiesNetService : MonoBehaviour
{ {
@@ -39,7 +27,8 @@ namespace BITKit.Entities
[Inject] [Inject]
private IEntityBinaryHeader _playerHeader; private IEntityBinaryHeader _playerHeader;
[Inject] private IAddressable _playerAddressable; [Inject]
private IAddressable _playerAddressable;
[Inject] [Inject]
private IEntity _playerEntity; private IEntity _playerEntity;
@@ -57,10 +46,16 @@ namespace BITKit.Entities
playerService.OnPlayerInitialized+=OnPlayerInitialized; playerService.OnPlayerInitialized+=OnPlayerInitialized;
playerService.OnPlayerDisposed += OnPlayerDisposed; playerService.OnPlayerDisposed += OnPlayerDisposed;
if (playerService.LocalPlayer)
{
playerService.LocalPlayer.Inject(this);
}
destroyCancellationToken.Register(() => destroyCancellationToken.Register(() =>
{ {
ticker.Remove(Tick); ticker.Remove(Tick);
playerService.OnPlayerInitialized-=OnPlayerInitialized; playerService.OnPlayerInitialized-=OnPlayerInitialized;
playerService.OnPlayerDisposed -= OnPlayerDisposed;
}); });
} }
@@ -121,7 +116,7 @@ namespace BITKit.Entities
} }
if (client.IsConnected is false && server.IsRunningServer is false) return; if (client is {IsConnected:false} && server is {IsRunningServer:false}) return;
using var memoryStream = new MemoryStream(); using var memoryStream = new MemoryStream();
if (client is {IsConnected:true}&& _playerEntity as Entity && _playerHeader != null) if (client is {IsConnected:true}&& _playerEntity as Entity && _playerHeader != null)
{ {

View File

@@ -34,7 +34,7 @@ namespace BITKit.Entities.Player
{ {
x.OnPlayerInitialized(); x.OnPlayerInitialized();
} }
UnityPlayerServiceService.Register((Entity)UnityEntity); UnityPlayerService.Register((Entity)UnityEntity);
} }
public override async void OnDestroyComponent() public override async void OnDestroyComponent()
{ {
@@ -53,7 +53,7 @@ namespace BITKit.Entities.Player
x.OnPlayerDisposed(); x.OnPlayerDisposed();
} }
disposeCancellationTokenSource.Dispose(); disposeCancellationTokenSource.Dispose();
UnityPlayerServiceService.UnRegister((Entity)UnityEntity); UnityPlayerService.UnRegister((Entity)UnityEntity);
} }
public void Serialize(BinaryWriter writer) public void Serialize(BinaryWriter writer)
{ {

View File

@@ -9,7 +9,7 @@ namespace BITKit.Entities.Player
/// Unity玩家服务 /// Unity玩家服务
/// </summary> /// </summary>
[Serializable] [Serializable]
public class UnityPlayerServiceService : IPlayerService public class UnityPlayerService : IPlayerService
{ {
public static event Action<Entity> OnPlayerInitialized; public static event Action<Entity> OnPlayerInitialized;
public static event Action<Entity> OnPlayerDisposed; public static event Action<Entity> OnPlayerDisposed;