首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# UNITY3D中的LeaderBoard?

C# UNITY3D中的LeaderBoard?
EN

Stack Overflow用户
提问于 2013-07-16 12:21:35
回答 2查看 9.2K关注 0票数 0

我需要开发LeaderBoard来存储Games.Just中球员的详细信息(意味着分数),在UNITY3D.so中显示球员在LeaderBoard上的分数,请帮助我我没有任何想法。在下面的代码中,社交平台NameSpace是存在的,但我不知道如何开始,以及如何在unity3d中实现LeaderBoard。

代码语言:javascript
复制
  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 ()
       {

       }

   }
EN

回答 2

Stack Overflow用户

发布于 2013-10-15 21:47:24

您需要创建一个IComparable类并添加详细信息,如名称和分数,按分数比较。

代码语言:javascript
复制
public class PlayerScore : IComparable<PlayerScore>

    public string name;
    public int score;

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

您还需要一个列表

代码语言:javascript
复制
public static List<PlayerScore> scoreIndex = new List<PlayerScore>(5);

你需要一些方法从用户那里获得输入来添加名字。

添加分数时,创建iComparer类的对象,并设置名称、分数等。

代码语言:javascript
复制
PlayerScore a = new PlayerScore();//create instance
    a.name = PlayerStats.playerName;//set name
    a.score = PlayerStats.score;//and score

    scoreIndex.Add(a);

然后将新对象添加到列表和排序列表中,List.Sort();。如果您想反转,则添加reverse()。

代码语言:javascript
复制
    scoreIndex.Sort ();                     
    scoreIndex.Reverse ();

将列表保存到播放器首选项,例如

代码语言:javascript
复制
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对象,并放置一个脚本,如下所示

代码语言:javascript
复制
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的位置设置为与分数脚本相同的分数。

在游戏开始时,将玩家首选项添加到列表中,否则在尝试检索姓名/分数时会出现错误。需要与列表大小相同的数量。

代码语言:javascript
复制
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);

不知道我是否很好地解释了这一点,但您基本上需要将playerpref添加到列表中,将可比较的分数添加到列表中,对列表进行排序,保存列表,显示保存的列表。我对此还是个新手,所以请轻松接受批评;)

票数 0
EN

Stack Overflow用户

发布于 2013-07-16 13:03:10

如果你指的是像本地高分表那样的排行榜,你需要两个函数: AddScore和一个获得高分的函数。(注意这个例子是用C#编写的)

代码语言:javascript
复制
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 = "";
      }
   }
}

然后获得高分:

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

如果你想创建一个网络/在线排行榜,你需要使用像GameFly这样的东西(看看这个例子)。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17667948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档