注:此文章需配合以下文章一起使用
用unity和php实现一个排行榜功能(unity客户端篇)
PHP服务端篇
开发环境安装
首先下载phpstudy,window64位版本
https://www.xp.cn/
安装,启动,打开localhost,看看是否正常
框架选用
选用thinkphp5这个框架,下载地址
https://www.thinkphp.cn/down/1278.html
解压,放到phpstudy的www目录下
打开localhost/public,即可看到thinkphp的欢迎页面
数据库准备
用navicat,新建数据数据库,我命名为untiy,unity库里新建数据表,命名自己定
表的字段
thinkphp5配置mysql
根据这篇文章隐藏index.php,这样访问url的时候可以省略index.php
https://blog.csdn.net/nookl/article/details/80039996
生成模型
application目录下,右键打开命令行,键入以下命令,生成模型
php think make:model dig_score
thinkphp5里排行榜数据读取
$list = DigScore::all(function ($query) {
$query->limit(20)->order('score', 'desc');
})->toArray();
return json(['code' => 0, 'data' => $list]);
这里取了分数最高的前20人,其他数字请自行修改limit
navicat里双击数据表,手动写入几条数据
此时访问localhost/public,就能看到json格式的排行数据了,这个json数据就是unity那边的排行榜展示来源了
在index方法下面,准备以下提交排行分数的代码
public function addscore(){
if(empty($_POST)){
return json(['code'=>1,'data'=>'缺少参数']);
}
$score = new DigScore();
$score->score =input('?post.score');
$score->name = input('?post.name');
$score->create_time = time();
$score->save();
return json(['code'=>0]);
}
打开tp5,application下的route.php文件
粘贴以下代码(访问url时更简短)
Route::post('addscore','index/index/addscore');
至此,服务端已经准备完毕,可以开始unity客服端代码编写