UnityTimer icon indicating copy to clipboard operation
UnityTimer copied to clipboard

Additive Scenes

Open mbbmbbmm opened this issue 8 years ago • 3 comments

Hi! I am using UnityTimer in the context of additive scenes. I have a main scene as a kind of manager scene that is never unloaded, but it cannot be the active scene most of the time. It needs to hold the TimerManager because the other scenes are all volatile. My workaround for now was to spawn a dummy timer at startup in the main scene and setting it to loop so it will always be there.

Would it be OK to make the TimerManager class public instead and add it manually from a different script in the main scene?

void Start()
{
    GameObject managerObject = new GameObject { name = "TimerManager" };
    managerObject.AddComponent<Timer.TimerManager>();
}

I made a little test and it seems to work, but I am not sure if there are going to be problems if TimerManager is public.

Thanks! Jan

mbbmbbmm avatar Feb 06 '17 12:02 mbbmbbmm

Hey Jan! This is a really good point, thanks for the issue.

My main reason for not wanting to make TimerManager public is just that I don't want people to have to worry about managing it - ideally it would take care of spawning itself when you call Timer.Register for the first time, in a way that just works, so you don't have to worry about calling Timer.Register only after the TimerManager has been created.

I'm not too experienced with additive scenes -- would it make sense for us to change the TimerManager spawning logic so it gets spawned into its own separate scene?

akbiggs avatar Feb 06 '17 17:02 akbiggs

That way the TimerManager won't be created inside one of the volatile scenes.

akbiggs avatar Feb 06 '17 17:02 akbiggs

So with additive scenes there's always one active scene, by default it is the last loaded scene. As far as I know the active scene is responsible for two things: it's the scene where GameObjects are instantiated, and it is the scene that provides the main data for the lighting.

This second responsibility is what makes it a little less easy than it probably should be :)

I haven't tried to create scenes at runtime yet, and I don't know if they get activated by default. It may be necessary to prevent that so the GI never gets switched and other instantiated GameObjects would appear in the correct scene. Also it would be important to protect the created scene from accidentally being unloaded, which may be problematic too...

I know that it's possible to instantiate GameObjects in inactive scenes by using a parent, i.e. searching the desired parent with "GameObject.Find" and then using that reference.

Maybe the easiest thing would be to have an optional script or prefab for users who use additive scenes. I think everybody who uses them is aware of the additional set up required to make them work. So e.g. if there's a GameObject called TimerManager placed by the Unity user in one of the loaded scenes use that and add the TimerManager class automatically.

mbbmbbmm avatar Feb 07 '17 07:02 mbbmbbmm