Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Getting buildIndex from SceneReference #10

Open
sirmarcomoro opened this issue Feb 10, 2021 · 4 comments
Open

Getting buildIndex from SceneReference #10

sirmarcomoro opened this issue Feb 10, 2021 · 4 comments

Comments

@sirmarcomoro
Copy link

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

@JohannesMP
Copy link
Owner

JohannesMP commented Feb 10, 2021

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.

@sirmarcomoro
Copy link
Author

I'll be here patiently waiting then :D

@pencilking2002
Copy link

+1 for this. Build index would be amazing!

@bliu886
Copy link

bliu886 commented Apr 5, 2022

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants