[Dumb Wood Unity3d] Miscellaneous 003 · Unity reads files from Android

Source: Internet
Author: User

This week was a problem for a few nights, that is to read the file in Android, this is a very simple thing, can imagine always good.

1. Reading files on the computer the first thing to solve is to read the files on the computer, this is not difficult, a bunch of information on the web. Like what:
  1.     
       
    1. < Span class= "KW1" style= "border:0px; margin:0px; padding:0px; Font-stretch:normal; Line-height:1.4em; Color:rgb (32, 96, 160); " >public  static  string  loadfile ( string  filepath)
    2. {
    3. stringwr.=Application.Streamingassetspath +"/"+FilePath;
    4. < Span class= "KW1" style= "border:0px; margin:0px; padding:0px; Font-stretch:normal; Line-height:1.4em; Color:rgb (32, 96, 160); " >return  file. readalltext ( url ;
    5. }

The file must be placed in the Assets\streamingassets directory (can have subdirectories), otherwise it will not be able to read the file after it is migrated to another device. The files in this directory are read-only and cannot be written, and my purpose is simply to read the files. Application.streamingassetspath will automatically return the correct path for the platform based on different platforms (the files in the Assets\streamingassets directory will be saved to different paths on different platforms. So you need to remove the correct path). Then you use File.readalltext to read the entire file and save it as a string object.

2. Read the file on AndroidComputerIt is very simple to read files on, after all, the data is much, and the file on Android is very tragic. Maybe it's all about iOS, but I'm a priority on Android.

You can try to search, basically the first search is Xuanhusung Momo's blog, he wrote "Read and write Files" way (100% can be successful). But that way you need to create the file in the code and then read the file, and all I need is to read the static file, which is the pre-created file. Momo is also more focused on iOS, so he hasn't done too much Android testing.

Other search articles are mostly talking about path problems (that is, what I said in the first headline). So, I have to complain, it's amazing, why so few people pay attention to the problem of Android read? Does this have to be written in the API documentation? (If there is, please tell me, I did not find, do not know what keywords to use)

Well, no nagging, less is less, but there is still data to mention the problem.say the solution, it is really simple to read files in Android, you have to use WWW to read, the following code:
  1.     
       
    1. < Span class= "KW1" style= "border:0px; margin:0px; padding:0px; Font-stretch:normal; Line-height:1.4em; Color:rgb (32, 96, 160); " >public  static  string  loadfile ( string  filepath)
    2. {
    3. stringUrl=Application.Streamingassetspath +"/"+FilePath;
    4. #if unity_editor
    5. < Span class= "KW1" style= "border:0px; margin:0px; padding:0px; Font-stretch:normal; Line-height:1.4em; Color:rgb (32, 96, 160); " >return  file. readalltext ( url ;
    6. #elif unity_android
    7. WWW www  =  new  www ( url ;
    8. while(!Www.IsDone) {}
    9. return  www. text
    10. #endif
    11. }
In theory, www is used to read Web files, and Android needs to be read in this way, which makes me feel strange. Maybe there's a better way? If there is, hope to share with me.

3. At the end there's a little place to note, if you want to split the text you read into rows, you can do this: return Www.text.Split (new string[] {"\ r \ n"}, Stringsplitoptions.none);

To use "\ r \ n" to split, rather than "\ n", otherwise there will be a messy strange problem (don't ask how I know, distressed, do not want to mention).

Well, I hope this article can help the same people as I have been tossing the problem.


The End.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[Dumb Wood Unity3d] Miscellaneous 003 · Unity reads files from Android

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.