【问题标题】:LeaderBoard in C# UNITY3D?C# UNITY3D 中的排行榜?
【发布时间】:2013-07-14 03:02:23
【问题描述】:

我需要开发排行榜来存储游戏中玩家的详细信息(意味着分数)。只是在 UNITY3D 中的排行榜上显示玩家分数。所以请帮助我,我不知道。在下面的代码Social Platforms NameSpace 中,但我不知道如何开始以及如何在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 ()
       {

       }

   }

【问题讨论】:

    标签: c# unity3d leaderboard


    【解决方案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 之类的东西(看看那个例子)。

    【讨论】:

      【解决方案2】:

      您需要创建一个 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 () 
      {
      }
      

      }

      为每个对象设置位置。使用分数脚本对分数执行相同操作。

      在游戏开始时将玩家偏好添加到列表中,否则在尝试检索名称/分数时会出错。需要与列表大小相同。

      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 添加到列表中,将可比较的分数添加到列表中,对列表进行排序,保存列表,显示保存的列表。我是新手,所以请不要批评;)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-15
        • 1970-01-01
        • 2021-05-14
        • 1970-01-01
        • 1970-01-01
        • 2020-12-17
        • 2017-08-04
        • 2022-11-26
        相关资源
        最近更新 更多