Exemple #1
0
        public static void OnWillCreateAsset(string path)
        {
            if (!UnityExtensionsSettings.instance.cSharpSettings.addNamespaceToCSharpFiles)
            {
                return;
            }
            path = AssetDatabase.GetAssetPathFromTextMetaFilePath(path);
            var file = new FileInfo(path);

            if (file.Extension == CSHARP_EXTENSION)
            {
                var script   = AssetDatabase.LoadAssetAtPath <MonoScript>(path);
                var assembly = PrefabUtils.GetAssembly(script, path);
                if (assembly)
                {
                    string contents = File.ReadAllText(file.FullName);
                    if (!contents.Contains("namespace ") && contents.Contains("public class "))
                    {
                        contents = contents.Replace("public class ", $"namespace {PrefabUtils.GetNamespace(assembly, path)} {{\r\npublic class ") + "}";
                        File.WriteAllText(file.FullName, contents);
                        AssetDatabase.Refresh();
                    }
                }
            }
        }
Exemple #2
0
        public static AssemblyDefinitionAsset GetAssembly(MonoScript script, string assetPath = null)
        {
            var directory = new FileInfo(assetPath ?? AssetDatabase.GetAssetPath(script)).Directory;

            return(PrefabUtils.LoadAssets <AssemblyDefinitionAsset>()
                   .Select(assembly => new { assembly, directory = new FileInfo(AssetDatabase.GetAssetPath(assembly)).Directory })
                   .Where(info => directory.FullName.Contains(info.directory.FullName))
                   .OrderByDescending(info => info.directory.FullName.Length)
                   .Select(info => info.assembly)
                   .FirstOrDefault());
        }