public static void OnWillCreateAsset(string path) { if (!path.Contains("Assets/Scripts/")) { return; } FilePathProcessor.ComplexFilePaths paths = FilePathProcessor.CheckAndGetPath(path); if (paths != null) { NamespaceCorrector.CorrectNamespace(paths); } }
public static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { for (int i = 0; i < movedFromAssetPaths.Length; i++) { if (!movedAssets[i].Contains("Assets/Scripts/")) { continue; } FilePathProcessor.ComplexFilePaths paths = FilePathProcessor.CheckAndGetPath(movedAssets[i]); if (paths != null) { NamespaceCorrector.CorrectNamespace(paths); } } }
public static void CorrectNamespace(FilePathProcessor.ComplexFilePaths paths) { string file = System.IO.File.ReadAllText(paths.full_path); if (file.IndexOf("namespace", StringComparison.Ordinal) > -1) { int start_of_namespace = file.IndexOf("namespace", StringComparison.Ordinal); int end_of_namespace = file.IndexOf("{", StringComparison.Ordinal); string old_namespace = file.Substring(start_of_namespace, end_of_namespace - start_of_namespace); file = file.Replace(old_namespace, "namespace " + GetNamespaceForPath(paths.origin_path).ToLower() + " "); } else { string[] lines = file.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); string ready = ""; for (int i = 0; i < lines.Length; i++) { if (i < 3) { ready += (i == 0 ? "" : Environment.NewLine) + lines[i]; } else { ready += Environment.NewLine + "\t" + lines[i]; } if (i == 3) { ready += Environment.NewLine + "namespace " + GetNamespaceForPath(paths.origin_path) + " {" + System.Environment.NewLine; } } ready += System.Environment.NewLine + "}"; file = ready; } System.IO.File.WriteAllText(paths.full_path, file); AssetDatabase.Refresh(); }