簡體   English   中英

C# UNITY3D 中的排行榜?

[英]LeaderBoard in C# UNITY3D?

我需要開發排行榜來存儲游戲中玩家的詳細信息(意味着分數)。只是在 UNITY3D 的排行榜上顯示玩家的分數。所以請幫助我,我不知道。 在下面的代碼中,社交平台命名空間在那里,但我不知道如何開始以及如何在 unity3d 中實現 LeaderBoard。

  using UnityEngine;
  using System.Collections.Generic;
  using UnityEngine.SocialPlatforms;


  public class LBoard : MonoBehaviour 
   {
        ILeaderboard leaderBoard;  
       // Use this for initialization
      void Start () 
        {
            leaderBoard = Social.CreateLeaderboard();
         }

      // Update is called once per frame
      void Update ()
       {

       }

   }

您需要創建一個 IComparable 類並添加名稱和分數等詳細信息,按分數進行比較。

public class PlayerScore : IComparable<PlayerScore>

    public string name;
    public int score;

    public int CompareTo(PlayerScore other)
    {
    return this.score.CompareTo(other.score);
    }

你還需要一份清單

public static List<PlayerScore> scoreIndex = new List<PlayerScore>(5);

您需要某種方式從用戶那里獲取輸入以添加名稱。

添加分數時,創建 iComparer 類的對象並設置名稱、分數等。

PlayerScore a = new PlayerScore();//create instance
    a.name = PlayerStats.playerName;//set name
    a.score = PlayerStats.score;//and score

    scoreIndex.Add(a);

然后將新對象添加到 List 和排序列表,List.Sort();。 如果你想反轉然后添加reverse()。

    scoreIndex.Sort ();                     
    scoreIndex.Reverse ();

將列表保存到播放器首選項,例如

PlayerPrefs.SetString("name0" , scoreIndex[0].name);
PlayerPrefs.SetInt("score0" , scoreIndex[0].score);
PlayerPrefs.SetString("name1" , scoreIndex[1].name);
PlayerPrefs.SetInt("score1" , scoreIndex[1].score);

要顯示名稱和分數,請為名稱/分數創建 3dText 對象並放置一個腳本,例如

public class PlayerNameHS : MonoBehaviour 

public int pos;


void Start () 
{
    renderer.material.color = Color.black;

    TextMesh t = (TextMesh)gameObject.GetComponent(typeof(TextMesh));
        t.text =  PlayerPrefs.GetString("name" + pos);

}

void Update () 
{
}

}

為每個對象設置 Pos。使用分數腳本對分數執行相同的操作。

在游戲開始時將玩家偏好添加到列表中,否則在嘗試檢索名稱/分數時會出現錯誤。 需要與列表大小相同的數量。

PlayerScore a = new PlayerScore();
    a.name = PlayerPrefs.GetString("name0");
    a.score = PlayerPrefs.GetInt("score0");
    yourScript.scoreIndex.Add(a);

    PlayerScore b = new PlayerScore();
    b.name = PlayerPrefs.GetString("name1");
    b.score = PlayerPrefs.GetInt("score1");
    yourScript.scoreIndex.Add(b);

不知道我是否解釋得很好,但您基本上需要將玩家偏好添加到列表中,將可比分數添加到列表中,對列表進行排序,保存列表,顯示保存的列表。 我是新手,所以不要批評;)

如果您的意思是像本地高分表這樣的排行榜,您將需要兩個函數:AddScore 和一個獲取高分的函數。 (注意這個例子是在 C# 中)

function AddScore(string name, int score){
   int newScore;
   string newName;
   int oldScore;
   string oldName;
   newScore = score;
   newName = name;
   for(int i=0;i<10;i++){
      if(PlayerPrefs.HasKey(i+"HScore")){
         if(PlayerPrefs.GetInt(i+"HScore")<newScore){ 
            // new score is higher than the stored score
            oldScore = PlayerPrefs.GetInt(i+"HScore");
            oldName = PlayerPrefs.GetString(i+"HScoreName");
            PlayerPrefs.SetInt(i+"HScore",newScore);
            PlayerPrefs.SetString(i+"HScoreName",newName);
            newScore = oldScore;
            newName = oldName;
         }
      }else{
         PlayerPrefs.SetInt(i+"HScore",newScore);
         PlayerPrefs.SetString(i+"HScoreName",newName);
         newScore = 0;
         newName = "";
      }
   }
}

然后獲得高分:

void GetHighScores()
{
    for(int i = 0; i < 10; i++)
    {
        Debug.Log(PlayerPrefs.GetString(i + "HScoreName") + " has a score of: " +  PlayerPrefs.GetInt(i + "HScore"));
    }
}

如果你想創建一個網絡/在線排行榜,你需要使用類似GameFly 的東西(看看那個例子)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM