Unity3d Local Data storage

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.