Strange and heavy calls on every Material creation.

Hi everyone!

I want to create materials at runtime.
In Unity we have no other choice but to use the Material constructor passing Shader into it.

new Material(_shader);

But when I look at profiler I see a strange call at the Material constructor:


As you can see on the screenshot “MaterialEditor.ApplyMaterialPropertyDrawers” allocates 0.6KB per material creation.

I need to create materials quite often so I really want to remove this allocation.
I don’t understand why Material constructor calls Editor methods, that’s strange.
Moreover, I never look at material’s inspector, cuz these materials are hidden.
I also tried new Material(templateMaterial); which gives the same result.

I didn’t test this in the build just yet. Anyway, this is strange and not expected behavior.

Probably worth doing – the editor makes many allocations that aren’t present in the build. This looks like one of those.

1 Like

Yes, you are right. As expected, this does not happen in build.

Anyway, this looks like a bug and there is no confirmation out there that says that this behavior is intentional.

Of course, I would understand, if such call would happen only once(on first material creation), or when Material’s inspector is shown. For now, I consider this a bug.

I believe this is By Design but for profiling purposes should be wrapped under an EditorOnly marker. Could you please report that as a bug, I.e. that it isn’t hidden under an EditorOnly marker?

2 Likes