usingUnityengine;usingSystem.Collections;
Path Tool class Public classpathkit{/** Suffix constant character*/ Public Const stringSUFFIX =". txt"; Const stringprefix="file://"; Const stringformat=". Unity3d"; Public Static stringresroot=application.persistentdatapath+"/"; Public Static stringGetstreamingassetspath (stringp_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 stringGetosdatapath (stringp_filename) { stringpath; 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); returnpath; } Public Static stringGeturlpath (stringP_filename,BOOLNeedprefix,BOOLNeedformat) { stringpath; 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); returnpath; } Public Static stringGetFileName (stringpath) { string[] _list= path. Split (New Char[]{'/'}); if(_list. Length>0)return_list[_list. length-1]; Else return ""; } Public Static stringGetfiledir (stringpath) {Path= path. Replace ("\\","/"); Path= path. Substring (0, Path. LastIndexOf ("/")); returnpath; } Public Static voidCreatedirifnotexists (stringpath) { stringDIR =getfiledir (path); if(!System.IO.Directory.Exists (dir)) {System.IO.Directory.CreateDirectory (dir); } } }
private void Savefriendtolocal () { string cachestr = "" ;//content string Path = Pathkit.resroot + string = 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 voidloadfriendfromlocal () {Try { stringPath = Pathkit.resroot +string. Format (Chat_cache_dir,userid); if(! File.exists (PATH))return; FileStream FS=NewFileStream (Path, FileMode.OpenOrCreate, FileAccess.Read); byte[] bytes =New byte[FS. Length]; Fs. Read (Bytes,0, (int) fs. Length); Fs. Close (); Mcachefriend=Newlist<string> ();string[] arr = Encoding.UTF8.GetString (bytes). Split ('\ n'); for(inti =0; I < arr. Length; i++) { string[] item = Arr[i]. Split ('\ t'); Mcachefriend.add (item[0]); } } Catch(System.Exception e) {Debug.Log (e.message); } }
Unity3d Local Data storage