Resources dynamic loading of resources

Resources dynamic loading of resources

The role of dynamic loading of Resources resources

  • Manually create the folder, put the resources that need to be dynamically loaded into the folder, and load the resources in the path directly through code without manual dragging.
  • Commonly used resource types for loading
    • Preset object——GameObject
    • Sound effect file——AudioClip
    • Text file - TextAsset
    • Image file——Texture

Ressources related APIs and usage

  1. Resources are loaded synchronously

    Resources.Load();
    
    • Note that the files loaded in this way are of Obj type and need to be manually converted to a specific type.
    • For example, loading a file
    TextAsset ta = Resources.Load("Txt/Test") as TextAsset; 
    
    • What should I do when loading files with the same name but different types?
      Use typeof to specify the type
    Resources.Load("Txt/Test", typeof(TextAsset)) as TextAsset;
    

    It can also be solved through the following generic method

    • Load all resources with the specified name
    Object[] objs = Resources.LoadAll("Tex/TestJPG");
    
    • Generic methods
    TextAsset ta2 = Resources.Load<TextAsset>("Tex/TestJPG");
    
  2. Resource asynchronous loading

    • The so-called asynchronous loading means opening a new thread to load resources, and the main thread can continue to run.
    • Compared with synchronous loading, asynchronous loading is performed in the main thread. For example: when we load a large resource file, if it is loaded synchronously, it may wait, which is time-consuming. The subsequent logic of the main thread must wait for the resource to be loaded. It can only continue after it is completed. The experience caused to the player is lag. Use asynchronous loading to let the main thread continue to execute the code logic it is responsible for, and let other threads be responsible for loading resources.
    • Note: For example, if you need to use the loaded resources in the code behind the resource loading code, the synchronous loading method is definitely available, If you load asynchronously, you need to wait for another thread to finish loading before you can get it, so you have to wait for at least one frame
      This code can be understood as Unity will open a new thread internally for resource loading a>
    Resources.LoadAsync<Texture>("Tex/TestJPG");
    
    • At the same time, we can also monitor this asynchronous loading event to facilitate timely acquisition of resources.
    • When the asynchronous loading is completed, this completed event will automatically call LoadOver internally after the asynchronous loading is completed.
    ResourceRequest rq = Resources.LoadAsync<Texture>("Tex/TestJPG");
    rq.completed += LoadOver;
    

    In addition, asynchronous loading can be used with coroutines to perform asynchronous loading of coroutines. Complex logic can be processed in coroutines, such as loading multiple resources at the same time, making it more efficient and performant.

  3. Resource unloading
    After we load a resource through Resources, the resource will be returned to the memory as a cache, so that it can be obtained directly from the memory during the second load without It needs to be reloaded again, so using Resource to load resources repeatedly will not waste memory. However, if this resource is not used later, we must uninstall this resource to avoid wasting memory and occupying memory space.

    • Uninstall the specified resource
      • Note that uninstalling the specified resources cannot uninstall GameObject type objects. What is uninstalled is content that does not need to be instantiated, such as Image, Audio, and Text.
    Resources.UnloadAsset()
    
    • Uninstall unused resources
    Resources.UnloadUnusedAssets();
    

Guess you like

Origin blog.csdn.net/m0_69778537/article/details/134561237