Unity 数据存储
2016-01-07 13:12
375 查看
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)
(2)
3 使用json
下载LitJson.dll 地址:http://lbv.github.io/litjson/ 开源
Project中 Import New Assets 添加进LitJson.dll
脚本中加入 using LitJson;
调用举例:
1
2
** 注意保存时只能创建文件,如果写入不同目录需要创建目录,然后创建文件
JSON 存储代码
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); } } }
相关文章推荐
- 一些关于数据存储和查询优化的想法
- 正确面对服务器数据存储防止数据丢失
- 高性能Javascript笔记 数据的存储与访问性能优化
- 在android开发中进行数据存储与访问的多种方式介绍
- Android 数据存储方式有哪几种
- unity实现多点触控代码
- 在Unity中实现动画的正反播放代码
- unity实现摄像头跟随
- Python使用shelve模块实现简单数据存储的方法
- android中使用SharedPreferences进行数据存储的操作方法
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- Android基础——数据存储
- [软件资讯]Unity已支持将3D游戏导出成Flash
- Unity 武器拖尾效果
- Android实现数据存储5种技术
- Android数据的四种存储方式