使用WWW下载资源时,下载的资源会加载到内存中,而当资源不被使用时,他不会被回收掉。使用www.Dispose并不能清除。
1.使用destroy方法,将资源放在一个全局变量中,替换资源时 手动清除
2.使用Resources.UnloadUnusedAssets();清楚所有未使用的资源
3.切换场景的方式让Unity自动清除所有未使用的资源
公司做bgm播放踩得一个坑:
IEnumerator LoadBgm(WallVideoConfigData WCdata)
{
BgmConfigData data = GlobalManager.Instance.bgmData[WCdata.bgmId];
string path = “file://”;
path += Application.streamingAssetsPath + “/” + data.bgmPath;
if (File.Exists(Application.streamingAssetsPath + “/” + data.bgmPath))
{
if (path.EndsWith(".wav") || path.EndsWith(".ogg"))
{
WWW www = new WWW(path);//未释放导致内存溢出!!!
yield return www;
try
{
Destroy(download);//尝试中–Destroy那个效率高,另外是否有其他方法销毁这个 下载进来的 clip
if (www.isDone && www != null)
{
download