android+unity3D实现数据的存取(PlayerPrefs)
android+unity3D实现数据的存取(PlayerPrefs) 我们先来看看PlayerPrefs的API吧,
我来解释一下,
SetInt是保存一个值PlayerPrefs.SetInt("Player Score",10);,
GetInt是读取一个值PlayerPrefs.GetInt("Player Score"),他们就是一对啦,作为整形的存取。
顾名思义,SetFloat和GetFloat为浮点型的存取,SetString和GetString是字符型的存取;
HasKey是指如果存有这个数,返回值就是true,反之,false;PlayerPrefs.HasKey("Player Score");
DeleteKey就是指删除这个数,PlayerPrefs.DeleteKey("Player Score");
DeleteAll就是删除所有数啦,PlayerPrefs.DeleteAll();
那下面我们来个简单的练习吧,
pref2.cs
- using UnityEngine;
- using System.Collections;
- public class pref2 : MonoBehaviour {
- // Use this for initialization
- void Start () {
- PlayerPrefs.SetInt("Player Score",10);
- print (PlayerPrefs.GetInt("Player Score")+"---"+PlayerPrefs.HasKey("Player Score"));
- PlayerPrefs.DeleteKey("Player Score");
- print(PlayerPrefs.GetInt("Player Score"));
- }
- // Update is called once per frame
- void Update () {
- }
- }
using UnityEngine; using System.Collections; public class pref2 : MonoBehaviour { // Use this for initialization void Start () { PlayerPrefs.SetInt("Player Score",10); print (PlayerPrefs.GetInt("Player Score")+"---"+PlayerPrefs.HasKey("Player Score")); PlayerPrefs.DeleteKey("Player Score"); print(PlayerPrefs.GetInt("Player Score")); } // Update is called once per frame void Update () { } }