unity-scene-reference icon indicating copy to clipboard operation
unity-scene-reference copied to clipboard

Getting buildIndex from SceneReference

Open sirmarcomoro opened this issue 4 years ago • 4 comments
trafficstars

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 :)

sirmarcomoro avatar Feb 10 '21 10:02 sirmarcomoro

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.

JohannesMP avatar Feb 10 '21 18:02 JohannesMP

I'll be here patiently waiting then :D

sirmarcomoro avatar Feb 13 '21 17:02 sirmarcomoro

+1 for this. Build index would be amazing!

pencilking2002 avatar Apr 12 '21 15:04 pencilking2002

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);
}

bliu886 avatar Apr 05 '22 18:04 bliu886