接着上一篇,我们继续测试Resources.Load 资源加载,然后再销毁与卸载,观察对应的内存变化。
先说结论:
1,不触发时,不占用什么资源(请对比上一篇prefab模式,那个要占, 其实就相当于Resources.Load() )
2,Resources.Load() 的时候,会载入Mesh。
3,Instantiate()生成游戏体的时候,会载入对应的Texture,占用内存。
4,Destroy() 会马上释放小部分显存。Unity等待GC时机*(见下面注释1),再释放部分mesh和texture资源。但是仍不彻底。
5,Resources.UnloadUnusedAssets() 可以清理内存,会把没有使用(或者资源=null)的资源清理掉。注意,官方建议在允许卡顿的情况下调用(比如loading界面),因为这是一个非常缓慢的操作。
注释1: 这里有一个关于内存管理的简单理解:Resources会把资源A/B/C打进一个bundle,你可以部分加载(比如A),或者全部加载(ABC),但不能部分卸载。在bundle本身完全卸载之前,不会卸载任何东西.。除非你调用了Resources.UnloadUnusedAssets()。
下面是试验过程,可以不看。
先获得一个空场景运行的数据,作为参照
在摄像机上,挂上如下脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 设计编写:常成功
/// 创建时间:2020/05/08
/// 脚本功能:测试动态加载与卸载, 基于Resource.
/// 挂载位置:默认场景的摄像机上
/// </summary>
public class Test_Mem_Resource : MonoBehaviour
{
// 记录load的资源
public List<GameObject> load_res_list;
// 记录实例化的游戏体
public List<GameObject> obj_list;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
Debug.Log("Num 1:");
GameObject obj_res = Resources.Load<GameObject>("Test/atc_geye@idle");
GameObject the_obj = Instantiate(obj_res);
the_obj.transform.position = new Vector3(-2,0,0);
load_res_list.Add(obj_res);
obj_list.Add(the_obj);
// 局部变量, 可以手动显式置null, 也可以不管, 因为已经没有引用了
//obj_res = null;
//the_obj = null;
}
if (Input.GetKeyDown(KeyCode.Alpha2))
{
Debug.Log("Num 2:");
GameObject obj_res = Resources.Load<GameObject>("Test/atc_niya@idle");
GameObject the_obj = Instantiate(obj_res);
load_res_list.Add(obj_res);
obj_list.Add(the_obj);
}
if (Input.GetKeyDown(KeyCode.Alpha3))
{
Debug.Log("Num 3:");
GameObject obj_res = Resources.Load<GameObject>("Test/Car_2C 1");
GameObject the_obj = Instantiate(obj_res);
the_obj.transform.position = new Vector3(2, 0, 0);
load_res_list.Add(obj_res);
obj_list.Add(the_obj);
}
// Destroy() 游戏体
if (Input.GetMouseButtonDown(1))
{
int tail_index = obj_list.Count -1;
Destroy(obj_list[tail_index]);
obj_list.RemoveAt(tail_index);
Debug.Log("Mouse R:" + tail_index.ToString());
}
// 释放已经没有引用的资源, 包括Resources.Load()的资源
if (Input.GetKeyDown(KeyCode.D))
{
load_res_list = null;
obj_list = null;
Debug.Log("Key D:");
Resources.UnloadUnusedAssets();
}
}
}
重启编辑器,冷运行程序:
Load() 并且初始化游戏体1次
Load() 并且初始化游戏体2次
Load() 并且初始化游戏体3次
Destroy() 游戏体1次
Destroy() 游戏体2次
Destroy() 游戏体3次
资源引用置null,再调用Resources.UnloadUnusedAssets()
PS:稍微把代码改一下,只Resources.Load(),不Instantiate(),我们看看什么样:
1
2
3