3-10. 5조 최고 점수 저장, UI 정보 반영

keubung·2024년 10월 18일
  • 최고 점수 저장
    • PlayerPerfs : 소규모의 데이터 저장 가능 But 보안 취약
      - 값 설정
      PlayerPrefs.SetInt("키", 값); → 정수 저장
      PlayerPrefs.SetFloat("키", 값); → 실수 저장
      PlayerPrefs.SetString("키", 값); → 문자열 저장

      - 값 불러오기
      : 저장할 때 사용한 키의 이름​을 불러옵니다.
      int a = PlayerPrefs.GetInt("정수를 저장할 때 사용한 키 이름"); → 정수 불러오기
      float a = PlayerPrefs.GetFloat("실수를 저장할 때 사용한 키 이름"); → 실수 불러오기
      string a = PlayerPrefs.GetString("문자열를 저장할 때 사용한 키 이름"); → 문자열 불러오기

      - 기타
      PlayerPrefs.HashKey("키 이름"); → 해당 키가 존재하는가?
      DeleteAll() : 모든 데이터 삭제, 이 함수를 사용할 경우 경고 메시지가 출력된다.
      DeleteKey("키 이름") : 해당 키와 대응하는 값을 삭제한다.
      Save() : 모두 저장

      [출처] Unity - 데이터 저장 (PlayerPrefs)|작성자 Knight

      → PlayerPerfs 사용

      PlayerPrefs.SetFloat(키(key), 값(value));
      PlayerPrefs.Save();
      RocketEnergySystem.cs

      private void Start()
      {
          LoadHighScore();
      }
      
      public void UpdateHighScore()
      {
          if (highScore < currentScore)
          {
              highScore = currentScore;
              SaveHighScore();
          }
      
          HighScoreTxt.text = $"HIGH : {highScore} M";
      }
      
      public void SaveHighScore()
      {
          PlayerPrefs.SetFloat(HighScoreKey, highScore);
          PlayerPrefs.Save();
      }
      
      private void LoadHighScore()
      {
          if (PlayerPrefs.HasKey(HighScoreKey))
          {
              highScore = PlayerPrefs.GetFloat(HighScoreKey);
          }
      }
      
      public float GetHighScore()
      {
          return highScore;
      }

  • 연료의 양이 UI에 실시간으로 반영되지 않고 버튼을 누를 때 마다 반영이 됨.
    → 연료와 연료 바의 계산을 Update문으로 이동
    → 매 프레임마다 계산 후 적용

    Shoot 메서드에 있던 코드 이동

    private void Update()
    {
        currentFuel += 0.01f;
        currentFuel = Mathf.Min(currentFuel, maxFuel);
        currentFuel = Mathf.Round(currentFuel * 100) / 100;   // 이동
        fuelBar.fillAmount = currentFuel / maxFuel;    // 이동
        FuelTxt.text = $"Fuel : {currentFuel}";
    }
profile
김나영(Unity_6기)

0개의 댓글