Unity注册表实现排行榜

时间:2022-09-04 14:33:51

unity通过电脑注册表实现分数存储排行榜。
1.得到注册表中的分数

        int firstSocre=0;
int scendScore=0;
int thirdScore=0;
firstSocre = PlayerPrefs.GetInt("firstSocre");
scendScore = PlayerPrefs.GetInt("scendScore");
thirdScore = PlayerPrefs.GetInt("thirdScore");

2.判断是否注册表是否有数字据并写进去

if (!PlayerPrefs.HasKey("firstSocre"))
{
PlayerPrefs.SetInt("firstSocre", firstSocre);
}
if (!PlayerPrefs.HasKey("scendScore"))
{
PlayerPrefs.SetInt("scendScore", scendScore);
}
if (!PlayerPrefs.HasKey("thirdScore"))
{
PlayerPrefs.SetInt("thirdScore", thirdScore);
}

3.排行榜实现

using UnityEngine;
using System.Collections;
using DG.Tweening;
public class PaiHangBangCtrl : MonoBehaviour
{
public GameObject nullObj;
public GameObject paihangbang;
public GameObject yuanScore;
int firstSocre=0;
int scendScore=0;
int thirdScore=0;
public int highScore;
int NowScore;
public Sprite[] sprites;
GameObject F;
GameObject S;
GameObject T;
GameObject O;
public GameObject Allhole;
public AnimalManager Manager;
public GameObject timeover;
public GameObject overUI;
public Score change;
void OnEnable()
{
NowScore = Score.Score1;
//PlayerPrefs.SetInt("firstSocre", 0);
//PlayerPrefs.SetInt("scendScore", 0);
//PlayerPrefs.SetInt("thirdScore", 0);
firstSocre = PlayerPrefs.GetInt("firstSocre");
scendScore = PlayerPrefs.GetInt("scendScore");
thirdScore = PlayerPrefs.GetInt("thirdScore");

ShowBang();
if (NowScore>thirdScore)
{
O = setScore(NowScore, yuanScore.transform.position, -0.5f, 1f);
//O.transform.SetParent(yuanScore.transform);
MarkScore(NowScore);
}

}

void Start()
{
//Score.Score1;
//Debug.Log(NowScore);

if (!PlayerPrefs.HasKey("firstSocre"))
{
PlayerPrefs.SetInt("firstSocre", firstSocre);
}
if (!PlayerPrefs.HasKey("scendScore"))
{
PlayerPrefs.SetInt("scendScore", scendScore);
}
if (!PlayerPrefs.HasKey("thirdScore"))
{
PlayerPrefs.SetInt("thirdScore", thirdScore);
}
}


void Update()
{

}

void MarkScore(int score)
{

if (NowScore > firstSocre)
{
PlayerPrefs.SetInt("scendScore", firstSocre);
PlayerPrefs.SetInt("thirdScore", scendScore);
//Destroy
// S = setScore(scendScore, new Vector3(2.4f, -5.4f, -1), -0.5f, 1f);
// T = setScore(thirdScore, new Vector3(2.4f, -6.4f, -1), -0.5f, 1f);
PlayerPrefs.SetInt("firstSocre", NowScore);
StartCoroutine(exchange(F));
return;
}
if (NowScore > scendScore&& NowScore<firstSocre)
{
PlayerPrefs.SetInt("thirdScore", scendScore);
//T = null;
//T = setScore(thirdScore, new Vector3(2.4f, -6.4f, -1), -0.5f, 1f);
PlayerPrefs.SetInt("scendScore", NowScore);
StartCoroutine(exchange(S));
return;
}
if (NowScore > thirdScore&& NowScore < scendScore)
{
PlayerPrefs.SetInt("thirdScore", NowScore);
StartCoroutine(exchange(T));
return;
}

}

IEnumerator exchange(GameObject obj)
{
yield return new WaitForSeconds(2f);
//GameObject yClone = Instantiate(yuanScore);
O.transform.DOScale(1.5f, 0.35f).SetEase(Ease.OutBack).OnComplete(() =>
{
O.transform.DOMove(obj.transform.position, 1f).SetEase(Ease.OutBack);
O.transform.DOScale(1f, 0.6f);

});
yield return new WaitForSeconds(1f);
Destroy(obj);

}
//IEnumerator Sort(GameObject numSorce)
//{
// if(NowScore > firstSocre)
// {
// S.
// }
// yield return new WaitForSeconds(1f);
//}
public void ShowBang()
{
//paihangbang.SetActive(true);
GameObject[] animal = GameObject.FindGameObjectsWithTag("clone");
for (int i = 0; i < animal.Length; i++)
{
Destroy(animal[i]);

}

Allhole.SetActive(false);
timeover.SetActive(false);
overUI.SetActive(true);
Manager.StartGame = false;
Score.Score1 = 0;
Score.NowScore = 0;

F = setScore(firstSocre, new Vector3(2.4f, -0.3f, -1), -0.5f, 1f);
S = setScore(scendScore, new Vector3(2.4f, -1.3f, -1), -0.5f, 1f);
T = setScore(thirdScore, new Vector3(2.4f, -2.3f, -1), -0.5f, 1f);

}


GameObject setScore(int scoreNum, Vector3 pos, float kuang, float numscale)
{
GameObject clone = (GameObject)Instantiate(nullObj, pos, Quaternion.identity);
clone.name = scoreNum.ToString();
char[] chars = scoreNum.ToString().ToCharArray();
for (int i = 0; i < chars.Length; i++)
{
int index = int.Parse(chars[chars.Length - i - 1].ToString());
GameObject indexclone = (GameObject)Instantiate(nullObj, new Vector3(pos.x + i * kuang, pos.y, pos.z), Quaternion.identity);
indexclone.transform.SetParent(clone.transform);
indexclone.transform.localScale = Vector3.one * numscale;
indexclone.AddComponent<SpriteRenderer>().sprite = sprites[index];
}
return clone;
}
public void againGame()
{
Manager.StartGame = true;

if(Manager.StartGame)
{

if(F!=null)
Destroy(F);
if (S!= null)
Destroy(S);
if (T!= null)
Destroy(T);
if (O!= null)
Destroy(O);
Debug.Log(Score.NowScore);
change.ChangeScore(Score.NowScore);
Allhole.SetActive(true);
timeover.SetActive(true);
timeover.GetComponent<TimeManager>().AllTime = 160f;
overUI.SetActive(false);
paihangbang.SetActive(false);
timeover.GetComponent<TimeManager>().win = true;
}
}
}