Skip to content

Instantly share code, notes, and snippets.

@Rovsau
Created April 7, 2023 22:54
Show Gist options
  • Save Rovsau/cd64d4227d19d5d7b6612ebf27baee74 to your computer and use it in GitHub Desktop.
Save Rovsau/cd64d4227d19d5d7b6612ebf27baee74 to your computer and use it in GitHub Desktop.
Automatically set Project Root Namespace
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