IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> C#服务端与Unity客户端实现房间通信技术 -> 正文阅读

[游戏开发]C#服务端与Unity客户端实现房间通信技术

一.基本简介

(详细可查看本文最底部的我的共享项目源码百度网盘下载链接)

1.C#控制台应用程序通过记事本实现创建房间、加入房间、离开房间、获取房间列表、获取房间信息

(1)创建房间

(2)加入房间

(3)离开房间

(4)获取房间列表

(5)获取房间信息

using System;

namespace GameServer
{
[Serializable]
public class RoomData
{
public int Id;
public bool IsDeleted;
public int RoomId;
public int UserId;
public int MaxCount;
}
}

?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace GameServer
{
public class RoomDo
{
public string Name { get; private set; }
public string Path { get; private set; }

public RoomDo()
{
Name = "RoomData";
Path = "C:///" + Name + ".txt";
}

public string Read()
{
FileStream fs = new FileStream (Path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Close ();
FileInfo file = new FileInfo (Path);
var reader = file.OpenText ();
string listJson = reader.ReadToEnd ();
reader.Close ();
return listJson;
}

public void Save(string listJson)
{
FileStream fs = new FileStream (Path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
fs.Close ();
FileInfo file = new FileInfo (Path);
var writer = file.CreateText ();
writer.WriteLine (listJson);
writer.Close ();
}

public void Insert(string [] names, string [] values)
{
string listJson = Read ();
if (listJson == "") {
listJson += "RoomData";
Save (listJson);
}?
else?
{
listJson += "&Id=" + (GetList ().Count + 1) + ",";
listJson += "IsDeleted=" + false + ",";
listJson += names [0] + "=" + values [0] + ",";
listJson += names [1] + "=" + values [1] + ",";
listJson += names [2] + "=" + values [2];
Save (listJson);
}
}

public void Delete(int id)
{
var list = GetList ();
if (list == null || list.Count <= 0) {
return;
}
if (id == 0) {
list.Clear ();
string listJson = ToListJson (list);
Save (listJson);
return;
}
var room = list.Where (r => r.Id == id).FirstOrDefault ();
if (room != null) {
list.Remove (room);
string listJson = ToListJson (list);
Save (listJson);
}
}

public void Update(int id, string name, string value)
{
var list = GetList ();
if (list == null || list.Count <= 0) {
return;
}
for (int i = 0; i < list.Count; i++) {
var item = list [i];
if (item.Id == id) {
if (name == "IsDeleted") {
item.IsDeleted = Convert.ToBoolean (value);
}
else if (name == "RoomId") {
item.RoomId = int.Parse (value);
}?
else if (name == "UserId") {
item.UserId = int.Parse (value);
}?
else if (name == "MaxCount") {
item.MaxCount = int.Parse (value);
}?
}
}
string listJson = ToListJson (list);
Save (listJson);
}

public string ToListJson(List<RoomData> list)
{
string listJson = "RoomData";
if (list == null || list.Count <= 0) {
return listJson;
}
for (int i = 0; i < list.Count; i++) {
listJson += "&Id=" + list[i].Id + ",";
listJson += "IsDeleted=" + list[i].IsDeleted + ",";
listJson += "RoomId=" + list[i].RoomId + ",";
listJson += "UserId=" + list [i].UserId + ",";
listJson += "MaxCount=" + list[i].MaxCount;
}
return listJson;
}

public List<RoomData> GetList()
{
List<RoomData> list = new List<RoomData> ();
string listJson = Read ();
if (listJson == "") {
return list;
}
if (!listJson.Contains("&")) {
Console.WriteLine (listJson);
}
else?
{
var jsons = listJson.Split ('&');
for (int i = 1; i < jsons.Length; i++) {
var attrs = jsons [i].Split(',');
RoomData room = new RoomData ();
room.Id = int.Parse (attrs [0].Split ('=') [1]);
room.IsDeleted = Convert.ToBoolean (attrs [1].Split ('=') [1]);
room.RoomId = int.Parse (attrs [2].Split ('=') [1]);
room.UserId = int.Parse (attrs [3].Split ('=') [1]);
room.MaxCount = int.Parse (attrs [4].Split ('=') [1]);
list.Add (room);
}
}
return list;
}

public List<RoomData> GetHouseOwnerList()
{
var roomList = GetList ();
roomList.Where ((x, i) => roomList.FindIndex (n => n.RoomId == x.RoomId) == i).ToList ();
return roomList;
}

public void CreateRoom(int roomId, int userId, int maxCount)
{
var result = GetList ().Where (r => r.UserId == userId).FirstOrDefault ();
if (result == null) {
Insert (
new string[]{ "RoomId", "UserId", "MaxCount" },?
new string[]{ roomId.ToString (), userId.ToString (), maxCount.ToString () }
);
}
}

public List<RoomData> GetRoom(int roomId)
{
return GetList ().Where (r => r.RoomId == roomId).ToList ();
}

public RoomData GetHouseOwner(int roomId)
{
return GetList ().Where (r => r.RoomId == roomId && r.UserId == roomId).FirstOrDefault ();
}

public void JoinRoom(int roomId, int userId)
{
var room = GetRoom (roomId);
var houseOwner = GetHouseOwner (roomId);
if (room == null || houseOwner == null) {
Console.WriteLine ("房间不存在");
return;
}
if (room.Count < houseOwner.MaxCount) {
CreateRoom (roomId, userId, houseOwner.MaxCount);
}
}

public void LeaveRoom(int roomId, int userId)
{
var person = GetList ().Where (r => r.RoomId == roomId && r.UserId == userId).FirstOrDefault ();
if (person == null) {
return;
}
if (person.RoomId == person.UserId) {
Console.WriteLine ("房间[" + roomId + "]房主[" + userId + "]离开了房间");
Delete (0);
}?
else {
Console.WriteLine ("房间[" + roomId + "]房客[" + userId + "]离开了房间");
Delete (person.Id);
}
}

public void Ready(int roomId, int userId)
{
var person = GetList ().Where (r => r.RoomId == roomId && r.UserId == userId).FirstOrDefault ();
if (person == null) {
return;
}
Update (person.Id, "IsDeleted", true.ToString ());
}

public void StartGame(int roomId, int userId)
{
var person = GetList ().Where (r => r.RoomId == roomId && r.UserId == userId).FirstOrDefault ();
if (person == null) {
return;
}
if (person.RoomId == person.UserId) {
Console.WriteLine ("执行房主开始游戏操作");
}
}
}
}

?

2.Socket TCP Connect、Send、Close、BeginReceive、EndReceive、string.Split('|');实现网络通信

3.Unity NetConn实现Connect、Send、Close、BeginReceive、EndReceive、string.Split('|');网络通信

4.Unity Dictionary<string,BasePanel>实现Base Panel的添加载入、面板显示和隐藏、退出游戏销毁

5.Unity UGUI CanvasGroup实现Base Panel的OnEnter和OnExit(ig开头的bool属性、in开头的bool属性、raycast相关的bool属性和alpha属性)

二.C#控制台应用程序通过记事本实现创建房间、加入房间、离开房间、获取房间列表、获取房间信息

三.C#服务端(控制台应用程序)

四.Unity客户端(5.6.0版本)

链接:https://pan.baidu.com/s/1Bc-h51td3FatDED2EWYFnA?
提取码:q2uz

  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2021-08-07 12:24:41  更:2021-08-07 12:25:27 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/3 12:45:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码