x


Leaking Textures in custom editor

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?

more ▼

asked Jun 23 '11 at 01:35 AM

Oliver Eberlei gravatar image

Oliver Eberlei
366 2 3 8

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Did you set the HideFlags ? Beside that i can't say much about where you've done something wrong since you only showed the creating part.

more ▼

answered Jun 23 '11 at 02:53 AM

Bunny83 gravatar image

Bunny83
45.4k 11 49 207

That did the trick! Thank you. I was not aware you could set the HideFlags for textures as well.

Jun 23 '11 at 07:32 PM Oliver Eberlei

The hideflags are part of UnityEngine.Object. All Unity objects are derived from it.

Jun 23 '11 at 07:35 PM Bunny83

Cheers, worked great!

Apr 30 '12 at 01:24 AM numberkruncher
(comments are locked)
10|3000 characters needed characters left

Why not destroy the textures as soon as you exit the for loop? It doesn't look like they are needed anymore. Your code is going to re-create them every time that function is called.

It seems to me that it would make more sense to create your textures outside your code and assign them in the inspector. Is there some reason you want to create new textures from scratch every frame?

Personally I would just use two different GUIStyles and alternate each time through the loop:

/* an array of GUI Styles that can be set in the inspector */

var rowStyles  :  GUIStyle [2];

function OnGUI ()
{
    for ( var i = 0; i < totalElements; i ++ ) {
       GUI.Label(someRectangle, someLabel, rowStyles[i % 2]);
    }
}
more ▼

answered Jun 23 '11 at 04:18 AM

jahroy gravatar image

jahroy
3.2k 14 18 41

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2206
x1675
x32
x3

asked: Jun 23 '11 at 01:35 AM

Seen: 1571 times

Last Updated: Apr 30 '12 at 03:46 AM