I'm creating a custom editor in a c# dll and loading textures embedded as resources in the dll. It all works fine, but when I save the scene, unity says its cleaning up leaked textures.
I'm disposing of the textures in EditorWindow.OnDisable(), and if I close the editor before saving there are no leaks. So I can't figure out how to avoid leaking textures.
Here's how I load the embedded resource:
static List<Texture2D> LoadedTextures = new List<Texture2D>();
public static Texture2D LoadDllResource(string resourceName, int width, int height)
{
texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
var myAssembly = Assembly.GetExecutingAssembly();
var myStream = myAssembly.GetManifestResourceStream("Editor.Resources." + resourceName + ".png");
if (myStream != null)
{
texture.LoadImage(ReadToEnd(myStream));
myStream.Close();
}
else
{
Debug.LogError("Missing Dll resource: " + resourceName);
}
LoadedTextures.Add(texture);
return texture;
}
I load textures into static variables. For example:
browserButton = new GUIContent(MyEditorUtility.LoadDllResource("browserIcon", 14, 14), "Open Browser");
Then in EditorWindow.OnDisable() I destroy the textures:
public static void DestroyLoadedTextures()
{
foreach (var texture in LoadedTextures)
{
Object.DestroyImmediate(texture);
}
LoadedTextures.Clear();
}
Again, if I close the window then save, no leaks. And if I don't destroy them, they leak. So it feels like I'm doing that part correctly.
But if I save with the editor window open, every texture I loaded from the dll leaks... is there some other window event I should be destroying loaded textures on?
Also Unity continues to report that it's cleaning up textures on every save, even though I haven't loaded any more textures (I do all loading in one place). So it seems like they're not really getting cleaned up...?