- Home /
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.
Answer by zangad · Jan 08, 2015 at 09:45 PM
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[i] = pathParts[i];
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();
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Invalid Encoding Specification Xml 1 Answer
How To Switch A XmlNodeList To A String 1 Answer
Read AND Write to XML at runtime 1 Answer