For the same process (enabling a UI Document and setting some values (like cleaning up some containers, removing specific Visual elements and inserting newly created Visual elements on them) I’m getting 4 different exceptions/errors.
When it happens: Normally this process works just fine both in the editor and mobile; however, recently we added a new feature that clicking on a mobile notification directs the player to a specific page (of course after async operations that take more than seconds). Clicking on a in-game button does the same but we are facing this issue only after clicking the notification; so we are suspecting that it may cause the layout calculation to be stuck at some point.
Where it happens: In the editor and some iPhone devices (We were able to test it in different iPhone 11s and 14 Pro) it was just fine, but we tested it in two Android devices (Redmi Note 12 Pro and Redmi Note 8 Pro) It has thrown the errors listed below.
Exception 1:
InvalidOperationException: VisualElements cannot be added to an active visual tree during generateVisualContent callback execution nor during visual tree rendering
UnityEngine.UIElements.UIR.RenderChain.UIEOnChildAdded (UnityEngine.UIElements.VisualElement ve)
UnityEngine.UlElements.VisualElement+Hierarchy.Insert (System.Int32 index, UnityEngine.UIElements.VisualElement child)
UnityEngine.UIElements.VisualElement.insert (System.Int32 index, UnityEngine.UIElements.VisualElement element)
Exception 2:
InvalidOperationException: Cannot modify VisualElement hierarchy during layout calculation
Error 3:
Assertion failed
Error 4 and 5 actually:
Trying to destroy object that is already released to pool
My questions:
-
I’m adding my child Visual Elements by using Insert() method in the same frame (Because I want a specific element to be the latest child). Would it be better if I just use Add() and then set the sibling index of that element afterwards? Would it make the calculation faster?
-
What if I divide the operation in different frames using coroutines? Would it solve my problem?
-
Could clicking on the notification have something to do with it? I’m curious why it does not happen normally but happens after clicking the notification.
-
Does this happen because of hardware limitations? I want to know why we could not reproduce it on iOS devices.
-
Is this a bug or a known limitation of UI Toolkit. If so, why it is not clearly stated in any documentation?
Unity Version: 2021.3.35f1
Thank you in advance for your help.