Unity2D:PlayerPrefs.HasKey

时间:2016-09-30 21:22:17

标签: unity3d in-app-purchase

我正在使用playerprefs来保存数据。虽然我在关闭应用程序时遇到了保存这些数据的麻烦。你看我有一个IAP商店,当玩家购买时会给玩家一个飞旋镖,通过一个按钮激活飞旋镖效果(在我的脚本中完成)。我的问题是,当我关闭游戏然后重新打开它时,playerprefs.haskey没有保存我的回旋镖效果。虽然通过场景确实可以保存我的飞旋镖效果。这是我的剧本:

public bool forceActive = false; 
public GameObject BoomerangOn, BoomerangOff;
public static int buttonCount = 0;
static int timesActivated =  0;

void Start()
{
    if (PlayerPrefs.HasKey ("boomerangbutton")) {
        buttonCount = PlayerPrefs.GetInt ("boomerangbutton");
        BoomerangEffect();
    }
}

void Update()
{
    PlayerPrefs.SetInt("boomerangbutton", buttonCount);
}

public void Activated ()
{
    if(timesActivated < BoomeerangText.score)
    {
        timesActivated++;
        StartCoroutine(BoomerangEffect());
    }
}

IEnumerator BoomerangEffect()
{

        BoomerangOn.SetActive (true);
        yield return new WaitForSeconds (10.0f);
        BoomerangOn.SetActive (false);
        BoomerangOff.SetActive (true);
        yield return new WaitForSeconds (1f);
        BoomerangOff.SetActive (false);
        forceActive = false;
    }

第二次修改

好的,我研究了一下,并将回旋镖效果脚本与我的回旋镖文本脚本联系起来。当用户从我的IAP商店购买回旋镖时,他们将获得5个回旋镖,一旦点击,回旋镖文本int将下降(如5,4,3,2和1),因此我的buttoncount int(这就是为什么需要timetimesaed)。但是,我将激活功能更改为:

 public void Activated ()
   {
    if (timesActivated < BoomeerangText.score) {
            timesActivated++;
            StartCoroutine (BoomerangEffect ());
    }
}

到目前为止它适用于关闭应用程序时激活我的飞旋镖效果,但当它到达最后一个int(1)没有任何反应时,我的效果不会发生,到目前为止这是我唯一的问题。 以上是我的代码现在的更新版本。以下是我的Boomerang文本脚本:

public static int score = 0;        // The player's score.
public static int click = 1;
public GameObject button;


Text text;                      // Reference to the Text component.

// Use this for initialization
void Start()
{
    if (PlayerPrefs.HasKey ("boomerangTextInt")) {
        score = PlayerPrefs.GetInt("boomerangTextInt");
    }
} 

void Awake()
{
    text = GetComponent<Text>(); 
}

public void Update()
{
    SetScoreText();
    PlayerPrefs.SetInt("boomerangTextInt", score);
}

void SetScoreText()
{
    text.text = " " + score;
    if (score <= 0)
    {
        text.text = "None";
        button.GetComponent<Button>().interactable = false;
    }
    else if (score >= 1)
    {
        button.GetComponent<Button>().interactable = true;
    }  
    // Set the displayed text to be the word "Score" followed by the score value.
}
public void MinusBoomerangText()
{
    score -= click;
    text.text = " " + score;
}

}

在我的采购脚本中,我有这个:

        public int scoreValue = 5;

        if (String.Equals(args.purchasedProduct.definition.id, PRODUCT_5_BOOMERANG, StringComparison.Ordinal))
        {
        BoomerangEffect.buttonCount += 5;
            BoomerangText.score += scoreValue;
            Debug.Log("Purchase successfull");
        }

谢谢。:)

1 个答案:

答案 0 :(得分:0)

您没有调用{"city":{"id":2624652,"name":"Arhus","coord":{"lon":xxx,"lat":xxx},"country":"DK","population":0,"sys":{"population":0}},"cod":"200","message":0.0028,"cnt":40,"list":[{"dt":1475269200,"main":{"temp":284.9,"temp_min":284.897,"temp_max":284.9,"pressure":1017.03,"sea_level":1020.8,"grnd_level":1017.03,"humidity":79,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"clouds":{"all":48},"wind":{"speed":6.75,"deg":247.005},"rain":{"3h":0.04},"sys":{"pod":"n"},"dt_txt":"2016-09-30 21:00:00"},{"dt":1475280000,"main":{"temp":284.21,"temp_min":284.205,"temp_max":284.21,"pressure":1017.5,"sea_level":1021.27,"grnd_level":1017.5,"humidity":87,"temp_kf":0},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}] ... ,这意味着对.Save()的所有更改仅在内存中并且不会持久保存到磁盘,这意味着下次启动应用程序时,之前的所有更改都将丢失。

在保存功能中尝试以下操作。

PlayerPrefs

免责声明:我并不是说你应该在你的更新中做这件事,因为效率低下,但这是你问题的根本原因