Manipulate EditorApplication.currentScene in a generic script

I’ve got a generic script to manage various scenes, and an xml file to hold the specific details of each scene.
I’m trying to keep it all organised:

\Project
- \Assets
-- \MyScene
-- scene.unity
--- \xml
--- sceneData.xml
- \Scipts
-- generic.cs

I’d now like to manipulate EditorApplication.currentScene to give me the folder for the scene because within each there is then an xml folder & the necessary xml file (as I’ve tried to show above).

Using currentScene I can get the string for my scene - C:\Users\markw\Documents\Project\Assets\MyScene\scene.unity

So how can I strip the scene file name (in C#)? I’ve not had to manipulate strings yet.

thanks.

1 Answer

1

Put this C# script somewhere in your solution:

public static class StringExtensions
    {
        public static string SceneFolder(this string scenePath)
        {
            string[] pathParts = scenePath.Split('/');
            
            if (pathParts.Length > 1)
            {
                string[] folderPath = new string[pathParts.Length - 1];
                
                for (int i = 0; i < pathParts.Length - 1; i++)
                    folderPath _= pathParts*;*_

return String.Join(“/”, folderPath);
}

return scenePath;
}
}
It adds an extension method that will allow you to extract out the folder part of a Unity scene path.
Then, you can get the scene folder like this:
string myScenePath = EditorApplication.currentScene.SceneFolder();