当前位置:   article > 正文

点击按钮人数加一,并存储数据_点击按钮后统计加一

点击按钮后统计加一


方法一:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class DataChange : MonoBehaviour {
  6. public Button StartBtn;
  7. public Text DataText;
  8. public int num;
  9. void Start ()
  10. {
  11. num = PlayerPrefs.GetInt("num", num);
  12. StartBtn.onClick.AddListener(() =>
  13. {
  14. num++;
  15. PlayerPrefs.SetInt("num", num);
  16. });
  17. }
  18. void Update ()
  19. {
  20. DataText.text = num.ToString();
  21. PlayerPrefs.GetInt("num",num);
  22. }
  23. }

方法二:

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. /// <summary>
  6. /// 点击按钮人数加一,关闭存储数据,再次点击时要从存的数据开始加
  7. /// </summary>
  8. public class TestPlayerPrefs : MonoBehaviour {
  9. private static TestPlayerPrefs instance;
  10. public int Count =1;
  11. public Text CountText; //统计人数Text
  12. void Awake()
  13. {
  14. instance = this;
  15. Count = 0;
  16. }
  17. void Start()
  18. {
  19. Count = PlayerPrefs.GetInt("Count", Count);
  20. CountText.text = "" + PlayerPrefs.GetInt("Count").ToString();
  21. }
  22. //记录人数
  23. public static TestPlayerPrefs Instance
  24. {
  25. get
  26. {
  27. if (instance != null) return instance;
  28. else
  29. {
  30. return null;
  31. }
  32. }
  33. }
  34. //点击按钮人数加一并存储
  35. public void OnClick()
  36. {
  37. CountText.text = "" + ++Count;
  38. Debug.Log(Count);
  39. PlayerPrefs.SetInt("Count", Count);
  40. }
  41. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/496891
推荐阅读
相关标签
  

闽ICP备14008679号