注:此文章需配合以下文章一起使用
用unity和php实现一个排行榜功能(PHP服务端篇)
unity客户端篇
目前我用的版本是unity2020,unity2019,这份代码应该也适用
排行榜显示部分
首先在unity里如图建一个排行榜panel
大致效果
,编辑器层级如下
rankItem是排行榜数据的预制体,
单条显示效果是这样
新建RankItem预制体的脚本,挂载RankItem预制体上,代码如下
RankItem.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//排行预制体类
public class RankItem : MonoBehaviour
{
public Text indexText;
public Text nameText;
public Text scoreText;
public void setItem(Score item)
{
indexText.text = item.rankIndex.ToString();
nameText.text = item.name;
scoreText.text = item.score;
}
}
依次把预制体里对应的text拖进去
新建ScoreManager.cs并挂载到rankPanel上,代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;
//得分管理类
public class ScoreManager : MonoBehaviour
{
string baseUrl = "http://localhost/public";
private string geturl = "";
private string url = "http://localhost/public/add";//请求url
public Text scoreText;
public static ScoreManager instance;
public GameObject rankContent; //scrollview的内容
public GameObject rankPanel;
public RankItem[] rankItems; //直接在编辑器里拖进去
public Button submitButton; //提交panel的提交按钮
public InputField nameInputField; //用户输入名字的input框
void Awake()
{
//Check if instance already exists
if (instance == null)
//if not, set instance to this
instance = this;
//If instance already exists and it's not this:
else if (instance != this)
//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject);
//Sets this to not be destroyed when reloading scene
}
void Start()
{
geturl = baseUrl;
}
}
ScoreManager里,start之后,添加获取排行榜数据的代码
//获取分数
public void getScore()
{
rankPanel.SetActive(true);
StartCoroutine(getScoreRequest());
}
//发送获取分数请求
IEnumerator getScoreRequest()
{
WWWForm form = new WWWForm();
string tmpurl = baseUrl + "/";
UnityWebRequest www = UnityWebRequest.Get(tmpurl);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
//请求失败得到的内容
Debug.Log(www.downloadHandler.text);
}
else
{
string json = www.downloadHandler.text;
//把服务端篇返回的json,解析成为unity可用的List
RequestResult res = JsonUtility.FromJson<RequestResult>(json);
int index = 0;
foreach (var item in rankItems)
{
item.gameObject.SetActive(false);
}
int count = res.data.Count;
for(int i = 0; i < count; i++)
{
Score a= res.data[i];
a.rankIndex = (i + 1);
rankItems[i].gameObject.SetActive(true);
rankItems[i].setItem(a);
index++;
}
}
}
这里用到了个自定义的解析返回结果的类RequestResult.cs,自己新建,内容如下
[Serializable]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class RequestResult
{
public int code;
public List<Score> data;
public string ToJson()
{
return JsonUtility.ToJson(this);
}
}
每条得分记录的类Score.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[Serializable]
public class Score
{
public int id;
public int rankIndex;
public string name;
public string score;
public override string ToString()
{
return JsonUtility.ToJson(this);
}
}
任意新建一个按钮,用于弹出rankPanel,事件挂上scoreManager的getscore。
在编辑器隐藏rankPanel
点击播放按钮,运行代码,此时,排行榜的数据已经可以正常读取到unity里了
排行榜数据提交部分
一般情况下,会在玩家游戏失败时,弹出类似这种panel
ScoreManager.cs里新增代码
//提交分数
public void submitScore()
{
StartCoroutine(SubmitScore());
}
//提交分数请求
IEnumerator SubmitScore()
{
WWWForm form = new WWWForm();
form.AddField("score", scoreText.text.ToString());
form.AddField("name", nameInputField.text);
string tmpurl = baseUrl+ "/addscore";
UnityWebRequest www = UnityWebRequest.Post(tmpurl, form);
yield return www.SendWebRequest();
if (www.result != UnityWebRequest.Result.Success)
{
Debug.Log(www.downloadHandler.text);
}
else
{
Debug.Log("Form upload complete!");
}
submitButton.interactable = false;
rankPanel.SetActive(true);
getScore();
}
把提交按钮的事件选择为submitScore
编辑器运行游戏调试。
至此,排行榜功能已经完成,你可以根据需要自己调整、添加内容