PHP服务端、Unity客户端 双端基础源码做avalon阿瓦隆桌游面sha(类似狼人游戏)支持WebGL、小程序发布
- License:没有经过作者允许,禁止商用此项目
- 此工程主要讲述功能点:创建房间、加入房间、房间内战
首先去gitee下载工程源码:https://gitee.com/chasing2moro/roommatch
PHP服务端发布(Windows下演示)
Windows
安装PHP
- 去官网 https://windows.php.net/download 下载一个Thread Safe X64/X86版本的zip(比如: https://windows.php.net/downloads/releases/php-8.2.2-Win32-vs16-x64.zip )
- 解压zip
- 设置环境变量为解压后的路径
遇到问题,可以去 https://www.workerman.net/windows 网站,查看详细步骤
启动服务器
双击工程里的 server\GatewayWorker\start_for_win.bat 文件(工程源码地址在文章开头)
Linux家族
安装、启动 请查阅 https://www.workerman.net/doc/workerman/install/install.html
Unity客户端发布
发布Windows客户端
- 打开Addressables Groups窗口(菜单路径:Asset Management/Addressables/Groups)
- 打包Bundle资源(菜单路径:Build/New Build/Default Build Script)
- 构建可执行文件(菜单路径:File/Build Settings/Build)
- 双击构建成功的可执行文件工程名.exe,即可运行
发布WebGL端
详细步骤请阅读 https://blog.csdn.net/lbxnba/article/details/128542984 里的发布WebGL部分
演示
运行5~10个可执行文件 工程名.exe,即可开始演示
- 创建房间
- 加入房间
- 房间内战
源码解析
- 工程讲述
- PHP服务端工程路径
.\server
- Unity客户端工程路径
.\client
- Unity入口场景
SceneLaunch.unity
- 登录协议 login
-
- PHP代码:
.\server\GatewayWorker\Applications\YourApp\DirRoot\Cmd\cmd_login.php
- PHP代码:
<?php
use \GatewayWorker\Lib\Gateway;
class cmd_login extends cmdbase
{
//客户端发送的login协议,会触发此函数
public function onCmd($msg){
//组装玩家id
$user_id = ClientMgr::getUid($msg->user, $msg->psw);
$_SESSION[SessionTag::uid] = $user_id;
//把在线的旧玩家踢下线
if(Gateway::isUidOnline($user_id)){
$client_id_list = Gateway::getClientIdByUid($user_id);
foreach ($client_id_list as $client_id) {
$notify_kick = new notify_kick();
$notify_kick->msg = "login kick out";
$message = Events::wrapMsg(notify_kick::class, $notify_kick);
Gateway::closeClient($client_id, $message);
echo "warning=>玩家在线,需要提下线:$client_id\n";
}
}
$client_id = $this->getCurClientId();
//绑定用户id,方便下次踢人或者恢复现场
Gateway::bindUid($client_id, $user_id);
$msg_body = new login_rsp();
$msg_body->ret = NetErrorCode::None;
$msg_body->reason = $user_id;
return $msg_body;
}
}
-
- Unity代码:
.\client\Assets\Scripts\UI\Window\UILogin\UILogin.Login.cs
- Unity代码:
//向网络层请求login协议
public void onButtonLoginClick(){
var req = new login();
req.user = account;
req.psw = psw;
req.versioncode = CommonDefine.VersionCode;
WebSocketClient.Instance.SendSerialize(req);
}
//订阅网络事件
private void OnEnable()
{
Facade.Instance.RegistNetEvent(nameof(login_rsp), OnHandleLoginRsp);
}
//退阅网络事件
private void OnDisable()
{
Facade.Instance.UnRegistNetEvent(nameof(login_rsp), OnHandleLoginRsp);
}
//网络层返回login协议
private void OnHandleLoginRsp(System.Object data)
{
var rsp = data as login_rsp;
switch (rsp.ret)
{
case NetErrorCode.None:
DataPlayer.Instance.userId = rsp.reason;
UITips.Instance.Show(XLocalization.Get("登录成功"));
break;
default:
Debug.LogError("unhandle error:" + rsp.ret);
break;
}
}
- 看懂上面的
登录协议login
前后端是如何沟通后,就知道下面所有协议是怎么用了。 -
- PHP代码:
.\server\GatewayWorker\Applications\YourApp\DirRoot\Protocol\rsp_mix.php
- PHP代码:
<?php
//登录
class login{
public string $user;
public string $psw;
public int $group_id;
public int $versioncode;
}
class login_rsp extends rspbase{
}
//重连
class reconnect{
public string $user;
public string $psw;
public string $group_id;
}
class reconnect_rsp extends rspbase{
}
//非协议:数据结构
class member_info{
public string $uid;
public string $name;
public int $card_type;
public int $support_type;
}
//非协议:数据结构
class simplemember_info{
public string $uid;
public int $support_type;
}
//非协议:数据结构
class splmember_info{
public string $uid;
public int $card_type;
public int $support_type;
}
//创建房间
class createroom{
}
class createroom_rsp extends rspbase
{
public string $group_id;
public array $member_infos;
}
//进入房间
class enterroom{
public string $group_id;
}
class enterroom_rsp extends rspbase
{
public string $group_id;
public array $member_infos;
}
//发牌
class dealcard{
}
class dealcard_rsp extends rspbase
{
}
//退出房间
class exitroom{
}
class exitroom_rsp extends rspbase
{
}
//选票
class vote{
public int $support_type;
}
class vote_rsp extends rspbase
{
}
//校验投票,决定能不能开车(做任务)
class examtrain{
public int $type;//1一个否决不能开车,2个否决不能开车
}
class examtrain_rsp extends rspbase
{
}
//清除所有投票
class voteclear{
}
class voteclear_rsp extends rspbase
{
}
//聊天
class chat{
public string $msg;
}
class chat_rsp extends rspbase{
}
-
- Unity代码:
.\client\Assets\Scripts\Net\WebSocket\protocol\rsp_mix.cs
- Unity代码:
public class rspbase
{
public int ret;
public string reason;
}
//登录
public class login
{
public string user;
public string psw;
public int versioncode;
}
public class login_rsp : rspbase
{
}
//重连
public class reconnect
{
public string user;
public string psw;
public string group_id;
}
public class reconnect_rsp : rspbase
{
}
public class member_info
{
public string uid;
public string name;
public int card_type;
public int support_type;
}
public class simplemember_info
{
public string uid;
public int support_type;
}
//创建房间
public class createroom
{
}
public class createroom_rsp : rspbase
{
public string group_id;
public member_info[] member_infos;
}
//进入房间
public class enterroom
{
public string group_id;
}
public class enterroom_rsp: rspbase
{
public string group_id;
public member_info[] member_infos;
}
//发牌
public class dealcard
{
}
public class dealcard_rsp: rspbase
{
}
//退出房间
public class exitroom
{
}
public class exitroom_rsp: rspbase
{
}
//选票
public class vote
{
public int support_type;
}
public class vote_rsp: rspbase
{
}
//校验投票,决定能不能开车(做任务)
public class examtrain
{
public int type;//1一个否决不能开车,2个否决不能开车
}
public class examtrain_rsp: rspbase
{
}
//清除所有投票
class voteclear
{
}
class voteclear_rsp: rspbase
{
}
//聊天
class chat
{
public string msg;
}
class chat_rsp: rspbase
{
}