方法一:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DataChange : MonoBehaviour {
public Button StartBtn;
public Text DataText;
public int num;
void Start ()
{
num = PlayerPrefs.GetInt("num", num);
StartBtn.onClick.AddListener(() =>
{
num++;
PlayerPrefs.SetInt("num", num);
});
}
void Update ()
{
DataText.text = num.ToString();
PlayerPrefs.GetInt("num",num);
}
}
方法二:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
/// <summary>
/// 点击按钮人数加一,关闭存储数据,再次点击时要从存的数据开始加
/// </summary>
public class TestPlayerPrefs : MonoBehaviour {
private static TestPlayerPrefs instance;
public int Count =1;
public Text CountText; //统计人数Text
void Awake()
{
instance = this;
Count = 0;
}
void Start()
{
Count = PlayerPrefs.GetInt("Count", Count);
CountText.text = "" + PlayerPrefs.GetInt("Count").ToString();
}
//记录人数
public static TestPlayerPrefs Instance
{
get
{
if (instance != null) return instance;
else
{
return null;
}
}
}
//点击按钮人数加一并存储
public void OnClick()
{
CountText.text = "" + ++Count;
Debug.Log(Count);
PlayerPrefs.SetInt("Count", Count);
}
}