2013-07-16 24 views
1

我需要開發LeaderBoard用於存儲玩家的詳細信息(平均分數)在Games.Just顯示玩家在UNITY3D.so PLUS的LeaderBoard得分幫助我我沒有任何想法。在下面的代碼中社交平臺名稱空間在那裏,但我不知道如何開始以及如何在unity3d中實現LeaderBoard。C#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() 
     { 

     } 

    } 

回答

-1

如果你的意思是頁首橫幅像本地高分表,則需要兩個功能: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(取看看這個例子)。

0

您需要創建一個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.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() 
{ 
} 

腳本}

設置波什每個object.Do相同的分數與分數腳本。

在遊戲開始時將玩家首選項添加到列表中,或者在嘗試檢索名稱/分數時出現錯誤。需要與列表大小相同。

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); 

不知道如果我解釋這很好,但你基本上需要添加playerprefs列出,添加可比分數列表,列表排序,保存列表,顯示保存的列表。我對此很陌生,因此很容易批評;)