• QQ
  • nahooten@sina.com
  • 常州市九洲新世界花苑15-2

游戏开发

常州小程序开发游戏-Unity3D数据保存

原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2019/0115/336.html

常州小程序开发游戏-Unity3D数据保存

 
Unity3d提供了一个用于本地持久化保存与读取的类——PlayerPrefs。工作原理非常简单,以键值对的形式将数据保存在文件中,然后程序可以根据这个名称取出上次保存的数值。下面常州微信公众平台给大家介绍的数据保存就需要用到PlayerPrefs类,一起来看看吧。

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SaveData : MonoBehaviour {
    // Use this for initialization
    void Start () {
        SaveData1();
        SaveData2();
    }
    // Update is called once per frame
    void Update () {
    }
    void SavaData1()
    {
    }
    void SaveData2()
    {
        string path = Application.dataPath + "/常州微信小程序开发1.txt";
        File.AppendAllText(path, "\thisCurrentTime=" + System.DateTime.Now.ToShortDateString());
        string[] ss = File.ReadAllLines(path);
        Dictionary<string, string> dic = new Dictionary<string, string>();
        foreach (string s in ss)
        {
            string[] s1 = s.Split('=');
            if (s1 != null && s1.Length >=2)
            {
                dic[s1[0]] = s1[1];
                Debug.Log(s1[0]);
            }
        }
        Debug.Log(dic["thisCurrentTime"]);
    }
    void SaveData1()
    {
        PlayerPrefs.SetString("CurrentTime", System.DateTime.Now.ToShortDateString());
        PlayerPrefs.Save();
        Debug.Log(PlayerPrefs.GetString("CurrentTime"));
    }

上篇:上一篇:常州手游开发制作-Unity3D中读取json文件
下篇:下一篇:常州微信公众号开发-问题未将对象引用设置到对