Last active
April 26, 2017 21:38
-
-
Save baba-s/92f3d7536bddc251612f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using UnityEditor; | |
public static class SceneCreator | |
{ | |
[MenuItem( "Assets/Create/Empty Scene" )] | |
private static void CreateEmptyScene() | |
{ | |
CreateScene( | |
"New Empty Sene", | |
EditorApplication.NewEmptyScene | |
); | |
} | |
[MenuItem( "Assets/Create/Scene" )] | |
private static void CreateScene() | |
{ | |
CreateScene( | |
"New Sene", | |
EditorApplication.NewScene | |
); | |
} | |
private static void CreateScene( | |
string filenameWithoutExtension, | |
Action newSceneCallback | |
) | |
{ | |
var filename = filenameWithoutExtension + ".unity"; | |
var path = GetPath() + "/" + filename; | |
path = AssetDatabase.GenerateUniqueAssetPath( path ); | |
newSceneCallback(); | |
EditorApplication.SaveScene( path ); | |
var scenes = EditorBuildSettings.scenes; | |
ArrayUtility.Add( | |
ref scenes, | |
new EditorBuildSettingsScene( path, true ) | |
); | |
EditorBuildSettings.scenes = scenes; | |
} | |
private static string GetPath() | |
{ | |
var instanceId = Selection.activeInstanceID; | |
var path = AssetDatabase.GetAssetPath( instanceId ); | |
path = string.IsNullOrEmpty( path ) ? "Assets" : path; | |
if ( Directory.Exists( path ) ) | |
{ | |
return path; | |
} | |
if ( File.Exists( path ) ) | |
{ | |
var parent = Directory.GetParent( path ); | |
var fullName = parent.FullName; | |
var unixFileName = fullName.Replace( "\\", "/" ); | |
return FileUtil.GetProjectRelativePath( unixFileName ); | |
} | |
return string.Empty; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment