Unity framework learning _ResManager, efficiently manage the loading and unloading of resources

         Unity framework learning _ResManager, efficiently manage the loading and unloading of resources


table of Contents

1. Blog introduction

2. Content

(1) Guidance

(2) Main logic

3. Push

4. Conclusion


1. Blog introduction

       This blog introduces the dynamic loading and unloading of Unity3D resources, and how to manage them efficiently. The idea of ​​this article is completely derived from the method of loading resources in the QFramework of the Unity3D framework of sandals. The blogger just added some content by applying the ideas, including the Resources method to load resources and the loading of AssetBundle resources.


2. Content

(1) Guidance

Resourceshttps://blog.csdn.net/Mr_Sun88/article/details/94147069

AssetBundlehttps://blog.csdn.net/Mr_Sun88/article/details/86553465

If you are not familiar with the Resources method and the loading of the AssetBundle package, you can jump directly to the other two bloggers' articles

(2) Main logic

Resource type tag: ResType

public enum ResType
{
   ResSingle,//Res单个资源
   ResGroup,//Res组资源
   AssetBundle,//AB包资源
}

ResType enumeration type, marking the type of the currently loaded resource

Abstract resource script: Res.cs

/// <summary>
/// 资源容器成员属性和方法
/// </summary>
public interface IRes
{
	ResType AssetType { get; set; }//资源类型标记
	string AssetKey { get; set; }//资源标记
	Object Asset { get; set; }//单体资源
	Object[] Assets { get; set; }//资源组
	int ReferenceCount { get; set; }//引用计数
	void Reference();//引用执行
	void Release();//释放执行
	void Destroy();//摧毁执行
}

       We abstract each resource that needs to be loaded into a Res that inherits IRes, and each Res has the above attributes and methods.

AssetType Records the type of the current resource, whether it is a single resource, a resource group, or an AB package
AssetKey Use the resource path as the key value when the resource is loaded
Asset If it is a single resource or AB package, assign the resource to Asset when constructing Res
Assets If it is a resource group, assign the resource group to Assets when constructing Res
ReferenceCount Record the number of times the resource has been cited
Reference() Called when the resource is referenced, ReferenceCount++
Release() Called when the resource is released, if there is a reference to the resource, then ReferenceCount--, if there is no reference, the resource is released
Destroy() Regardless of the resource reference, directly release

Public resource container: PublicLoadingResList

        /// <summary>
	/// 公共容器 储存所有动态资源
	/// </summary>
	public static List<Res> PublicLoadingResList = new List<Res>();

All resources dynamically loaded by the loader are stored in the public container PublicLoadingResList

Resource Loader: ResLoader.cs

/// <summary>
/// 资源的加载容器属性和方法
/// </summary>
public interface IResLoader
{
	List<Res> SelfLoadingResList { get; set; } //储存当前容器加载的资源
	T AssetLoad<T>(string assetPath) where T : Object; //Resources单个资源的同步加载
	T[] AssetsLoad<T>(string folderPaht) where T : Object; //Resources目录下全部资源加载
	IEnumerator AssetLoadAsync<T>(string assetPath, Action<T> callback) where T : Object; //异步加载Resources下的单个资源
	AssetBundle ABLoadFromMemory(string assetPath); //直接加载AB包
	IEnumerator ABLoadFromMemoryAsync(string assetPath, Action<AssetBundle> callback); //LoadFromMemoryAsync 异步加载AB包
	AssetBundle ABLoadFromFile(string assetPath); //LoadFromFile直接加载本地AB包
	IEnumerator ABLoadFromFileAsync(string assetPath, Action<AssetBundle> callback); //从本地异步加载AB资源
	IEnumerator ABLoadWWW(string assetPath, Action<AssetBundle> callback); //WWW放置加载AB包
	IEnumerator ABLoadWebRequest(string assetPath, Action<AssetBundle> callback); //WebRequest请求AB包
	void UnloadAll();//卸载当前加载器资源

}
SelfLoadingResList Store resources dynamically loaded by the current loader
 AssetLoad<T>(string assetPath) Load path resources in the Resources directory
AssetsLoad<T>(string folderPaht) Load all resources under a file in the Resources directory
AssetLoadAsync<T>(string assetPath, Action<T>callback)  Asynchronously load a single resource in the Resources directory
 ABLoadFromMemory(string assetPath) LoadFromMemory method to load AB package resources
ABLoadFromMemoryAsync(string assetPath, Action<AssetBundle> callback) LoadFromMemoryAsync method asynchronously loads AB package resources
 ABLoadFromFile(string assetPath) LoadFromFile method to load AB package resources
 ABLoadFromFileAsync(string assetPath, Action<AssetBundle> callback) The LoadFromFileAsync method asynchronously records AB package resources
ABLoadWWW(string assetPath, Action<AssetBundle> callback) WWW method asynchronously loads AB package resources
 ABLoadWebRequest(string assetPath, Action<AssetBundle> callback) UnityWebRequestAssetBundle method asynchronously loads AB package resources
UnloadAll() Unload the current loader resources

       When resources need to be dynamically loaded, we instantiate a ResLoader, and use the loader to load all the resources needed for the current page. When we load the resources, we will judge according to the AssetKey of the resource, whether the resource exists in the loader container or whether it exists In the public container, if it exists, it will return directly without reloading. If it does not exist, the resource will be loaded and added to the container. This can avoid repeated loading of the resource. We can directly call the current page's loader method UnloadAll when the current page is destroyed. () to release the loaded resources, different loaders for different page instances, to facilitate resource management.

Resource loader management:

/// <summary>
/// 资源管理属性方法
/// </summary>
public interface IResManager
{
	ResLoader GetResLoader();//获取资源加载器实例
}

 

GetResLoader() Get an instance of the resource loader ResLoader

 The current manager has only one method to obtain an instance of the loader, and we will continue to add it in the future


3. Push

Engineering demo Github:  https://github.com/KingSun5/Study_ResManager

凉鞋Chat:https://gitbook.cn/books/5b36e7d3dc52aa6b10c03a29/index.html

QFramework: https://gitbook.cn/books/5b36e7d3dc52aa6b10c03a29/index.html

 


4. Conclusion

       Because of the large amount of content, all the main implementation logic codes are not released by the blogger. They are all in the blogger’s Github demo project. Students in need can jump directly to the blogger’s Github. If you feel that the blogger’s article is written It's not bad, you might as well pay attention to the blogger and like the blog post. In addition, the ability of the blogger is limited. If there is any error in the post, please comment and criticize.

       QQ exchange group: 806091680 (Chinar)

       This group was created by CSDN blogger Chinar, recommend it! I am also in the group!

       This article is an original article, please reprint the source of the famous author and stick to the top! ! ! !

 

Guess you like

Origin blog.csdn.net/Mr_Sun88/article/details/94357068