ScoreManager.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class ScoreManager : MonoBehaviour {
  5. //单例
  6. public static ScoreManager instance;
  7. private void Awake()
  8. {
  9. instance = this;
  10. }
  11. public int Score;
  12. public int BestScore;
  13. public float BGM;
  14. public float AM;
  15. private GameObject bullet;
  16. public GameObject explode;
  17. // Use this for initialization
  18. void Start () {
  19. bullet = Resources.Load<GameObject>("Prefabs/Bullet");
  20. explode = Resources.Load<GameObject>("Prefabs/Explode");
  21. Init();
  22. if (PlayerPrefs.HasKey("BGM"))
  23. {
  24. BGM = PlayerPrefs.GetFloat("BGM");
  25. }
  26. else
  27. {
  28. BGM = 0.6f;
  29. }
  30. if (PlayerPrefs.HasKey("AM"))
  31. {
  32. AM = PlayerPrefs.GetFloat("AM");
  33. }
  34. else
  35. {
  36. AM = 0.6f;
  37. }
  38. }
  39. //分数的存储和调取 如果已经有了最高分 第一次显示时直接调取 如果没有先存后取
  40. public void Init()
  41. {
  42. if (PlayerPrefs.HasKey("Best"))
  43. {
  44. BestScore = PlayerPrefs.GetInt("Best");
  45. }
  46. }
  47. public void SetScore()
  48. {
  49. if (Score>BestScore)
  50. {
  51. PlayerPrefs.SetInt("Best",Score);
  52. }
  53. }
  54. // Update is called once per frame
  55. void Update () {
  56. GetComponent<AudioSource>().volume = BGM;
  57. bullet.GetComponent<AudioSource>().volume = AM;
  58. explode.GetComponent<AudioSource>().volume = AM;
  59. }
  60. }