So currently I’m writing some code to deal with the SceneManager… the SceneManager doesn’t have a ‘before scene is loaded’ event, so I basically am writing my own wrapper for that so that I can hook my own event in, and any time I use this wrapper to load a scene (instead of SceneManager) I’ll get the event as needed.
Here’s the thing.
When writing the interface for it I instinctively write it in this format:
public Scene LoadScene(string sceneName)
And that’s when I notice something… LoadScene doesn’t return anything!
Ok… weird. Because we have all these other methods on SceneManager where one could pass in a Scene object to do stuff. How am I supposed to get the reference to the scene? This is starting to stink like coroutine in older versions of Unity… they’re going to want me to use strings, but then the object identity is uncertain… I’ll get to that.
Anyways, so I’m like. All right, I’ll use this ‘GetSceneByName’:
Huh? What does that mean? Does that mean it’s included in the build settings? Or that it’s been… ‘added’… to the SceneManager? Like it’s been previously loaded? So it only works if I’ve loaded it?
Lets look at the rest of the documentation, maybe that’ll help resolve things!
Hrmm… here’s a ‘GetSceneByBuildIndex’…
Hrmmm… ok, this implies that it can’t just be in the build settings, it must also have been loaded! Cause if you must load it first to validly find a scene by build index… you can’t find a scene by name either just because it’s the buildSettings. I mean… why wouldn’t you be able to by build index if you could by name? So if you can’t by build index, you logically can’t by name?
I’m still not certain though, lets run a test where I try to ‘GetSceneByName’ when that scene has yet to be loaded…
var scene = SceneManager.GetSceneByName("SomeSceneNotLoadedYet");
Hrmmm… it returns a valid scene!
Is this because I’m in the editor?
Ughhhh… am I going to have to build a test project to test this code?
Why am I having to do this?
[edit]
Just noticed that the Scene object is actually a struct… so that’s why it appeared valid. It’s not though… so ok, that explains if I want to get the Scene token, the scene has to be loaded. The rest of my post still stands.
[/edit]
Unity… I thought you were getting your shiznit together! I thought you were finally building more appropriate APIs. I was so happy when you created the ‘Coroutine’ class giving us object identity for that, so that way you can actually stop a specific Coroutine… rather than magic strings. And when I saw a while back that you released this new SceneManager (I know… I’m a little to this game, I just hadn’t needed it as of late to do anything complex until very recently). But yeah… I was super excited to see you also gave Scenes some object identity instead of just string names (well not fully object identity since it’s a struct, but better… definitely better).
But this brings me to an issue.
So one of the reasons I really like the ‘Coroutine’ object y’all added way back when. It’s because if I started 2 coroutines from the same method, I could stop one, and know which one I was actually stopping. Cause when using the strings… who knows what happens!?
So… with this Scene object. My question is… lets say I load the same scene twice… additively. Is this even possible? If it is, how do I get a reference to the ‘Scene’ token for each unique scene?
Why the heck doesn’t LoadScene return the Scene token!?
This just boggles my freaking mind!
…
Sorry, had to vent.
Just sometimes I wonder about the API design choices unity makes.
Like I’m not arguing I’m the best at creating an API either… but jeez guys, way to half heartedly approach this thing. You have ways to Unload scenes by a Scene token… but no way to consistently get that Token! Nor document the behaviour of it!