Hello. How does everyone implement transitions between different screens? For example, if there are several different screens - Menu, Settings, Main Content, etc. What techniques does everyone use? Please describe your examples.
So far, I have set it up so that each screen is placed under different objects with a UIDocument inside a single Unity scene, and I assign a sorting index of 1 to the active screen and 0 to the others. That’s how I switch between them.
That means all documents still render behind the active one!
I don’t think UI Toolkit elements have “occlusion culling” where they only render what can be seen.
Worse yet, they all process things. Like if one of these background panels is still running a coroutine or implements Update() it will keep running, and possibly trigger something later that you don’t want to be triggered.
Instead, use the good old trick:
- Loop over all UI documents and set all of them inactive
- Set the desired document active after the loop
You can also create two gameobjects in the scene:
- Active Documents (active)
- Inactive Documents (inactive)
Go over every child in Active Documents and reparent them to Inactive Documents. Then reparent the desired document(s) to be under Active Documents. The objects inherit the active state from their parent so this also marks them active and inactive accordingly.
This second style helps if you don’t want to maintain a list of documents in a MonoBehaviour, and want to rely getting documents by code only, like: inactiveDocuments.GetComponentInChildren(includeInactive: true) - this also avoids having to search the entire scene hierarchy for these types. Note the “includeInactive” flag.