用unity和php实现一个排行榜功能(PHP服务端篇)

注:此文章需配合以下文章一起使用
用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客服端代码编写

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值