Unity如何实现分数排行榜功能(Web数据库)

发表于2018-06-04
评论0 3.5k浏览
对于排行榜的实现方法有多种,不过本篇和要分享的是从Web数据库中实现分数排行榜功能,这样玩家就能第一时间了解最近排行数据。

这篇文章中有使用到的工具有:
XAMPP:建立排行榜所需资料库
NetBeans:撰写 PHP 表单部分
Unity:撰写客户端发送请求
教学中所使用的 Username 为 root

是 MySQL 中预设的使用者帐号,也是权限最高的帐号,因为撰写教学所以在这里直接使用 root,建议实际撰写时使用另外建立的使用者帐号,防止被骇客攻击。

先使用 MySQL 建立资料库及资料表
mysql -u root
create database highscore;
use highscore;
create table ranking (
id int(10) auto_increment primary key,
name varchar(15),
score int(10)
);

资料库及资料表建立完成后可至 http://localhost/phpmyadmin 查看资料表是否建立成功

分数上传

接下来开启 NetBeans 撰写分数上传部分
建立一个新专案并命名为 Unity
新增一个 RankingUpdate.php 脚本

回到 Unity 建立 UI.cs 脚本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UI : MonoBehaviour
{
	public string path = "http://localhost/Unity/RandingUpdate.php";
	public string downloadPath = "http://localhost/Unity/RankingDownload.php";
	public string name;
	public string score;
	void OnGUI()
	{
		name = GUILayout.TextField(name, 10);
		score = GUILayout.TextField(score, 10);
		if(GUILayout.Button("Update"))
		{
			StartCoroutine("ScoreUpdate");
		}
	}
	public IEnumerator ScoreUpdate()
	{		
		WWWForm form = new WWWForm();
		Dictionary<string,string> data = new Dictionary<string, string>();
		data.Add("name", name);
		data.Add("score", score);
		foreach(KeyValuePair<string,string> post in data)
		{
			form.AddField( post.Key, post.Value );
		}
		WWW www = new WWW(path, form);
		yield return www;
		Debug.Log(www.text);
	}
}

开始后输入名字及分数按下 Update 按钮,若成功则会在 Console 面板
看到刚刚在 php 里的提示,在 http://localhost/phpmyadmin 可查询是否正确写入资料。


排行榜下载

在 NetBeans 中建立 RankingDownload.php 脚本

回到 Unity 更新 UI.cs 脚本
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class UI : MonoBehaviour
{
	public string path = "http://localhost/Unity/RandingUpdate.php";
	public string downloadPath = "http://localhost/Unity/RankingDownload.php";
	public string name;
	public string score;
	void OnGUI()
	{
		name = GUILayout.TextField(name, 10);
		score = GUILayout.TextField(score, 10);
		if(GUILayout.Button("Update"))
		{
			StartCoroutine("ScoreUpdate");
		}
		if(GUILayout.Button("Download"))
		{	
			StartCoroutine("ScoreDownload");	
		}
	}
	public IEnumerator ScoreUpdate()
	{		
		WWWForm form = new WWWForm();
		Dictionary<string,string> data = new Dictionary<string, string>();
		data.Add("name", name);
		data.Add("score", score);
		foreach(KeyValuePair<string,string> post in data)
		{
			form.AddField( post.Key, post.Value );
		}
		WWW www = new WWW(path, form);
		yield return www;
		Debug.Log(www.text);
	}
	public IEnumerator ScoreDownload()
	{
		WWWForm form = new WWWForm();
		Dictionary<string,string> data = new Dictionary<string, string>();
		data.Add("download", "1");
		foreach(KeyValuePair<string,string> post in data)
		{
			form.AddField( post.Key, post.Value );
		}
		WWW www = new WWW(downloadPath, form);
		yield return www;
		Debug.Log(www.text);
	}
}

接下来在游戏中按下 Download 按钮,就可以接收到由资料库所传回的讯息。

由于这里回传的资料没有另外经过处理,所以格式看起来不是很好。

不过还是可以看到回传的顺序为
TED,1003
TED,1000
这样建立排行榜的概念就算完成了。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引