现在的位置: 首页 > 综合 > 正文

U3D C#文件读写

2018年06月07日 ⁄ 综合 ⁄ 共 3309字 ⁄ 字号 评论关闭

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);
}
}

}

抱歉!评论已关闭.