This is the 30th day of my participation in the August Challenge

Resource to load

Load the map

Implementation steps:

  1. Create the Image and create the scriptResImageTestMounted on it
  2. Import the image named “Unity” into a folder named “Resources”
  3. Convert image format to “Sprite (2D and UI)”
  4. Write the code and run it

Folder in project:

Image format:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ResImageTest : MonoBehaviour { private Image m_Image; Void Start() {m_Image = GetComponent<Image>(); // set m_image.sprite = resources.load <Sprite>("Unity"); }}Copy the code

Load the audio

Implementation steps:

  1. Create an empty object and add to itAudioSourcecomponent
  2. Import the audio into a folder named “Resources”
  3. Write code, mount the code to the object created in 1, and run

The implementation code is as follows:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ResAudioTest : MonoBehaviour 
{
    // Sound components
    private AudioSource au;
    // Sound file
    private AudioClip ac;

    void Start ()
    {
        // Find the component on the object
        au = gameObject.GetComponent<AudioSource>();
        
        // Dynamic loading using the Resources class must correspond to the Resources folder
        // Separate multilayer folders with "/", for example: "Sound/ BMG"
        In addition to generics in the example above, casts can also be used
        ac = (AudioClip)Resources.Load("bgm");

        // Clip the audio to the source
        au.clip = ac;     
        
        // Play after 1s delay
        au.PlayDelayed(1); }}Copy the code

Resources to uninstall

  1. All Resources. UnloadUnusedAssets uninstall without reference Resources

  2. UnloadAsset unloads a single resource, only a single base resource (e.g. Texture, Sprite), not GameObject.

  3. Resources. UnloadAsset uninstall, Sprite would only uninstall Sprite components, and associated Texture2D, not uninstall Texture2D pictures, need Resources. The call UnloadUnusedAssets, Will uninstall the Texture2D image. So after unloading Sprite cited the Sprite of objects in a scene is not affected, only then call Resources. After UnloadUnusedAssets, image information will be lost.

  4. When Resources.UnloadAsset is uninstalled, the Texture2D image is uninstalled and the image information of the scene is lost.

  5. Resources. UnloadUnusedAssets can unload the GameObject in memory, need to put the reference variable is set to null


Other concepts

The index information is a serialized lookup tree that is used to locate the resource name to the resource’s file GUID and local ID, as well as to find the resource itself.

On most platforms, the search tree is BST, and its build time complexity is O(nlog(n)). The build time increases with the increase of n (the more resources, the longer the build time).

This BST tree building process is impossible to skip when the game starts, and if the number of Resources under Resources exceeds 10,000, it can take up to a few seconds of startup time on low-end phones. The fact that not all of these resource indexes need to be pre-loaded can slow down the game’s performance.