Unity uses file streams to manipulate files under Streamingassets

Source: Internet
Author: User

Background:

1. When Unity first starts, it copies the first package resources under Streamingassets to the Persistentdatapath directory.

2, unity-android platform on this operation can only use WWW to load the files under Streamingassets, resulting in speed problems.

Note:

1. This program requires you to have a little knowledge of Android.

Solution:

1, in the packaging, the streamingassets under the file directory to make a record, Each file that is packaged in a relative streamingassets path is recorded in a files.txt, such as a file under Streamingassets bundles/ Testbundles.unity3d, Files.txt's name is Bundles/testbundles.unity3d.

2, the Android layer read the file stream, in unity with the WWW read files.txt, and then read the path data in the Files.txt, loop. The path is then passed to Android to get the stream, which then flows into unity, and then unity uses the file stream to write the stream to Persistentdatapath, where the efficiency issue can be ignored, and unity's essence is an Android activity, Although written in the exile in C #, but the optimization of the WWW loading has been improved many times, the Android code is as follows.

 public  byte  null                  Span style= "COLOR: #000000" >;  try   {InputStream = Assetmanagerandoird.open (path);                    catch   (IOException e) { LOG.V (  " unity  "   return   readtextbytes (InputStream); }

3, the code of the 2nd, if the file specified on the path is empty, most of the real-world apps will be crash directly, and will not be actively throwing exceptions.

4, for the 3rd optimization, in the OnCreate to obtain the root directory of all the files and folders, Assetmanager assets = assets.list ("");

Then use recursion to get all the files under all subdirectories, record the path + file name separately, and log the information to a list list<string> filenamelist = new arraylist<string> ().

5, each time the file is read to the empty Filenamelist.contains (path), determine whether the file exists, if there is read, if not exist then do not read, do not exist when you need to return a 0-byte stream to unity, if return null, the same crash will occur, For example, return new Byte[0], which will completely prevent the app from crashing.

Okay, here's the copy and load of the files under the Streamingassets folder.

Unity uses file streams to manipulate files under Streamingassets

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.