2017-02-09 6 views
0

私はPlayerPrefs.SetInt("counter", 1);を認識していますが、カウンタを作ってカウンタスコアを保存しようとしています。 PlayerPrefs.AddInt("Counter", 1);はありませんでした。Playerprefsインクリメントを追加する

+0

あなたは 'PlayerPrefs.GetInt( "カウンタ" を試してみました)+ 1? – Abion47

+0

@ Abion47より具体的には、PlayerPrefs.SetInt( "counter"、(PlayerPrefs.GetInt( "counter")+ 1)); ' –

答えて

0

AddIntのようなものはありません。あなたは古いintPlayerPrefs.GetIntと読まなければなりません。読み取ったデータを1でインクリメントしてから、PlayerPrefs.SetIntに戻してください。

増分するたびに値を保存しないでください。ゲームプレイ中にインクリメントしますが、ゲームを終了するか、次のシーンに進むときにはのみを保存してください。これにより、常にゲームプレイ中にデータの読み書きがパフォーマンスに影響を与えないことが保証されます。

あなたはまだそれから、ここでこれを行うことを主張する場合はそれを行うことができヘルパークラスである:

namespace EXT 
{ 
    public class PlayerPrefs 
    { 
     public static void AddInt(string key, int numberToAdd) 
     { 
      //Check if the key exist 
      if (UnityEngine.PlayerPrefs.HasKey(key)) 
      { 
       //Read old value 
       int value = UnityEngine.PlayerPrefs.GetInt(key); 

       //Increment 
       value += numberToAdd; 

       //Save it back 
       UnityEngine.PlayerPrefs.SetInt(key, value); 
      } 
     } 

     public static void SubstractInt(string key, int numberToSubstract) 
     { 
      //Check if the key exist 
      if (UnityEngine.PlayerPrefs.HasKey(key)) 
      { 
       //Read old value 
       int value = UnityEngine.PlayerPrefs.GetInt(key); 

       //De-Increment 
       value -= numberToSubstract; 

       //Save it back 
       UnityEngine.PlayerPrefs.SetInt(key, value); 
      } 
     } 
    } 
} 

使用

//Add one 
EXT.PlayerPrefs.AddInt("Counter", 1); 
//Substract one 
EXT.PlayerPrefs.SubstractInt("Counter", 1); 
関連する問題