I’m making a 2D cartoon. Each scene of the cartoon is a timeline and all the scenes are included in a master timeline control track. (alongside other tracks mainly audio track)
I’m using FindObjectsOfType to assign MaterialPropertyBlock to every sprite rendered in the scene to override the _Color and _MainTex to customize the cartoon taking into account user generated assets.
If I play a specific scene on its own (not using the master timeline control tracks), everything is working as expected. The sprites have the texture and color that are parts of the MaterialPropertyBlock.
But if I play the same scene but controlled by the master timeline control track, only the _Color parameter of the MaterialPropertyBlock is working, the texture of the sprite remain the original texture, ignoring the one I set with the block SetTexture(“_MainTex”, …)
I have read in another topic that Timeline uses MaterialPropertyBlock internally to animate some properties of the gameobjects. Maybe my problem is linked to some intricacies of Timeline I’m not aware of.
Does my situation sounds like a bug? If not, how can I achieve what I’m aiming at?
If you animate anything as part of the Renderer, the Animation system allocates a MaterialPropertyBlock in order to override the properties.
So if you want to set them yourself, you should acquire it from the renderer if it already exists.
Also, if you animate these properties anywhere, they will be set every frame.
You can still override them through code if you want to, but you’ll need to set it every frame in LateUpdate
After further tests, it seems my problem is not linked to the Timeline nesting. The nesting only pointed the problem thanks to the “Control Activation” in the Control playable asset section. The master timeline was activating and deactivating the objects of the nested timeline. And that’s the behavior I need.
But it revealed that any Renderer that has been assigned a MaterialPropertyBlock with a custom _Color and _MainTex seems to lose the _MainTex parameter after the GameObject is disabled then enabled again. Only the _Color remains.
So this may not be the right forum section to ask that anymore but why is the MaterialPropertyBlock losing the texture I set with the MaterialPropertyBlock.SetTexture (and reverting to the default texture) when I disable/enable the GameObject?
Deleting dynamically allocated resources when the object is disabled is a fairly common pattern in other Unity Components to try and keep the runtime memory usage to a minimum.