美文网首页
C# 文件读写

C# 文件读写

作者: 游戏创作者 | 来源:发表于2020-03-04 17:33 被阅读0次

    在unity中实现如下:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using System.IO;
    
    public class FileManager : MonoBehaviour
    {
     
        // Start is called before the first frame update
        void Start()
        {
            getSavePath();
        }
    
        //保存关卡数据
        public void saveLeavelData(string name, string content) {
            string fullPath = getSavePath() + name;
            if (!File.Exists(fullPath)) {
                File.Create(fullPath).Dispose();
            }
            Debug.Log(fullPath);
            if (content != null && content.Length > 0) {
                File.WriteAllText(fullPath, content);
            } 
        }
    
        //获取关卡数据
        public string getLevelData(string name) {
            string fullPath = getSavePath() + name;
            if (File.Exists(fullPath))
            {
                string content = File.ReadAllText(fullPath);
                return content;
            }
            return null;
        }
    
        //获取存储路径
        string getSavePath() {
            string savePath = Application.persistentDataPath + "/MathBlock/";
            string path = Path.GetFullPath(savePath);
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            return path;
        }
    
    }
    
    

    注:创建文件后要调Dispose(),不然会报错

    相关文章

      网友评论

          本文标题:C# 文件读写

          本文链接:https://www.haomeiwen.com/subject/paublhtx.html