U3D C#文件读写

using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System.Collections.Generic;


public class FileData : MonoBehaviour {


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
public const string FILE_SAVEDATA = "appsave.v1";
public const string FILE_LEVELDATA = "level.v1";
public const string FILE_NAME="LEVELMESSAGE";
public static Dictionary<string,CustomsPass> customsPassDictionary=new Dictionary<string,CustomsPass>();
//读取文件保存路径
public static string SaveFilesPath
    {
        get
        {
#if UNITY_EDITOR
           return Path.Combine(Application.dataPath.Replace("/Assets", "/"), "downloadfiles");
#else
           return Application.persistentDataPath;
#endif
        }
    }

//获得文件详细路径
public static string SaveDataPath(string file)
    {
        return pathCombine(SaveFilesPath, string.Format("/{0}", file));
    }

public static string pathCombine(string mainPath,string path){
return mainPath+path;
}


public static bool loadFile(){
string path = SaveDataPath(FILE_SAVEDATA); //获得路径
if(System.IO.File.Exists(path)==false){ //验证文件是否存在
return false;
}
byte[] dat=File.ReadAllBytes(path);
if(dat==null) return false;
StringBuilder sb=new StringBuilder(dat.Length);
Encoding utf=Encoding.UTF8;
string readdata=utf.GetString(dat,0,dat.Length);
string[] strs=readdata.Trim().Split('\n');
for(int i=0;i<strs.Length;i++){
string[] lemse=strs[i].Split(';');
if(i==strs.Length-1) {
UseData.maxlevel=int.Parse(lemse[0].Split('=')[1].Trim());
UseData.explosionnum=int.Parse(lemse[1].Split('=')[1].Trim());
UseData.UniversalPowernum=int.Parse(lemse[2].Split('=')[1].Trim());
}else{
CustomsPass cpass=customsPassDictionary["level"+(i+1)];
if(lemse!=null && lemse.Length>0){
cpass.useMaxScore=int.Parse(lemse[1].Split('=')[1].Trim());
cpass.useLevel=int.Parse(lemse[2].Split('=')[1].Trim());
}
}
}

return true;
}

public static bool saveFile(){
string path = SaveDataPath(FILE_SAVEDATA); //获得路径
StringBuilder sb=new StringBuilder(16384);
for(int i=0;i<customsPassDictionary.Count;i++){
CustomsPass cpass=customsPassDictionary["level"+(i+1)];
sb.AppendFormat("index={0};useMaxScore={1};useLevel={2}\n",
               cpass.index,cpass.useMaxScore,cpass.useLevel
               );
}
sb.AppendFormat("maxlevel={0};explosionnum={1};UniversalPowernum={2}",UseData.maxlevel,UseData.explosionnum,UseData.UniversalPowernum);
try{
if(File.Exists(path)==true){
File.Delete(path);
}
byte[] dat_list=Encoding.UTF8.GetBytes(sb.ToString());
using(FileStream fs=File.OpenWrite(path)){
fs.Write(dat_list,0,dat_list.Length);
fs.Flush();
fs.Close();
}
}catch(IOException e){
Debug.LogError(e.ToString());
}
return true;
}


internal static void readLevelFile(){
TextAsset levelFile = Resources.Load(FILE_NAME, typeof(TextAsset)) as TextAsset;
if(!levelFile)
return;
byte[] dat=levelFile.bytes;
if(dat==null) return ;
StringBuilder sb=new StringBuilder(dat.Length);
Encoding utf=Encoding.UTF8;
string readdata=utf.GetString(dat,0,dat.Length);
string[] strs=readdata.Trim().Split('\n');
foreach(string s in strs){
string[] cp=s.Split(';');
CustomsPass customspass=new CustomsPass();
string index=cp[0].Split('=')[1];
customspass.index=int.Parse(index);
customspass.farget=int.Parse(cp[1].Split('=')[1]);
customspass.moves=int.Parse(cp[2].Split('=')[1]);
customspass.one_score=int.Parse(cp[3].Split('=')[1]);
customspass.two_score=int.Parse(cp[4].Split('=')[1]);
customspass.three_score=int.Parse(cp[5].Split('=')[1]);
customspass.JellyStatus=int.Parse(cp[6].Split('=')[1]);
customsPassDictionary.Add("level"+index,customspass);
}
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值