Search Issue Tracker
By Design
Votes
0
Found in
2019.4
2020.3
2021.1.21f1
2021.2
2022.1
Issue ID
1375738
Regression
No
GC.Alloc when SpriteRenderer.get_sprite() is called
Reproduction steps:
1. Open the attached project ("GetSprite_GC_Alloc_Example.zip")
2. Open "SampleScene"
3. Open the Profiler window and enable "Deep profile"
4. Enter Play mode
5. Exit Play mode right after the sprite in Game view changes
6. Search "get_sprite" in the Profiler search, select "SpriteRenderer.get_sprite()" and clear the search
7. Move the bar through "CPU usage" and observe the "GC Alloc" column
Expected result: No GC allocation under get_sprite() at any frame
Actual result: 40 B GC allocation under get_sprite() at certain frames
Reproducible with: 2019.4.34f1, 2020.3.25f1, 2021.2.7f1, 2022.1.0b2
Note: Copying a Sprite that has been copied before will not allocate GC
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Articulation Body with 'Revolute' Joint Type has erratic behavior when Upper Limit is set to above 360
- WebGL Player fails to render Scene when Terrain with Detail Mesh is added and WebGPU Graphics API is used
- Inconsistent errors are logged when different types are passed into the Query "Q<>" method in UIToolkit and the ancestor VisualElement is null
- Crash on GetMaterialPropertyByIndex when opening a specific Scene
- Discrepancies in the styling are present when using a TSS file instead of a USS file in custom EditorWindow
Resolution Note:
This is actually by design: some of the resources handled by Unity are often typically only manipulated by native code. For those kind of resources (textures, sprites, vertex buffers, etc.), we only create a managed wrapper when some managed component explicitly want to access it.
So for a given Sprite object, the first time it is accessed (using SpriteRenderer.get_sprite() for example), the call to "Scripting::ScriptingWrapperFor" will instanciate this managed wrapper, to gives managed code a view over the object.
The wrapper is cached though, so if you try to access the same sprite from managed code repeatedly, the wrapper will only be created once.