其他分享
首页 > 其他分享> > 写个Unity 简单使用Protobuffer进行网络传输的Demo

写个Unity 简单使用Protobuffer进行网络传输的Demo

作者:互联网

博主用的protobuf3.10.0的版本

下载资源文件

先去GitHub去下载两个东西

链接:https://github.com/protocolbuffers/protobuf/releases/tag/v3.10.0

第一个:protobuf的C#包

第二个:protobuf的转化工具

下载得到的两个资源 ->解压

导入资源文件

1、把这个文件导入到客户端和服务端(也可以生成Dll再导入,看个人习惯)

Unity导入后如下图

2、写protobuf文件 (在刚刚下载的Win-bin目录下创建GameData.proto)

protobuf内容如下:

syntax="proto3";
message GameData{
	string PlayerName =1;
	float Power =2;
	int32 Money = 3;
	repeated bool unlock = 4;
	data levelData = 5;
	message data{
		int32 process = 1;
		
	}
}

3、protobuf生成C#代码

创建个bat批处理文件

批处理代码:(本来想处理成,一个文件夹存放proto文件,一个文件夹处理c#脚本,奈何水平有限,搞不懂批处理要怎么写)

@echo off
for %%i in (*.proto) do (
    protoc --csharp_out=./ %%i
)
pause

双击bat,生成C#脚本

把脚本导入客户端和服务端

4、编写服务端代码:

(创建窗体类,点击启动服务器 ->代码实际Ip地址根据自己IP地址走)

using System;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Google.Protobuf;

namespace Server
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Socket ServerSocket;
        private Socket ClientSocket;
        private void button1_Click(object sender, EventArgs e)
        {
            IPAddress ipadss = IPAddress.Parse("192.168.149.1");
            EndPoint endPoint = new IPEndPoint(ipadss, 6666);
            ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            ServerSocket.Bind(endPoint);
            ServerSocket.Listen(10);
            Thread tread = new Thread(Accept);
            tread.Start();
            button1.Visible = false;
        }

        private void Accept()
        {
            ClientSocket = ServerSocket.Accept();
            string success = "你已链接到服务端";
            byte[] bytes = Encoding.UTF8.GetBytes(success);
            GameData data = new GameData();
            data.PlayerName = "Name";
            data.Money = 100;
            data.Power = 10;
            data.Unlock.Add(true);
            data.Unlock.Add(true);
            data.Unlock.Add(true);
            data.Unlock.Add(false);
            data.LevelData = new GameData.Types.data { Process = 1 };
            bytes = data.ToByteArray();
            ClientSocket.Send(bytes);
            Thread tread2 = new Thread(GetData);
            tread2.Start();
        }
        private void GetData()
        {
            byte[] bytes = new byte[1024];
            ClientSocket.Receive(bytes);
            string str = Encoding.UTF8.GetString(bytes);
        }
    }
}

5、客户端代码

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using UnityEngine;

public class PBDataHelper : MonoBehaviour
{
    private Socket ClientSocket;
    void Start()
    {
        IPAddress ipadss = IPAddress.Parse("192.168.149.1");
        EndPoint endPoint = new IPEndPoint(ipadss, 6666);
        ClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
        ClientSocket.Connect(endPoint);
        Thread tread2 = new Thread(GetDatas);
        tread2.Start();
    }
    private void GetDatas()
    {
        byte[] bytes = new byte[1024];
        int _length = ClientSocket.Receive(bytes);
        Debug.Log(_length);
        byte[] newBytes = new byte[_length];
        Buffer.BlockCopy(bytes, 0, newBytes, 0, _length);
        GameData dataInfo = GameData.Parser.ParseFrom(newBytes);
        Debug.Log(dataInfo);
        Debug.Log(dataInfo.PlayerName);
        Debug.Log(dataInfo.Money);
    }
}

6、测试结果

1、先启动服务器:

2、启动Unity获取通信结果

标签:Demo,Protobuffer,bytes,System,Unity,new,using,data,ClientSocket
来源: https://blog.csdn.net/LM514104/article/details/118251775