Getting buildIndex from SceneReference
sirmarcomoro opened this issue · 4 comments
Heya, love the code, but I'm finding I'd have an easier time implementing it in my projects if there was a way to get a reference to the buildIndex of the SceneReference instance I am dealing with :)
Currently the code is written with the assumption for you to use the ScenePath
string. The reason for this is that UnityEditor.SceneAsset
only exposes a path so initially we didn't do that extra work. I definitively see how being able to reference the index would be useful, and really the UnityEngine.Scene
object as a whole.
Since the index can change we'd want to hold onto the UnityEngine.Scene
object regardless, which cannot be serialized, so in OnDeserialize
we could use the path to get the Scene object and hold onto a reference to that.
Would require a bit of moving stuff around so that it happens as part of the EditorApplication.update
call to HandleAfterDeserialize
when in editor (since that can modify the path itself), but should be doable.
I'll be here patiently waiting then :D
+1 for this. Build index would be amazing!
You can get the build index using SceneUtility.GetBuildIndexByScenePath. This works without needing to reference UnityEditor, so you can use this in-game.
public SceneReference scene;
public void MyMethod()
{
// buildIndex = -1 when scene is not included or is disabled in the build settings.
int buildIndex = SceneUtility.GetBuildIndexByScenePath(scene.ScenePath);
}