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
{
#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
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}的资源");
var package = YooAssets.GetPackage(obj);
var assetInfos = package.GetAssetInfos(nameof(IAddressable));
BIT4Log.Log<AddressableHelper>($"正在解析:{assetInfos.Length}个资源");
var reportBuilder = new StringBuilder();
foreach (var x in assetInfos)
{
var asyncHandle = YooAssets.LoadAssetAsync(x.AssetPath);
await asyncHandle;
if (asyncHandle.AssetObject is IAddressable addressable)
var addressable = asyncHandle.AssetObject switch
{
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}");
IAddressable a=>a,
MonoBehaviour m=>m.GetComponent<IAddressable>(),
GameObject g=>g.GetComponent<IAddressable>(),
_ => null
};
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>(reportBuilder.ToString());

View File

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

View File

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

View File

@ -10,19 +10,7 @@ using UnityEngine;
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
{
@ -39,7 +27,8 @@ namespace BITKit.Entities
[Inject]
private IEntityBinaryHeader _playerHeader;
[Inject] private IAddressable _playerAddressable;
[Inject]
private IAddressable _playerAddressable;
[Inject]
private IEntity _playerEntity;
@ -57,10 +46,16 @@ namespace BITKit.Entities
playerService.OnPlayerInitialized+=OnPlayerInitialized;
playerService.OnPlayerDisposed += OnPlayerDisposed;
if (playerService.LocalPlayer)
{
playerService.LocalPlayer.Inject(this);
}
destroyCancellationToken.Register(() =>
{
ticker.Remove(Tick);
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();
if (client is {IsConnected:true}&& _playerEntity as Entity && _playerHeader != null)
{

View File

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

View File

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