• unity游戏存档


    使用Json存储在在本地

    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using UnityEngine;
    using Newtonsoft.Json;
    using System;
    using System.Text;
    using System.Linq;
    
    [Serializable]
    public class SaveGameData
    {
        public CharacterInfo characterInfo;
        public string title;
        [HideInInspector]
        public float savePosX;
        [HideInInspector]
        public float savePosY;
        [HideInInspector]
        public string path;
    }
    
    public class SaveGame 
    {
        public static int saveIndex = 1;
        public static string rootPath { get { return Application.streamingAssetsPath + "/Save"; } }
        private static List<SaveGameData> m_saveInfo;
        public static List<SaveGameData> SaveInfo
        {
            get {
                ReadPlayerAllInfo();
                return m_saveInfo;
            }
        }
    
        internal static void ClearSave(SaveGameData saveInfo)
        {
            m_saveInfo.Remove(saveInfo);
            string file= Path.GetFileName(saveInfo.path);
            string path = rootPath + "/" + file;
            if (File.Exists(path))
            {
                File.Delete(path);
            }
    #if UNITY_EDITOR
    
            path = rootPath + "/" + file + ".meta";
            if (File.Exists(path))
            {
                File.Delete(path);
            }
    #endif
    
        }
    
    
    
        public static CustomEvent onSaveSuccessful=EventFactory.CreatCustomEvent();
    
        public static void SavePlayerInfo(CharacterInfo characterInfo,Transform transform,string title)
        {
            saveIndex++;
            SaveGameData saveGame = new SaveGameData()
            {
                characterInfo = characterInfo,
                title = saveIndex + "" + title,
                savePosX = transform.position.x,
                savePosY = transform.position.y,
                path = rootPath +"/"+ saveIndex + ".json"
            };
    
            string s = JsonConvert.SerializeObject(saveGame);
            using (FileStream fs = new FileStream(saveGame.path, FileMode.OpenOrCreate, FileAccess.Write))
            {
                byte[] b = System.Text.Encoding.UTF8.GetBytes(s);
                fs.Write(b,0,b.Length);
            }      
            if (onSaveSuccessful != null)
            {
                onSaveSuccessful.Invoke();
            }
        }
    
    
        public static void ReadPlayerAllInfo()
        {
            string[] paths= Directory.GetFiles(rootPath);
            if (paths.Length == 0) return;
            List<string> validPath = new List<string>();
            for (int i = 0; i < paths.Length; i++)
            {
                if (paths[i].EndsWith(".json"))
                {
                    paths[i]=paths[i].Replace("\", "/");
                    validPath.Add(paths[i]);
                }
            }
            m_saveInfo = new List<SaveGameData>();
            saveIndex = validPath.Count+1;
            for (int i = 0; i < validPath.Count; i++)
            {
                SaveGameData loadPlayer = ReadJson(validPath[i]);
                m_saveInfo.Add(loadPlayer);
            }
        }
        static SaveGameData ReadJson(string path)
        {
            using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                byte[] data = new byte[fs.Length];
                if (data.Length==0)
                    return null;
                fs.Read(data, 0, data.Length);
                byte[] bomBuffer = new byte[] { 0xef, 0xbb, 0xbf };
                string dataStr = System.Text.Encoding.UTF8.GetString(data);
                if (data[0] == bomBuffer[0]
                    && data[1] == bomBuffer[1]
                    && data[2] == bomBuffer[2])
                {
                    int copyLength = data.Length - 3;
                    byte[] dataNew = new byte[copyLength];
                    Buffer.BlockCopy(data, 3, dataNew, 0, copyLength);
                    dataStr = System.Text.Encoding.UTF8.GetString(dataNew);
                }
                return JsonConvert.DeserializeObject<SaveGameData>(dataStr);
            }
        }
    }
  • 相关阅读:
    【Android Developers Training】 73. 布局变化的动画
    【Android Developers Training】 72. 缩放一个视图
    【Android Developers Training】 71. 显示翻牌动画
    svn更改地址怎么办
    python学习手册
    failed to bind pixmap to texture
    Ubuntu 12.04安装Google Chrome
    svn update 时总是提示 Password for '默认密钥' GNOME keyring: 输入密码
    重设SVN 的GNOME keyring [(null)] 的密码
    Nginx + uWSGI + web.py 搭建示例
  • 原文地址:https://www.cnblogs.com/DazeJiang/p/14826111.html
Copyright © 2020-2023  润新知