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); } }}
- Unity 数据存储
- Unity数据的存储
- Unity 数据存储
- unity 简单数据存储PlayerPrefs
- Unity数据存储路径总结
- unity中利用Dictionary存储数据
- [雨松MOMO] Unity数据存储路径总结
- 【Unity编辑器扩展简介】存储数据
- Unity存储数据的各种路径
- Unity 中存储数据到JSON本地文件
- C#篇-unity存储类成员数据
- Unity项目中使用SQLite来管理本地数据存储
- 【Unity&XML】数据存储创建&格式的选择&SetAttribute&InnerText
- unity中游戏装备数据怎么 建立,存储和读取?
- 【unity插件】多平台数据存储插件IBoxDB学习
- 【Unity】Unity数据加密
- unity开发之三:www访问数据库(数据存储和数据获取)
- Unity PlayerPrefs 存储字段
- MJPEG
- Visual Studio Code(VSCODE)自定义语言和颜色主题
- IOS object-c 中定时器NSTimer的开启与关闭
- Tomcat的URL中文乱码解决以及传输优化
- DTD(Document Type Definition文档类型定义)
- Unity 数据存储
- app推送功能
- vs 打印信息到Output
- XML Schema基本结构
- Android 第三方开源 SlidingMenu 使用详解
- busybox passwd修改密码
- 大排量 下次换车,一定要大排量
- POJ 1426-Find The Multiple
- 第1章第1节练习题5 无序表删除指定区间值