Skip to content

Instantly share code, notes, and snippets.

@lynxelia
Last active April 21, 2022 10:28
Show Gist options
  • Save lynxelia/b1a5680ccdac48d822c0d2844fc555a1 to your computer and use it in GitHub Desktop.
Save lynxelia/b1a5680ccdac48d822c0d2844fc555a1 to your computer and use it in GitHub Desktop.
A Unity3d editor script that automatically forces garbage collection when opening a scene
using System;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
[InitializeOnLoad]
public static class EditorSceneMemoryManager
{
static EditorSceneMemoryManager()
{
EditorSceneManager.sceneOpened += OnSceneOpened;
}
static void OnSceneOpened(Scene scene, OpenSceneMode mode)
{
GarbageCollect();
}
[MenuItem("Tools/Force Garbage Collection")]
static void GarbageCollect()
{
EditorUtility.UnloadUnusedAssetsImmediate();
GC.Collect();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment