Files
TaiWan/Assets/Roaming/Scripts/ClearSceneData.cs
2025-10-31 15:20:38 +08:00

70 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using UnityEngine;
using System.Collections;
using System.Runtime.CompilerServices;
using Object = UnityEngine.Object;
using UnityEngine.SceneManagement;
public class ClearSceneData : MonoBehaviour
{
//异步对象
private AsyncOperation async;
//下一个场景的名称
private static string nextSceneName;
void Awake()
{
Object[] objAry = Resources.FindObjectsOfTypeAll<Material>();
for (int i = 0; i < objAry.Length; ++i)
{
objAry[i] = null;//解除资源的引用
}
Object[] objAry2 = Resources.FindObjectsOfTypeAll<Texture>();
for (int i = 0; i < objAry2.Length; ++i)
{
objAry2[i] = null;
}
//卸载没有被引用的资源
Resources.UnloadUnusedAssets();
//立即进行垃圾回收
GC.Collect();
GC.WaitForPendingFinalizers();//挂起当前线程,直到处理终结器队列的线程清空该队列为止
GC.Collect();
}
void Start()
{
StartCoroutine("AsyncLoadScene", nextSceneName);
}
///
/// 静态方法直接切换到ClearScene此脚本是挂在ClearScene场景下的就会实例化执行资源回收
///
///
public static void LoadScene(string _nextSceneName)
{
nextSceneName = _nextSceneName;
SceneManager.LoadScene("LoadScene");
}
///
/// 异步加载下一个场景
///
IEnumerator AsyncLoadScene(string sceneName)
{
async = SceneManager.LoadSceneAsync(sceneName);
yield return async;
}
void OnDestroy()
{
async = null;
}
}