Hi everyone,
I created a custom editor in which I wanted to have alternating colors between elements, just like the console alternates the background color between multiple log entries.
I used
EditorGUILayout.BeginHorizontal()
to enclose each element, since I have multiple GUI components per element. To change the background color of BeginHorizontal, I dynamically created two textures and alternated them in the background color like this
BackgroundTextures = new Texture2D[] { new Texture2D( 1, 1 ), new Texture2D( 1, 1 ) };
BackgroundTextures[ 0 ].SetPixel( 0, 0, new Color( 0.85f, 0.85f, 0.85f ) );
BackgroundTextures[ 0 ].Apply();
BackgroundTextures[ 1 ].SetPixel( 0, 0, new Color( 0.76f, 0.76f, 0.76f ) );
BackgroundTextures[ 1 ].Apply();
for( int i = 0; i < totalElements; ++i )
{
horizontalRow.normal.background = BackgroundTextures[ elementNr % 2 ];
...
}
This works great (as opposed to GUI.color or GUI.backgroundColor which don’t change the color of a BeginHorizontal()). My only problem is once I save the scene, I get the following error:
Cleaning up leaked objects in scene since no game object, component or manager is referencing them
Texture2D has been leaked 2 times.
Which is related to the two Texture2D’s I create. But I have no idea where I have to free the textures since I am using them all the time in the editor window. As I said, the warning message occurs every time I save the scene.
Does anyone know how to fix these leaks?