Unity中向服务器发送用户名和得分,并存入数据库,再讲数据库中的得分按照降序的方式下载到Unity中。
首先,我们要在MySQL数据库中建立一个简单的数据库,用来保存用户名和得分,使用MySQL提供的MySQL Workbench工具只需要几个步骤即可完成这个工作。
这里需要创建两个php脚本,一个用来上传用户名和分数,另一个用来下载分数并将其排序发送给Unity。
<?php
//UploadScore.php
require_once ("PHPStream.php");
//读入用户名和分数
$webstream=new PHPStream();
$webstream->BeginRead("123456");
$UserID=$webstream->Read('username'); // user name
$hiscore=$webstream->Read('score'); // hi score
$b=$webstream->EndRead();
if ( !$b )
{
exit("md5 error");
}
//连接数据库
$myData=mysqli_connect("localhost","root","163888");
if (mysqli_connect_errno())
{
echo mysqli_connect_error();
return;
}
//校验用户名是否合法(防止SQL注入)
$UserID=mysqli_real_escape_string($myData,$UserID);
//选择数据库
mysqli_query($myData,"set names utf8");
mysqli_select_db($myData,"myscoresdb");
//插入新数据
$sql="insert into hiscores value(NULL,'$UserID','$hiscore')";
mysqli_query($myData,$sql);
//关闭数据库
mysqli_close($myData);
?>
<?php
require_once ("PHPStream.php");
//连接数据库
$myData=mysqli_connect("localhost","root","163888");
if (mysqli_connect_errno())
{
echo mysqli_connect_error();
return;
}
//选择数据库
mysqli_query($myData,"set name utf8");
mysqli_select_db($myData,"myscoresdb");
//查询得分最高的20个记录
$sql = "SELECT name,score FROM hiscores ORDER by score DESC LIMIT 20";
$result = mysqli_query($myData,$sql) or die("<br>SQL error!<br/>");
$num_results = mysqli_num_rows($result);
//准备发送数据到Unity
$webstream=new PHPStream();
$webstream->BeginWrite(PKEY);
//发送排行榜分数的数量
$webstream->WriteInt($num_results);
for ($i=0;$i<$num_results;$i++)
{
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$data[$i][0]=$row['name'];
$data[$i][1]=$row['score'];
//发送用户名和得分
$webstream->WriteString($data[$i][0]);
$webstream->WriteString($data[$i][1]);
//echo $data[$i][0];
//echo $data[$i][1];
}
$webstream->EndWrite();
mysqli_free_result($result);
//关闭数据库
mysqli_close($myData);
//发送
echo $webstream->bytes;
?>
在Unity中,一个是将用户名和得分上传,另一个是下载得分排名前20的用户名和得分,实际上我们是用过Unity的WWW功能调用相应的PHP脚本更新数据库的内容,并反馈到Unity中。如下图:
public const string UploadScoreUrl = "http://192.168.1.5:8088/UploadScore.php";
public const string DownloadScoresUrl = "http://192.168.1.5:8088/DownloadScores.php";
private string[] m_hiscores;
IEnumerator UploadScore(string name,string score)
{
PostStream poststream = new PostStream();
poststream.BeginWrite(true);
poststream.Write("username",name);
poststream.Write("score",score);
poststream.EndWrite();
WWW www = new WWW(UploadScoreUrl,poststream.BYTES,poststream.Headers);
yield return www;
if (www.error != null)
{
Debug.LogError("www.error:" + www.error);
}
else
{
Debug.LogError("www.text:"+ www.text);
}
}
IEnumerator DownloadScores()
{
WWW www = new WWW(DownloadScoresUrl);
yield return www;
if (www.error != null)
{
Debug.LogError("www.error:" + www.error);
}
else
{
int count = 0;
PostStream poststream = new PostStream();
poststream.BeginRead(www,true);
poststream.ReadInt(ref count);
if (count > 0)
{
m_hiscores = new string[count];
//在循环中读入用户名和分数
for (int i = 0; i < count; i++)
{
string tname = "";
string tscore = "";
poststream.ReadString(ref tname);
poststream.ReadString(ref tscore);
m_hiscores[i] = tname + ":" + tscore;
Debug.LogError(m_hiscores[i]);
}
bool ok = poststream.EndRead();
if (!ok) Debug.LogError("MD5 error");
}
}
}
string userName = "";
string score = "";
Vector2 vector2;
private void OnGUI()
{
if (GUI.Button(new Rect(10, 110, 150, 30), "上传"))
{
StartCoroutine(UploadScore(userName,score));
score = "";
userName = "";
}
if (GUI.Button(new Rect(10, 140, 150, 30), "下载"))
{
StartCoroutine(DownloadScores());
}
//GUI.color = Color.blue;
GUI.Label(new Rect(10, 10, 100, 50), "用户名:");
GUI.Label(new Rect(10, 50, 100, 50), "密 码:");
userName = GUI.TextField(new Rect(80, 10, 100, 30), userName);
score = GUI.PasswordField(new Rect(80, 50, 100, 30), score,'*');
//开始滚动视图
vector2 = GUI.BeginScrollView(new Rect(180, 0, 500, 500), vector2, new Rect(0, 0, 500, 500), true, true);
if (m_hiscores != null)
{
for (int i = 0; i < 20; i++)
{
if (i < m_hiscores.Length) GUI.Button(new Rect(0, 25 * i, 500, 20), m_hiscores[i]);
else GUI.Button(new Rect(0, 25 * i, 500, 20),"");
}
}
GUI.EndScrollView();
}