《Unity3D网络游戏实战》学习与实践--制作一款大乱斗游戏

server/2024/9/24 6:31:38/

角色类

基类Base Human是基础的角色类,它处理“操控角色”和“同步角色”的一些共有功能;CtrlHuman类代表“操控角色”​,它在BaseHuman类的基础上处理鼠标操控功能;SyncHuman类是“同步角色”类,它也继承自BaseHuman,并处理网络同步(如果有必要)​。

BaseHuman

        using System.Collections;using System.Collections.Generic;using UnityEngine;public class BaseHuman : MonoBehaviour {//是否正在移动protected bool isMoving = false;//移动目标点private Vector3 targetPosition;//移动速度public float speed = 1.2f;//动画组件private Animator animator;//描述public string desc = "";//移动到某处public void MoveTo(Vector3 pos){targetPosition = pos;isMoving = true;animator.SetBool("isMoving", true);}//移动Updatepublic void MoveUpdate(){if(isMoving == false) {return;}Vector3 pos = transform.position;transform.position = Vector3.MoveTowards(pos, targetPosition, speed*Time.deltaTime);transform.LookAt(targetPosition);if(Vector3.Distance(pos, targetPosition) < 0.05f){isMoving = false;animator.SetBool("isMoving", false);}}// Use this for initializationprotected void Start () {animator = GetComponent<Animator>();}// Update is called once per frameprotected void Update () {MoveUpdate();}}

CtrlHuman

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class CtrlHuman : BaseHuman
{new void Start(){base.Start();}// Update is called once per framenew void Update(){base.Update();if(Input.GetMouseButtonDown(0)) {Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);RaycastHit hit;Physics.Raycast(ray,out hit);if(hit.collider.tag == "Terrain") {MoveTo(hit.point);}}}
}

如何使用网络模块

在实际的网络游戏开发中,网络模块往往是作为一个底层模块用的,它应该和具体的游戏逻辑分开,而不应该把处理逻辑的代码写到 ReceiveCallback 里面去。因为ReceiveCallback应当只处理网络数据,不应该去处理游戏功能

一个可行的做法是,给网络管理类添加回调方法,当收到某种消息时就自动调用某个函数,这样便能够将游戏逻辑和底层模块分开。制作网络管理类前,需要先了解委托、协议和消息队列这三个概念。

通信协议

通信协议是通信双方对数据传送控制的一种约定,通信双方必须共同遵守,方能“知道对方在说什么”和“让对方听懂我的话”​。

使用一种最简单的字符串协议来实现。协议格式如下所示,消息名和消息体用“|”隔开,消息体中各个参数用“, ”隔开。

消息名|参数1, 参数2, 参数3, ...

Move|127.0.0.1:1234, 10, 0, 8,

处理数据:

        string str = "Move|127.0.0.1:1234, 10, 0,8, ";string[] args = str.Split('|');string msgName = args[0]; //协议名:Movestring msgBody = args[1]; //协议体:127.0.0.1:1234, 10, 0,8,string[] bodyArgs = msgBody.Split(', ');string desc = bodyArgs [0];               //玩家描述:127.0.0.1:1234float x = float.Parse(bodyArgs [1]);     //x坐标:10float y = float.Parse(bodyArgs [2]);     //y坐标:0float z = float.Parse(bodyArgs [3]);     //z坐标:8

消息队列

多线程消息处理虽然效率较高,但非主线程不能设置Unity3D组件,而且容易造成各种莫名其妙的混乱。由于单线程消息处理足以满足游戏客户端的需要,因此大部分游戏会使用消息队列让主线程去处理异步Socket接收到的消息。

C#的异步通信由线程池实现,不同的BeginReceive不一定在同一线程中执行。创建一个消息列表,每当收到消息便在列表末端添加数据,这个列表由主线程读取,它可以作为主线程和异步接收线程之间的桥梁。由于MonoBehaviour的Update方法在主线程中执行,可让Update方法每次从消息列表中读取几条信息并处理,处理后便在消息列表中删除它们

NetManager类

网络模块中最核心的地方是一个称为NetManager的静态类,这个类对外提供了三个最主要的接口。

  • Connect方法,调用后发起连接;
  • AddListener方法,消息监听。其他模块可以通过AddListener设置某个消息名对应的处理方法,当网络模块接收到这类消息时,就会回调处理方法;
  • Send方法,发送消息给服务端。

无论内部实现有多么复杂,网络模块对外的接口只有图片展示的这几个:

对内部而言,NetManager使用了异步Socket接收消息,每次接收到一条消息后,NetManager会把消息存入消息队列中​。NetManager有一个供外部调用的Update方法,每当调用它时就会处理消息队列里的第一条消息,然后根据协议名将消息分发给对应的回调函数

    using System.Collections;using System.Collections.Generic;using UnityEngine;using System.Net.Sockets;using UnityEngine.UI;using System;public static class NetManager {//定义套接字static Socket socket;//接收缓冲区static byte[] readBuff = new byte[1024];//委托类型public delegate void MsgListener(String str);//监听列表private static Dictionary<string, MsgListener> listeners =new Dictionary<string, MsgListener>();//消息列表static List<String> msgList = new List<string>();//添加监听public static void AddListener(string msgName, MsgListener listener){listeners[msgName] = listener;}//获取描述public static string GetDesc(){if(socket == null) return "";if(! socket.Connected) return "";return socket.LocalEndPoint.ToString();}//连接public static void Connect(string ip, int port){//Socketsocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream, ProtocolType.Tcp);//Connect(用同步方式简化代码)socket.Connect(ip, port);//BeginReceivesocket.BeginReceive( readBuff, 0, 1024, 0,ReceiveCallback, socket);}//Receive回调private static void ReceiveCallback(IAsyncResult ar){try {Socket socket = (Socket) ar.AsyncState;int count = socket.EndReceive(ar);string recvStr =System.Text.Encoding.Default.GetString(readBuff, 0, count);msgList.Add(recvStr);socket.BeginReceive( readBuff, 0, 1024, 0,ReceiveCallback, socket);}catch (SocketException ex){Debug.Log("Socket Receive fail" + ex.ToString());}}//发送public static void Send(string sendStr){if(socket == null) return;if(! socket.Connected)return;byte[] sendBytes = System.Text.Encoding.Default.GetBytes(sendStr);socket.Send(sendBytes);}//Updatepublic static void Update(){if(msgList.Count <= 0)return;String msgStr = msgList[0];msgList.RemoveAt(0);string[] split = msgStr.Split('|');string msgName = split[0];string msgArgs = split[1];//监听回调;if(listeners.ContainsKey(msgName)){listeners[msgName](msgArgs);}}}

参考书籍:《Unity3D网络游戏实战(第2版)》 (豆瓣) (douban.com)


http://www.ppmy.cn/server/95609.html

相关文章

计时器(Python)

代码 import time from tkinter import ttk import threading from tkinter import scrolledtext import tkinter as tkclass TimerApp:def __init__(self, root):self.root rootself.root.title("计时器")self.screen_w, self.screen_h self.root.winfo_screenwi…

Mybatis面试

Mybatis 面试 1、Mybatis 的执行流程是什么&#xff1f; 1、读取MyBatis配置文件&#xff1a;mybatis-config.xml 加载运行环境 和 映射文件 2、构造会话工厂 SqlSessionFactory &#xff08;全局只有一个&#xff09; 3、会话工厂创建SqlSession对象&#xff08;项目与数据…

【c++】爬虫到底违不违法?

很多小伙伴都想知道爬虫到底违法吗&#xff0c;今天博主就给大家科普一下 爬虫本身并不违法&#xff0c;但使用爬虫采集数据可能涉及违法风险&#xff0c;具体取决于采集行为是否侵犯了他人的合法权益&#xff0c;尤其是隐私权和个人信息权。以下是对爬虫是否违法的详细分析&am…

C# 6.定时器 timer

使用控件&#xff1a; 开启定时器&#xff1a;timer1.Start(); 关闭定时器&#xff1a;timer1.Stop(); 定时间时间间隔:Interval timer1.Interval 1000; Interva等于1000是每一秒刷新一次 定时器默认时间间隔是100ms 代码创建定时器 ①创建 Timer t1 new Timer(); …

项目实战_表白墙(升级版)

你能学到什么 表白墙&#xff08;升级版&#xff09;Mybatis的一些简单应用 正文 前⾯的案例中, 我们写了表⽩墙, 但是⼀旦服务器重启, 数据就会丢失. 要想数据不丢失, 需要把数据存储在数据库中&#xff0c;接下来咱们借助MyBatis来实现数据库的操作。 数据准备 如果我们…

sais复杂推理能力评估笔记(一):baseline简介

赛题流程 初赛数据集为逻辑推理数据&#xff0c;其中训练集中包含500条训练数据&#xff0c;测试集中包含500条测试数据。每个问题包括若干子问题&#xff0c;每个子问题为单项选择题&#xff0c;选项不定&#xff08;最多5个&#xff09;。目标是为每个子问题选择一个正确答案…

【c++】用C++制作一个简易windows系统

源码: #include <iostream> #include <cstdlib> // 为了使用system #include<limits> void clearScreen() {system("cls"); }void displayMenu() {clearScreen();std::cout << "1.我的文件" << std::endl;std::cout <&…

[知识点]-[最短路]

单源最短路径 代码&#xff1a; #include<bits/stdc.h> #define int long longusing namespace std;const int N 1e510;int n,m,A; int dis[N]; int vis[N];vector<pair<int,int> > e[N];void spfa() {for(int i1;i<n;i) dis[i] 1e18;queue<int &g…