Unity注册表实现排行榜

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;
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值