如何在unity c#中排序和显示一个数组来创建一个排行榜

本文关键字:一个 数组 排行榜 创建 显示 unity 排序 | 更新日期: 2023-09-27 18:15:49

我现在有一点问题,我必须与员工的数据库工作。这些员工有不同的数据,这些是字符串和整型,数组中的示例是:Naam:Pino | Team:2 | VerkoopA:12 | VerkoopB:12 | NPS:40 | convert:10。

这是一个只有一个雇员的例子,数据库中有更多的雇员,我已经在代码中拆分了它们。我想做的是用这些数据制作一个排行榜,但是我不知道如何分离数组中的不同项目,如果我已经分离了,我如何对数组进行排序,以查看谁拥有最多的销售额,谁拥有最多的销售额,所以我可以用过滤器制作一个排行榜。

还有,是否有一种简单的方法可以有一个可视化的排行榜,可以显示一定数量的员工?因为我现在显示它的方式它什么也没显示。

我使用Unity和我自己的服务器与Xampp

致以亲切的问候

Dh

脚本:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Leaderboard : MonoBehaviour
{
    // Array of all the employees
    public string[] leaderboard;
    Text txt;
    IEnumerator Start()
    {

        // Getting the leaderboard data from mySQL.
        WWW leaderboardsData = new WWW("http://localhost/leaderboards.php");
        yield return leaderboardsData;
        // Split every employee in the Array so they are all apart from each other. |
        // an example Message Naam:Pino | Team:2 | VerkoopA:12 | VerkoopB:12 | NPS:40 | Conversie:10 
        string leaderboardsDataString = leaderboardsData.text;
        leaderboard = leaderboardsDataString.Split(';');
        // for every employee in the array print every employee's data.
        foreach (string employee in leaderboard)
        {
            string employeeApart = employee;
            DisplayLeaderboards(employeeApart);
        }
    }
    // Display the leaderboards 1 by 1.
    void DisplayLeaderboards(string employeeApart)
    {
        print(employeeApart);
        txt = gameObject.GetComponent<Text>();
        txt.text = employeeApart;
    }

    string GetDataValue(string data, string index)
    {
        string value = data.Substring(data.IndexOf(index) + index.Length);
        if (value.Contains("|")) value = value.Remove(value.IndexOf("|"));
        return value;
    }
}
PHP:

<?php
$servername = "localhost";
$username =  "root";
$password = "";
$dbName = "test3";
//Make Connection
$conn = new mysqli($servername, $username, $password, $dbName);
//Check Connection
if(!$conn){
    die("Connection Failed. ". mysqli_connect_error());
}
$sql = "SELECT ID, Team, VerkoopA, VerkoopB, NPS, Conversie FROM Leaderboards";
$result = mysqli_query($conn ,$sql);

if(mysqli_num_rows($result) > 0){
    //show data for each row
    while($row = mysqli_fetch_assoc($result)){
        echo "ID:".$row['ID'] . "|Team:".$row['Team']. "|VerkoopA:".$row['VerkoopA']. "|VerkoopB:".$row['VerkoopB'] . "|NPS:".$row['NPS']. "|Conversie:".$row['Conversie'] . ";";
    }
}
?>
编辑:

经过程序员的帮助,我决定使用json

我现在有一点问题,我必须与员工一起使用数据库。这些员工有不同的数据,我的PHP文件的输出是:

[{"ID":"1","团队":"2","VerkoopA":"35","VerkoopB":"12","NPS":"25"、"Conversie":"18"},{" ID ":"2","团队":"1","VerkoopA":"55","VerkoopB":"2","NPS":"12","Conversie":"40"},{" ID ":"3","团队":"2","VerkoopA":"12","VerkoopB":"12","NPS":"40"、"Conversie":"10"}]

我想做的事情是做一个排行榜的数据,但我不知道如何在数组中使用JsonHelper分离不同的项目,我得到一个错误:ArgumentException: JSON必须代表一个对象

