I have a project where i need to setup my scene in edit mode but also support data setups at run time.
For example:
I place an object in the scene it needs to subscribe to a manager class, this manager class adds it to a grid graph. I do this currently by using OnEnable with [ExecuteAlways] to subscribe to the manager. The problem is, my monobehaviour is not technically setup until i call an Init function which sets the bounds of the object and a bunch of other stuff. The manager needs to know the bounds to add to the grid graph so the logic ends up like this:
Instantiate Object (or add to the scene in edit mode) - > OnEnable → Register to Manager
Then Init() is called either from run time or the data is set from inspector during edit mode and i use OnValidate to set bounds which ever is more appropriate.
But by that point it’s too late the manager has already added it to the grid with a default bounds of zero.
So the full order of operations currently is:
Edit mode [ExecuteAlways]:
Add Prefab → OnEnable → Register To Manager → Has bounds 0 since its not yet set in inspector.
Play mode:
Instantiate → OnEnable → Register To Manager → Has bounds 0 since Init() function has not yet been called → Init(bounds,etc etc)
Either way this current setup just isn’t going to work.
Here is my annoying dilemma:
If i don’t register to manager via OnEnable and instead do it in the call of Init() function like some of you might suggest, then, when building the scene in edit mode, i have to manually register every object i place into the scene to the manager via some custom editor with a button like “Register To Manager” which calls the Init function once i am certain the data is correctly setup.
This is rather unmanageable at scale when i have lots of objects and lots of scenes with lots of objects. Its fine for run time but when building scenes this is not ideal and its easy to miss one.
I use the manager (which is a scriptable object) to check if an object in the scene overlaps another object (among other useful functions) so it’s important that both run time and edit mode data is correctly set so the scene data is setup properly.
What is a smarter way to do this, im sure theres many different approaches here. But wondering what some more experienced users would do to solve this?