Unity - 显示

时间:2018-03-24 23:54:58

标签: c# unity3d

我是编码的初学者。我的一个朋友过去常常帮助我,但现在他很忙,所以我必须独自做事。游戏具有记住玩家在玩游戏之前输入的名称以及玩家得分的功能。现在,我的问题是,当我玩游戏时,它并没有出现在我的排行榜场景中。然后当我用另一个名字玩另一轮时,我可以看到之前的分数,但不是我现在的分数。就像数据延迟出现一样。

void ReplaceRank(string player, int currentScore) {
    string oldName;
    int oldScoreNumber;

    for (int i = 0; i < 10; i++) {
        if (currentScore > scores[i]) {
            highscoreShow.enabled = true;
            oldScoreNumber = scores[i];
            oldName = PlayerPrefs.GetString(oldPlayerName[i]);
            PlayerPrefs.SetInt(oldScore[i], currentScore);
            PlayerPrefs.SetString(oldPlayerName[i], player);
            Debug.Log(currentScore);
            if (i <= 9) {
                ReplaceRank(oldName, oldScoreNumber);
            }
            break;
        }
    }
}

private void GetAllKeys() {
    oldScore = new List<string>();
    oldPlayerName = new List<string>();
    scores = new List<int>();

    for (int i = 11; i < 21; i++) {
        if (PlayerPrefs.GetString("score" + i + "Name", "") == "") {
            PlayerPrefs.SetString("score" + i + "Name", "");
        }
        oldPlayerName.Add("score" + i + "Name");
        oldScore.Add("score" + i);
        scores.Add(PlayerPrefs.GetInt("score" + i, 0));
    }
}

1 个答案:

答案 0 :(得分:0)

即使这看起来微不足道,但保存有点棘手,你可以做的一些事情可以让它变得更容易,例如你应该将分数保存为列表

保存

  1. 将分数添加到列表
  2. 将分数列表转换为Json
  3. 在播放器首选项中保存json字符串
  4. 装载

    1. 从播放器首选项中获取json字符串
    2. 将Json转换为列表
    3. 脚本中的更新列表
    4. 快速编写了这个类..它的测试和工作但你可能需要修改它以满足你的需求并改进它

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using UnityEngine;
      
      
      
      namespace ScoreBoard
      {
          [Serializable]
          public class PlayerScore
      
          {
              public string Name;
              public int Score;
      
              public PlayerScore(string name, int score)
              {
                  Name = name;
                  Score = score;
              }
          }
      
          [Serializable]
          // we need to store list of scores in a container class so we can change it to json 
          public  class PlayerScoresRankedListContainer
          {
              public  List<PlayerScore> PlayerScoresRanked = new List<PlayerScore>();
          }
      
      
          [Serializable]
          public class ScoresRanked  : MonoBehaviour
          {
              public static PlayerScoresRankedListContainer PlayerScoresListContainer =new PlayerScoresRankedListContainer();
      
              public void Awake()
              {
                  //example of usage 
      
                  //get saved items
                  if (PlayerPrefs.GetString("PlayerScoresRanked").Length > 0)
                  {
                      PlayerScoresListContainer.PlayerScoresRanked = GetSortedListFromPlayerPrefs();
                      DebugShowScores();
                  }
                  else
                  {
                      //test the class asving items 
      
                      AddScoreToRankedList("player1", 1000);
                      AddScoreToRankedList("player2", 20);
                      AddScoreToRankedList("player3", 100);
                      SaveListAsJSONInPlayerPrefs();
                  }
      
              }
      
              private void AddScoreToRankedList(string player, int currentScore)
              {
      
                  var score = new PlayerScore(player, currentScore);
      
                  if (DoesScoreAlreadyExist(score))
                  {
                      //we remove a score if it already exists so we can updated it 
                      //you might not need this maybe you just want to keep adding scores
                      PlayerScoresListContainer.PlayerScoresRanked.RemoveAll(x => x.Name == score.Name);
                      PlayerScoresListContainer.PlayerScoresRanked.Add(score);
      
                  }
                  else
                  {
                      PlayerScoresListContainer.PlayerScoresRanked.Add(score);
                  }
      
      
              }
      
              public void SaveListAsJSONInPlayerPrefs()
              {
      
      
                 var jsonlist = JsonUtility.ToJson(PlayerScoresListContainer);
      
                  Debug.Log("LOG ITEMS BEING SAVED: "+jsonlist);
      
                  PlayerPrefs.SetString("PlayerScoresRanked", jsonlist);
              }
      
              public List<PlayerScore> GetSortedListFromPlayerPrefs()
              {
                  var jsonlist = PlayerPrefs.GetString("PlayerScoresRanked");
      
                  var ListContainer = JsonUtility.FromJson<PlayerScoresRankedListContainer>(jsonlist);
      
                  var listsorted = ListContainer.PlayerScoresRanked.OrderByDescending(x => x.Score).ToList();
      
                  return listsorted;
              }
      
              public bool DoesScoreAlreadyExist(PlayerScore scoreToChcek)
              {
                  if (PlayerScoresListContainer.PlayerScoresRanked.Exists(x => x.Name == scoreToChcek.Name))
                  {
                      return true;
                  }
                  else
                  {
                      return false;
                  }
              }
      
              public void DebugShowScores()
              {
                  foreach (var playerScore in PlayerScoresListContainer.PlayerScoresRanked)
                  {
                      Debug.Log(playerScore.Name + " " + playerScore.Score);
                  }
              }
      
          }
      
      }