This has started to become a real pain point, I’m curious if anyone has found some useful patterns that help out.
It’s not that bad until you start to factor in editor tooling and things that have to be running at design time, but are inherently scene scoped. Like instanced rendering for example. Then throw on top production vs dev flows that are different in terms of scene load order.
Most of the challenges seem to all go back to MB land has to control setup/shutdown of ECS. We have hooks like RuntimeInitializeOnLoadMethod but that’s for play mode. You have to solve for editor mode also. You can leverage SO’s sort of. Use them to create editor buttons to manually start ECS. Once it’s started you can then leverage EditorApplication.playModeStateChanged to manage stuff from there.
MB’s with ExecuteAlways sort of works also, it has it’s own set of gotchas. Like OnEnable fires when it shouldn’t, so special logic to contain that. And then in play mode you are back to well we could use better patterns if it wasn’t for editor mode.
And round in circles I go.
I could probably leverage SO’s and EditorApplication.playModeStateChanged to set up and manage state for the life of Unity, with a one time action on entering Unity to kick it all off. But I’m hesitant to go building something like that out without confirming there isn’t a better approach.
MB’s themselves are predictable enough, there are fairly consistent rules there and you have script execution order.
A common paradigm we have is systems that are global in the sense that they are always needed by the active scene, but with scene state/events. So best would be ECS starts up before any scene loading/MB events hit. And ECS is where the bulk of the logic lives. With MB’s mostly used for events or scene specific configuration params. At the same time some of that is our custom instanced rendering, like terrain, vegetation, etc… So it all has to be running in editor mode also.
Having MB’s start ECS is problematic, you need a callback loop for that to give ECS a frame to startup if you want everything to always just work. Unity themselves has first frame callbacks, hidden gameobjects, they obviously haven’t really solved this well either.
if I could find a more direct api to control ECS/MB flow that would be ideal. I’m assuming no such beast exists as pre ECS no reason for it. But was hoping maybe something new was out I hadn’t seen.
Can’t say I’ve exactly run into this problem. We never change systems and just create them all with the World before entering the game scene. Also all our MB in game are instantiated from systems.
(In this particular project we destroy the World when returning to menu and create it again each time when entering game but it is highly likely next project we will never destroy them.)
The biggest issue I have with our MB is legacy code from before I was here which has them referencing entities requiring all sorts of life checks.
-edit-
i should add though, I do agree with you timings are a bit of a nightmare when it comes to editor, playmode and builds… they’re all different.
The bulk of the pain goes directly back to custom rendering mostly. We need the terrain and vegetation visible it just has to be there to iterate on some things at design time. And that forces making it work in a single scene context in addition to the normal play flow.
I suspect what you are looking for is the ability to preview the ECS world (rendering) while outside of play mode. For that, you might want to specify a system to [ExecuteAlways] and set up Live Link to preview entities. The hooks do exist for this use case. But there is zero documentation on the topic.
ExecuteAlways doesn’t help out with startup/shutdown timing or flow.
Unity themselves resort to hidden gameobjects and first frame callbacks and such to make similar scenarios work. I’ve had to resort to similar patterns but I’m trying to find a cleaner approach because it’s just sort of a mess really. And I didn’t really expect to find a better solution as I’ve gone fairly deep here and turned up nothing, but was hoping well maybe someone has a clever idea.