简体   繁体   English

C# UNITY3D 中的排行榜?

[英]LeaderBoard in C# UNITY3D?

I Needed To Develop LeaderBoard For Storing Details(means Scores) of Players in Games.Just Displaying players Scores on LeaderBoard in UNITY3D.so plz help me i dont have any idea.我需要开发排行榜来存储游戏中玩家的详细信息(意味着分数)。只是在 UNITY3D 的排行榜上显示玩家的分数。所以请帮助我,我不知道。 in below code Social Platforms NameSpace is there but i dont know how start and how to implement LeaderBoard in 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 ()
       {

       }

   }

You need to create an IComparable class and add details like name and score , compare by score.您需要创建一个 IComparable 类并添加名称和分数等详细信息,按分数进行比较。

public class PlayerScore : IComparable<PlayerScore>

    public string name;
    public int score;

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

You also need a list你还需要一份清单

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

You need some way of getting input from user to add name.您需要某种方式从用户那里获取输入以添加名称。

When adding score, create object of iComparer class and set name, score etc.添加分数时,创建 iComparer 类的对象并设置名称、分数等。

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

    scoreIndex.Add(a);

Then add new object to List and sort list, List.Sort();.然后将新对象添加到 List 和排序列表,List.Sort();。 if you want reversed then add reverse().如果你想反转然后添加reverse()。

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

Save list to player prefs eg将列表保存到播放器首选项,例如

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

To display names and scores, create 3dText objects for names/scores and place a script like要显示名称和分数,请为名称/分数创建 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 () 
{
}

} }

Set Pos for each object.Do the same for scores with score script.为每个对象设置 Pos。使用分数脚本对分数执行相同的操作。

At the start of the game add player prefs into list or you will get an error when trying to retrieve names/scores.在游戏开始时将玩家偏好添加到列表中,否则在尝试检索名称/分数时会出现错误。 needs to be same amount as list size.需要与列表大小相同的数量。

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

Don't know if I'm explaining this well, but you basically need to add playerprefs to list, add comparable scores to list, sort the list, save the list, display the saved list.不知道我是否解释得很好,但您基本上需要将玩家偏好添加到列表中,将可比分数添加到列表中,对列表进行排序,保存列表,显示保存的列表。 I'm new to this so take it easy with criticism ;)我是新手,所以不要批评;)

If you mean leaderboard like a local high scores table, you would need two functions: AddScore and a function that gets the high scores.如果您的意思是像本地高分表这样的排行榜,您将需要两个函数:AddScore 和一个获取高分的函数。 (note this example is in C#) (注意这个例子是在 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 = "";
      }
   }
}

And then to get the highscores:然后获得高分:

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

If you want to create a networked/online leaderboard, you need to use something like GameFly (take a look at that example).如果你想创建一个网络/在线排行榜,你需要使用类似GameFly 的东西(看看那个例子)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM