This commit is contained in:
CortexCore 2024-06-17 14:38:09 +08:00
parent 3d447499fe
commit 65f9e40105
8 changed files with 86 additions and 14 deletions

View File

@ -11,6 +11,7 @@ using System.Numerics;
using System.Reflection;
using BITKit.Net.Examples;
using Newtonsoft.Json;
using Unity.Mathematics;
namespace BITKit.Net
{
@ -21,7 +22,7 @@ namespace BITKit.Net
public event Action OnDisconnected;
public event Action OnConnectedFailed;
public bool IsConnected => client.connected;
public float2 Traffic { get; set; }
public bool ManualTick { get; set; }
public int Ping { get; private set; }
@ -111,6 +112,7 @@ namespace BITKit.Net
await Task.Delay(100);
}
_timer.Start();
Traffic+=new float2(1,0);
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
if (BITApp.SynchronizationContext is not null)
await UniTask.SwitchToSynchronizationContext(BITApp.SynchronizationContext);
@ -144,6 +146,7 @@ namespace BITKit.Net
{
try
{
Traffic+=new float2(0,bytes.Count);
OnDataInternel(bytes, channel);
}
catch (Exception e)
@ -187,6 +190,7 @@ namespace BITKit.Net
break;
case NetCommandType.Heartbeat:
Traffic+=new float2(1,0);
client.Send(new[] { (byte)NetCommandType.Heartbeat }, KcpChannel.Reliable);
_isConnected.AddElement(this);
break;
@ -450,6 +454,7 @@ namespace BITKit.Net
.Write(((byte)NetCommandType.Message))
.Write(message)
.Build();
Traffic+=new float2(bytes.Length,0);
client.Send(bytes, KcpChannel.Reliable);
}
@ -463,6 +468,7 @@ namespace BITKit.Net
{
while (commandQueue.TryDequeue(out var bytes))
{
Traffic+=new float2(bytes.Length,0);
client.Send(bytes, KcpChannel.Reliable);
}
@ -486,6 +492,7 @@ namespace BITKit.Net
public void HandShake()
{
// send client to server
Traffic+=new float2(2,0);
client.Send(new byte[]{0x01, 0x02}, KcpChannel.Reliable);
}

View File

@ -19,6 +19,34 @@ namespace BITKit
public sealed class NetRpcAttribute : Attribute
{
}
/// <summary>
/// 帮助类
/// </summary>
public static class NetUtils
{
/// <summary>
/// 计算文件大小函数(保留两位小数),Size为字节大小
/// </summary>
/// <param name="size">初始文件大小</param>
/// <returns></returns>
public static string GetFileSize(long size)
{
var num = 1024.00; //byte
if (size < num)
return size + "B";
if (size < Math.Pow(num, 2))
return (size / num).ToString("f2") + "K"; //kb
if (size < Math.Pow(num, 3))
return (size / Math.Pow(num, 2)).ToString("f2") + "M"; //M
if (size < Math.Pow(num, 4))
return (size / Math.Pow(num, 3)).ToString("f2") + "G"; //G
return (size / Math.Pow(num, 4)).ToString("f2") + "T"; //T
}
}
/// <summary>
/// 网络指令类型

View File

@ -4,7 +4,8 @@
"references": [
"GUID:14fe60d984bf9f84eac55c6ea033a8f4",
"GUID:f51ebe6a0ceec4240a699833d6309b23",
"GUID:49b49c76ee64f6b41bf28ef951cb0e50"
"GUID:49b49c76ee64f6b41bf28ef951cb0e50",
"GUID:d8b63aba1907145bea998dd612889d6b"
],
"includePlatforms": [],
"excludePlatforms": [],

View File

@ -16,6 +16,13 @@ namespace BITKit.Net.Kcp
[SerializeField] private ushort m_port;
[SerializeField] private bool connectOnStart;
[SerializeField] private bool autoReconnect;
[SerializeField]
[ReadOnly]private Vector2 traffic;
[SerializeField]
[ReadOnly]private string upTraffic;
[SerializeField]
[ReadOnly]private string downTraffic;
#if UNITY_EDITOR
[SerializeField] private Optional<string> allowDebugHost;
[SerializeField] private Optional<ushort> allowDebugPort;
@ -157,6 +164,7 @@ namespace BITKit.Net.Kcp
public void Tick()
{
_netProviderImplementation.Tick();
}
public void HandShake()
@ -219,6 +227,9 @@ namespace BITKit.Net.Kcp
timer.Update(obj);
rate = timer;
Tick();
traffic = client.Traffic;
upTraffic = NetUtils.GetFileSize((long)traffic.x);
downTraffic = NetUtils.GetFileSize((long)traffic.y);
}
[BIT]
private void EditorConnect()

View File

@ -133,22 +133,14 @@ namespace BITKit
};
if (fieldInfo is not null && Attribute.IsDefined(fieldInfo, typeof(ReadOnlyAttribute), true))
{
var attribute = fieldInfo.GetCustomAttribute<ReadOnlyAttribute>();
field.pickingMode = PickingMode.Ignore;
field.SetEnabled(false);
field.style.opacity = 1;
var x = field.Q("unity-text-input");
var clearColor = new Color(0, 0, 0, 0);
if (x is not null)
field.AddToClassList("readonly");
if (attribute.HideLabel)
{
x.style.backgroundColor = clearColor;
x.style.borderTopColor = clearColor;
x.style.borderBottomColor = clearColor;
x.style.borderLeftColor = clearColor;
x.style.borderRightColor = clearColor;
foreach (var visualElement in field.Children())
{
visualElement.pickingMode = PickingMode.Ignore;
}
field.AddToClassList("hide-label");
}
}

View File

@ -13,3 +13,15 @@
border-top-color: rgb(149, 149, 149);
border-bottom-color: rgb(149, 149, 149);
}
.readonly #unity-text-input {
background-color: rgba(0, 0, 0, 0);
border-left-color: rgba(0, 0, 0, 0);
border-right-color: rgba(0, 0, 0, 0);
border-top-color: rgba(0, 0, 0, 0);
border-bottom-color: rgba(0, 0, 0, 0);
}
.readonly .hide-label TextField Label {
display: none;
}

View File

@ -0,0 +1,11 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<Style src="project://database/Assets/BITKit/Unity/UX/BITInspector.uss?fileID=7433441132597879392&amp;guid=3a78fb5b182fa434781baf5c9f733586&amp;type=3#BITInspector" />
<ui:Button text="Button" parse-escape-sequences="true" display-tooltip-when-elided="true" />
<ui:TextField picking-mode="Ignore" label="Text Field" value="filler text" />
<ui:VisualElement class="readonly">
<ui:VisualElement class="hide-label" style="flex-grow: 1;">
<ui:TextField picking-mode="Ignore" label="Text Field" value="filler text" />
</ui:VisualElement>
<ui:TextField picking-mode="Ignore" label="Text Field" value="filler text" />
</ui:VisualElement>
</ui:UXML>

View File

@ -0,0 +1,10 @@
fileFormatVersion: 2
guid: a61f70effbd22d74999cc16a508f5bc3
ScriptedImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 2
userData:
assetBundleName:
assetBundleVariant:
script: {fileID: 13804, guid: 0000000000000000e000000000000000, type: 0}