使用PlayerPrefs保存Highscore,然后将其移至下一个场景(GameOver屏幕)?

时间:2019-02-09 03:44:29

标签: c# visual-studio unity3d game-engine

我有一款游戏,涉及在不到一分钟的时间内摧毁掉落的时钟以击败高分。我目前有一个脚本,可以在敲钟时跟踪乐谱。我试图在此屏幕上显示高分并将其保存在PlayerPrefs中。虽然它将在时钟更新时更新当前乐谱,但不会更新现有的高分文字或在再次启动程序时将其保存。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PickUp : MonoBehaviour
{
public GameObject explosionEffect;
public Rigidbody2D Clock;
public AudioClip pickupSound;
AudioSource audioSource;
public Text Score;
public Text highscore;
int Amount = 0;

public float SpeedUp = 1;

// Use this for initialization
void Start()
{
    audioSource = GetComponent<AudioSource>();
}

// Update is called once per frame
void Update()
{

}

private void OnTriggerEnter2D(Collider2D other)
{
    {
        if (other.tag == "Clock")
        {
            Destroy(Instantiate(explosionEffect.gameObject, transform.position, transform.rotation), 5f);
            audioSource.PlayOneShot(pickupSound, 1F);
            Amount++;
            Score.text = Amount.ToString() + " Clocks" ;

//Code of concern.
                if (Amount > PlayerPrefs.GetInt("Highscore", 0))
                {
                    PlayerPrefs.SetInt("Highscore", Amount);
                    highscore.text = "Highscore: " + Amount.ToString();
                    PlayerPrefs.Save();
                    Debug.Log("HS");
                }
//Speeds up the clocks when hit, until a limit is met.
                if (Clock.velocity.y >= -30)
                {
                    Clock.velocity += Vector2.down * SpeedUp;
                    Debug.Log("Speed is now " + Clock.velocity);
                }
            }
        }
    }
}

我还试图弄清楚如何将这些信息转移到Gameover场景中。我不确定是否应该依靠PlayerPrefs(如果可以做到这一点,我不知道)还是研究json来做到这一点。

0 个答案:

没有答案