unity3d 本地数据存储

using UnityEngine;
using System.Collections;
//路径工具类
public class PathKit
{
    /** 后缀常量字符 */
    public const string SUFFIX = ".txt";
    const string PREFIX="file://";
    const string FORMAT=".unity3d";   
    public static string RESROOT=Application.persistentDataPath+"/";
    public static string GetStreamingAssetsPath (string p_filename)
    {
        string _strPath = "";
        if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer )
            _strPath = "file://" + Application.streamingAssetsPath + "/" + p_filename + ".unity3d";
        else if (Application.platform == RuntimePlatform.Android )
            _strPath = Application.streamingAssetsPath + "/" + p_filename + ".unity3d";
        else if (  Application.platform == RuntimePlatform.OSXEditor ||  Application.platform == RuntimePlatform.IPhonePlayer)
            _strPath = "file://"+ Application.streamingAssetsPath + "/" + p_filename + ".unity3d";
        
        return _strPath;
    }   
   
   
    public static string GetOSDataPath (string p_filename)
    {
        string path;
        path = "";
        
        if (Application.platform == RuntimePlatform.OSXEditor)
            path = Application.persistentDataPath + p_filename;
        
        if (Application.platform == RuntimePlatform.IPhonePlayer)
            path = RESROOT + p_filename;
        
        
        if (Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsEditor)
            path = Application.dataPath +"/cache/"+ p_filename;   
        
        
        if (Application.platform == RuntimePlatform.Android)  
            path =RESROOT + p_filename;
    //    Debug.LogWarning("===========path:"+path);
        return path;
    }
   
        public static string GetURLPath (string p_filename,bool needPreFix,bool needFormat)
    {
        string path;
        path = "";
        
        if (Application.platform == RuntimePlatform.OSXEditor)
            path = Application.persistentDataPath+"/" + p_filename;
        
        if (Application.platform == RuntimePlatform.IPhonePlayer)
            path = RESROOT + p_filename;
        
        
        if (Application.platform == RuntimePlatform.WindowsEditor)
            path = Application.dataPath +"/cache/"+ p_filename;   
        
            if (Application.platform == RuntimePlatform.WindowsPlayer)
            path = Application.dataPath +"/cache/"+ p_filename;        
        
        if (Application.platform == RuntimePlatform.Android)  
            path = RESROOT + p_filename;
        if(needPreFix) path=PREFIX+path;
        if(needFormat) path=path+FORMAT;   
        //Debug.LogWarning("===========path:"+path);
        return path;
    }
   
    public static string getFileName(string path)
    {
        
    string[] _list=    path.Split(new char[]{'/'});
        
        
        
    if(_list.Length>0)    return _list[_list.Length-1];
        else
            return "";
        
    }
   
    public static string getFileDir(string path)
    {
        path = path.Replace("\\","/");
        path = path.Substring(0,path.LastIndexOf("/"));
        return path;
    }
   
    public static void CreateDirIfNotExists(string path)
    {
        string dir = getFileDir(path);
        if(!System.IO.Directory.Exists(dir))
        {
            System.IO.Directory.CreateDirectory(dir);
        }
    }
   
}

private void SaveFriendToLocal()
    {
        string cacheStr = "";//内容string path = PathKit.RESROOT + string.Format(CHAT_CACHE_DIR, UserId);
        FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);
        sw.Flush();
        sw.BaseStream.Seek(0, SeekOrigin.Begin);
        sw.Write(cacheStr);
        sw.Close();
    }


private void LoadFriendFromLocal()
    {
        try
        {
            string path = PathKit.RESROOT + string.Format(CHAT_CACHE_DIR,UserId);
            if (!File.Exists(path)) return;
            FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read);
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, (int)fs.Length);
            fs.Close();
            mCacheFriend = new List<string> ();string[] arr = Encoding.UTF8.GetString (bytes).Split ('\n');
            for (int i = 0; i < arr.Length; i++) {
                string[] item = arr[i].Split ('\t');
                mCacheFriend.Add (item[0]);
            }   
        }
        catch (System.Exception e)
        {
            Debug.Log(e.Message);
        }
    }

转载于:https://www.cnblogs.com/feiyanstudio/articles/7339474.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值