Unity 3D 持久化数据 PlayerPrefs EditorPrefs

 PlayerPrefs是Unity 自带的存档方法,它的优点是使用起来非常方便。引擎已经封装好GetKey 以及 SetKey 等的方法,并且还做保存数据的优化。由于保存数据可能是个耗时操作,频繁地保存可能会带来卡顿,所以Unity 默认会在应用程序切入后台时统一保存文件,开发者也可以强制调用 Save 来保存。

缺点是编辑器模式下查看存档非常不方便。

PlayerPrefs的储存位置 
在Mac OS X上PlayerPrefs存储在~/Library/PlayerPrefs文件夹,名为unity.[company name].[product name].plist
在Windows独立模式下,PlayerPrefs被存储在注册表的 HKCU\Software[company name][product name]键下(打开“运行”输入regedit打开注册表)。

这里company和product名是在Project Setting中设置的.。

using UnityEngine;

public class PlayerPrefsMyTools : MonoBehaviour
{
    private void Start()
    {
        PlayerPrefs.SetInt("MyIntValue", 100);
        PlayerPrefs.SetFloat("MyFloatValue", 200.5f);
        PlayerPrefs.SetString("MyStringValue", "Hello World!");

        PlayerPrefs.Save();

        Debug.Log(PlayerPrefs.GetInt("MyIntValue").ToString());
        Debug.Log(PlayerPrefs.GetFloat("MyFloatValue").ToString());
        Debug.Log(PlayerPrefs.GetString("MyStringValue"));

        if (PlayerPrefs.HasKey("MyIntValue"))
        {
            Debug.Log("HasKey : MyIntValue");
        }

        PlayerPrefs.DeleteKey("MyFloatValue");

        if (!PlayerPrefs.HasKey("MyFloatValue"))
        {
            Debug.Log("not haskey : MyFloatValue");
        }

        PlayerPrefs.DeleteAll();
    }
}

//--EditorPrefs

在编辑器模式下,Unity 也提供了一组存档功能,它不需要考虑运行时的效率,所以没有采用PlayerPrefs优化的方式,而是直接保存了。

using UnityEditor;
using UnityEngine;

public class EditorPrefsMyTools
{
    [MenuItem("MyEditorPrefs/EditorPrefsTest", false, 0)]
    static void EditorPrefsFunction()
    {
        EditorPrefs.SetInt("MyIntValue", 100);
        EditorPrefs.SetFloat("MyFloatValue", 200.5f);
        EditorPrefs.SetString("MyStringValue", "Hello World!");
        EditorPrefs.SetBool("MyBoolValue", false);

        Debug.Log(EditorPrefs.GetInt("MyIntValue").ToString());
        Debug.Log(EditorPrefs.GetFloat("MyFloatValue").ToString());
        Debug.Log(EditorPrefs.GetString("MyStringValue"));
        Debug.Log(EditorPrefs.GetBool("MyBoolValue").ToString());

        if (EditorPrefs.HasKey("MyIntValue"))
        {
            Debug.Log("HasKey : MyIntValue");
        }

        EditorPrefs.DeleteKey("MyFloatValue");

        if (!EditorPrefs.HasKey("MyFloatValue"))
        {
            Debug.Log("not haskey : MyFloatValue");
        }

        EditorPrefs.DeleteAll();
    }
}

//--PlayerPrefs 保存复杂结构

PlayerPrefs可以保存字符串,结合JSON的序列化和反序列化功能,它就可以保存各种复杂的数据结构了。

另外,保存存档取决于硬件当时的条件,完全有可能保存不上,所以可以通过try...catch来捕捉异常。

using System;
using System.Collections.Generic;
using UnityEngine;

public class PlayerPrefsAndJSONMyTools : MonoBehaviour
{
    [Serializable]
    public class Record
    {
        public string name;
        public int intValue;
        public List strsList;
    }
    private void Start()
    {
        Record record = new Record();
        record.name = "my name str";
        record.intValue = 100;
        record.strsList = new List() { "str1", "str2" };

        string jsonstr = JsonUtility.ToJson(record);
        Debug.Log(jsonstr);

        try
        {
            PlayerPrefs.SetString("record", jsonstr);
        }
        catch (Exception err)
        {
            Debug.LogError("record err :" + err.ToString());
        }

        record = null;
        record = JsonUtility.FromJson(PlayerPrefs.GetString("record"));
        Debug.Log(record.name);
        Debug.Log(record.intValue.ToString());
        for (int i = 0; i < record.strsList.Count; i++)
        {
            Debug.Log(record.strsList[i]);
        }
    }
}

 

你可能感兴趣的:(Unity,3D,游戏开发,第二版,学习笔记)