脚本:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Leaderboard: MonoBehaviour
{
    // Array of all the employees
    [System.Serializable]
    public class LeaderBoard
    {
        public string ID;
        public string Team;
        public string VerkoopA;
        public string VerkoopB;
        public string NPS;
        public string Conversie;
    }

    Text txt;
    IEnumerator Start()
    {
        // Getting the leaderboard data from mySQL.
        WWW leaderboardsData = new WWW("http://localhost/leaderboards.php");
        yield return leaderboardsData;
        string leaderboardsDataString = leaderboardsData.text;
        print(leaderboardsDataString);
        LeaderBoard[] leaderboard;
        leaderboard = JsonHelper.FromJson<LeaderBoard>(leaderboardsDataString);
        // for every employee in the array print every employee's data.
        foreach (LeaderBoard employee in leaderboard)
        {
            DisplayLeaderboards(employee);
        }
    }

    // Display the leaderboards 1 by 1.
    void DisplayLeaderboards(LeaderBoard employeeApart)
    {
        Debug.Log("ID: " + employeeApart.ID);
        Debug.Log("Team: " + employeeApart.Team);
        Debug.Log("VerkoopA: " + employeeApart.VerkoopA);
        Debug.Log("VerkoopB: " + employeeApart.VerkoopB);
        Debug.Log("NPS: " + employeeApart.NPS);
        Debug.Log("Conversie: " + employeeApart.Conversie);
    }
}
PHP:

<?php
$servername = "localhost";
$username =  "root";
$password = "";
$dbName = "test3";
//Make Connection
$conn = new mysqli($servername, $username, $password, $dbName);
//Check Connection
if(!$conn){
    die("Connection Failed. ". mysqli_connect_error());
}

$sql = "select * from Leaderboards";
$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}
echo json_encode($emparray);    
?>

如何在unity c#中排序和显示一个数组来创建一个排行榜

由于您可以访问服务器和服务器脚本(php),因此您应该使用jsonxml来执行此操作,而不是使用|分离数据。

以json格式从数据库发送数据。在Unity端,使用WWW类接收数据,然后将json转换回JsonUtility类。

Unity的JsonUtility不支持数组,所以你需要一个包装器来对付它。你可以得到JsonHelper类,它允许json数组在这里

这就是我上面描述的内容:

[System.Serializable]
public class LeaderBoard
{
    public string ID;
    public string Team;
    public string VerkoopA;
    public string VerkoopB;
    public string NPS;
    public string Conversie;
}
Text txt;
IEnumerator _Start()
{

    // Getting the leaderboard data from mySQL.
    WWW leaderboardsData = new WWW("http://localhost/leaderboards.php");
    yield return leaderboardsData;

    string leaderboardsDataString = leaderboardsData.text;
    LeaderBoard[] leaderboard = JsonHelper.FromJson<LeaderBoard>(leaderboardsDataString);
    // for every employee in the array print every employee's data.
    foreach (LeaderBoard employee in leaderboard)
    {
        DisplayLeaderboards(employee);
    }
}
// Display the leaderboards 1 by 1.
void DisplayLeaderboards(LeaderBoard employeeApart)
{
    Debug.Log("ID: " + employeeApart.ID);
    Debug.Log("Team: " + employeeApart.Team);
    Debug.Log("VerkoopA: " + employeeApart.VerkoopA);
    Debug.Log("VerkoopB: " + employeeApart.VerkoopB);
    Debug.Log("NPS: " + employeeApart.NPS);
    Debug.Log("Conversie: " + employeeApart.Conversie);
}

在php端,你必须使用json_encode函数在发送到Unity之前将数据库信息编码为json数组。我不是php爱好者,但你可以在这里和这里了解更多。

排序结果:

使用

从服务器接收数据后,可以按ID排序
leaderboard = leaderboard.OrderBy(c => c.ID).ToArray();

or by NPS

leaderboard = leaderboard.OrderBy(c => c.NPS).ToArray();

这必须在foreach循环之前完成。

编辑:

php生成的json不能直接在Unity中准备好。不擅长php,不能在php端修复json数据。虽然,我可以在Unity端修复它。

在接收到的字符串前面添加{"Items":,然后在它的末尾添加}

我做了一个简单的函数:
string fixJson(string value)
{
    value = "{'"Items'":" + value + "}";
    return value;
}

将该函数包含到脚本中,然后简单地替换

string leaderboardsDataString = leaderboardsData.text;

string leaderboardsDataString = fixJson(leaderboardsData.text);