Unity 数据存储

来源:互联网 发布:网游加速器mac版 编辑:程序博客网 时间:2024/05/15 02:32

1 PlayerPrefs
DeleteAll Removes all keys and values from the preferences. Use with caution.
DeleteKey Removes key and its corresponding value from the preferences.
GetFloat Returns the value corresponding to key in the preference file if it exists.
GetInt Returns the value corresponding to key in the preference file if it exists.
GetString Returns the value corresponding to key in the preference file if it exists.
HasKey Returns true if key exists in the preferences.
Save Writes all modified preferences to disk.
SetFloat Sets the value of the preference identified by key.
SetInt Sets the value of the preference identified by key.
SetString Sets the value of the preference identified by key.
2 文本 后缀为 .txt
(1)

    public class ExampleClass : MonoBehaviour {        public TextAsset asset;        void Start() {            print(asset.text);        }    }

(2)

    TextAsset text = (TextAsset)Resources.Load("Map1");    if (text != null) {        Debug.Log (text.text);    } else {        Debug.Log("text is null");    }

3 使用json
下载LitJson.dll 地址:http://lbv.github.io/litjson/ 开源
Project中 Import New Assets 添加进LitJson.dll
脚本中加入 using LitJson;

    /// 数据保存    /// </summary>    /// <param name="tobject">Tobject.</param>    /// <param name="path">;Path.</param>    /// <typeparam name="T">The 1st type parameter.</typeparam>    public void Save (object tobject, string path)    {        string serializedString = JsonMapper.ToJson (tobject);        using (StreamWriter sw = File.CreateText(path)) {            sw.Write (serializedString);        }    }    /// 载入数据    /// </summary>    /// <param name="path">;Path.</param>    /// <typeparam name="T">The 1st type parameter.</typeparam>    public T Load<T> (string path)    {        if (File.Exists (path) == false)            return default(T);        using (StreamReader sr = File.OpenText(path)) {            string stringEncrypt = sr.ReadToEnd ();            if (string.IsNullOrEmpty (stringEncrypt))                return default(T);            return  JsonMapper.ToObject<T> (stringEncrypt);        }    }

调用举例:
1

        SAVE        public List<string> NotifyString;        private string LOADING_DATA_CONFIG_URL;         LOADING_DATA_CONFIG_URL = Application.persistentDataPath+ @"/LoadNotify.data";        NotifyString = new List<string> ();        NotifyString.Add(LOADING_DATA_CONFIG_URL);        Save(NotifyString,LOADING_DATA_CONFIG_URL);        LOAD        List<string> stringList = Load<List<string>> (LOADING_DATA_CONFIG_URL);

2

        public class Hero{            public string name;        }        SAVE        private string LOADING_DATA_CONFIG_URL;         LOADING_DATA_CONFIG_URL = Application.persistentDataPath+ @"/LoadNotify.data";        Hero hero = new Hero();        hero.name = LOADING_DATA_CONFIG_URL;        Save(hero,LOADING_DATA_CONFIG_URL);        LOAD        Hero hero = Load<Hero> (LOADING_DATA_CONFIG_URL);

** 注意保存时只能创建文件,如果写入不同目录需要创建目录,然后创建文件

JSON 存储代码

using UnityEngine;using System.Collections;using LitJson;using System.IO;using System.Collections.Generic;public class Hero{    public string name;}public class GamePanel : MonoBehaviour {    public UILabel mLable;    public UIButton mButton;    //      public List<string> NotifyString;    private string LOADING_DATA_CONFIG_URL;     // Use this for initialization    void Start () {        LOADING_DATA_CONFIG_URL = Application.temporaryCachePath+ @"/LoadNotify.data";        Hero hero = new Hero();        hero.name = LOADING_DATA_CONFIG_URL;        //      NotifyString = new List<string> ();        //      NotifyString.Add (LOADING_DATA_CONFIG_URL);        Save(hero,LOADING_DATA_CONFIG_URL);        if (mButton != null) {            mButton.gameObject.AddComponent<UIEventListener>();            UIEventListener.Get (mButton.gameObject).onClick = ClickSprite;        }    }    private void ClickSprite(GameObject go){        //      List<string> stringList = Load<List<string>> (LOADING_DATA_CONFIG_URL);        Hero hero = Load<Hero> (LOADING_DATA_CONFIG_URL);        if (mLable != null) {            mLable.text = hero.name;        }    }    // Update is called once per frame    void Update () {    }    /// 数据保存    /// </summary>    /// <param name="tobject">Tobject.</param>    /// <param name="path">;Path.</param>    /// <typeparam name="T">The 1st type parameter.</typeparam>    public void Save (object tobject, string path)    {        string serializedString = JsonMapper.ToJson (tobject);        using (StreamWriter sw = File.CreateText(path)) {            sw.Write (serializedString);        }    }    /// 载入数据    /// </summary>    /// <param name="path">;Path.</param>    /// <typeparam name="T">The 1st type parameter.</typeparam>    public T Load<T> (string path)    {        if (File.Exists (path) == false)            return default(T);        using (StreamReader sr = File.OpenText(path)) {            string stringEncrypt = sr.ReadToEnd ();            if (string.IsNullOrEmpty (stringEncrypt))                return default(T);            return  JsonMapper.ToObject<T> (stringEncrypt);        }    }}
0 0