Created
April 7, 2023 22:54
-
-
Save Rovsau/cd64d4227d19d5d7b6612ebf27baee74 to your computer and use it in GitHub Desktop.
Automatically set Project Root Namespace
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.IO; | |
using UnityEditor; | |
using UnityEngine; | |
namespace Rovsau.Unity.Editor.RunOnce | |
{ | |
// Sets a root namespace for the project, | |
// then self destructs. | |
// Output: | |
// RootNameSpacePrefix + ProjectFolderName | |
[InitializeOnLoad] | |
public class ProjectInitializer | |
{ | |
private const string RootNameSpacePrefix = "MyNamespace."; // <------ Edit this | |
static ProjectInitializer() | |
{ | |
Debug.Log("<ProjectInitializer>"); | |
SetRootNamespace(); | |
SelfDestruct(); | |
} | |
private static void SetRootNamespace() | |
{ | |
string project = Directory.GetCurrentDirectory(); | |
string projectName = Path.GetFileName(project).Replace(" ", ""); | |
EditorSettings.projectGenerationRootNamespace = RootNameSpacePrefix + projectName; | |
Debug.Log("Project Root Namespace: \t" + EditorSettings.projectGenerationRootNamespace); | |
} | |
private static void SelfDestruct([System.Runtime.CompilerServices.CallerFilePath] string path = "") | |
{ | |
path = Path.GetRelativePath(Directory.GetCurrentDirectory(), path); | |
Debug.Log("</ProjectInitializer self destructed>"); | |
AssetDatabase.DeleteAsset(path); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment