This is the 30th day of my participation in the August Challenge
Resource to load
Load the map
Implementation steps:
- Create the Image and create the script
ResImageTest
Mounted on it - Import the image named “Unity” into a folder named “Resources”
- Convert image format to “Sprite (2D and UI)”
- 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:
- Create an empty object and add to it
AudioSource
component - Import the audio into a folder named “Resources”
- 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
-
All Resources. UnloadUnusedAssets uninstall without reference Resources
-
UnloadAsset unloads a single resource, only a single base resource (e.g. Texture, Sprite), not GameObject.
-
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.
-
When Resources.UnloadAsset is uninstalled, the Texture2D image is uninstalled and the image information of the scene is lost.
-
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.