Unity reads internal, external resources in a detailed

Source: Internet
Author: User
Tags readable
Unity reads internal, external resources in a detailed

The Unity read resource is roughly divided into two parts: an internal resource, what is an internal resource, or a resource that is imported into unity, which we call asset, a resource that unity has a ready-made management approach to. The other part is external resources, not imported unity, nor packaged assetbundle resources, such as a txt, PNG, LUA and other resources, you need to implement read and resource management. The following are specific: internal resources

Let's talk about the next platform path problem:

iOS platform

  Application.datapath:            application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/data readable
  Application.streamingassetspath:application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/xxx.app/data/raw        readable
  Application.persistentdatapath:    application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/documents
  readable and writable Application.temporarycachepath:application/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/library/caches             can be read and writable

DataPath is the app package installation path, and the app itself is here, and this directory is read-only. Streamingassetspath is the raw directory under DataPath.

There are three folders in the app's standalone data store directory: Documents,library and TMP.

Documents directory, which stores data that requires long-term retention, such as hot updates, icloud automatically backs up this directory.

Library directory, this directory has two subdirectories, caches and preferences.
Caches is a relatively temporary directory, suitable for storing temporary files for the download cache, which may be cleared by the system when there is insufficient space, and Application.temporarycachepath returns this path. I write the hot update temp file here, wait for a version of all content updates completely, and then transfer the content to the documents directory.
Preferences is used to apply storage preferences, read or set with Nsuserdefaults.

TMP directory, temporary directory, which holds data temporarily used by the application while it is running.

Android

Application.datapath:  /data/app/xxx.xxx.xxx.apk
application.streamingassetspath:  jar:file:///data/ App/xxx.xxx.xxx.apk/!/assets
application.persistentdatapath:  /data/data/xxx.xxx.xxx/files
Application.temporarycachepath:  /data/data/xxx.xxx.xxx/cache

Android These few path read permissions and iOS, it should be noted that the Application.streamingassetspath directory is the JAR package protocol, in unity need to use WWW to load the file, the other paths can be directly read by the file stream.

Editor

This will not say ... The general ...

On the issue of the release of resources, already have a small partner detailed answers, not verbose, point portal.
Portal External Resources

In other words, external resources, external resources need to follow the FTP, jar, HTTP, HTTPS and other protocols, the specific stitching needs to pay attention to one or fewer "/" problems, and then use the WWW to load on the line. Read external resources Most of the use of the PC, Android, iOS, etc. or thrown into the Persistentdatapath path. Specific examples are as follows:

Public texture2d loadtexture2d (string path)
{
      string dataPath = "file:///" + path; 
      www www = new www (dataPath);
        while (!www.isdone)   //Synchronous Wait, of course you can use the co-process, your own decision is good
        {
        }
        if (www.error! = null)
            return null;
     Texture2d texture = new Texture2d (4, 4, TEXTUREFORMAT.ARGB32, false); The initial size arbitrarily set, as long as greater than 1 is good, false is not open mipamaps
     texture. LoadImage (www.bytes);
     You can set some texture related properties, the WWW-loaded texture2d is the default property
      Texture.wrapmode = Texturewrapmode.clamp;
      Www.Dispose ();  Disconnect, if not disconnected, the next time will be prompted to occupy the connection
      return texture;
}


The need to pay attention is to release resources, directly with gameobject.destroyimmediate (texture) or Gameobject.destroy (texture), so as to unload memory, external resources is not a asset,unity will not help you to manage, you need to pay attention to the memory leak problem.

Resource Release Portal

Personal practice, if there is a mistake welcome message exchange.

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.