更改文件架构
This commit is contained in:
71
Packages/Common~/Scripts/Net/NetMessageProxy.cs
Normal file
71
Packages/Common~/Scripts/Net/NetMessageProxy.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.IO;
|
||||
|
||||
namespace BITKit
|
||||
{
|
||||
public class MessageReader : NetMessageReader<NetMessageProxy.MyMessage>
|
||||
{
|
||||
public override NetMessageProxy.MyMessage ReadBinary(BinaryReader reader)
|
||||
{
|
||||
return new()
|
||||
{
|
||||
_string = reader.ReadString(),
|
||||
_int = reader.ReadInt16(),
|
||||
_float = reader.ReadSingle(),
|
||||
};
|
||||
}
|
||||
|
||||
public override void WriteBinary(BinaryWriter writer, NetMessageProxy.MyMessage value)
|
||||
{
|
||||
writer.Write(value._string);
|
||||
writer.Write(value._int);
|
||||
writer.Write(value._float);
|
||||
}
|
||||
}
|
||||
public class NetMessageProxy : MonoBehaviour
|
||||
{
|
||||
[System.Serializable]
|
||||
public record MyMessage
|
||||
{
|
||||
public string _string;
|
||||
public int _int;
|
||||
public float _float;
|
||||
}
|
||||
[SerializeReference, SubclassSelector] public INetMessager provider;
|
||||
public MyMessage testMessage;
|
||||
void Awake()
|
||||
{
|
||||
provider.Init();
|
||||
DI.Register<INetMessager>(provider);
|
||||
provider.Register<MyMessage>(OnReceiveMessage);
|
||||
}
|
||||
[BIT]
|
||||
public void SendTestMessage()
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
provider.Send(testMessage);
|
||||
}
|
||||
[BIT]
|
||||
public void SelfDiagnostics()
|
||||
{
|
||||
BITAppForUnity.ThrowIfNotPlaying();
|
||||
BIT4Log.Log<NetMessageProxy>(provider.GetDiagnostics());
|
||||
}
|
||||
void OnReceiveMessage(MyMessage message)
|
||||
{
|
||||
BIT4Log.Log<NetMessageProxy>($"已成功收到NetMessage:\n{message}");
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
[UnityEditor.CustomEditor(typeof(NetMessageProxy))]
|
||||
public class NetMessageProxyInspector : BITInspector<NetMessageProxy>
|
||||
{
|
||||
|
||||
}
|
||||
#endif
|
||||
}
|
Reference in New Issue
Block a user