public static bool TryFindScriptForDataContainer(Type dataType, out MonoScript script) { if (TryFindDataContainerType(dataType, out Type containerType)) { return(EditorAssetUtilities.TryFindScript(containerType, out script)); } script = null; return(false); }
private static void GenerateForDataTypes(Type[] dataTypes) { var @namespace = $"{typeof(DataContainer).Namespace}.GeneratedClasses"; var codeFileInfos = GenerateCodeFiles(@namespace, GenerateTypeDeclarations(dataTypes)); var directory = new DirectoryInfo(Path.Combine(Application.dataPath, "Generated", "JakePerry", "ScriptableData")); directory.Create(); // Get reference to the project folder (one up from Assets). var projectDirFullName = directory.Parent.Parent.Parent.Parent.FullName; int trimCount = projectDirFullName.Length + 1; if (projectDirFullName.EndsWith("/") || projectDirFullName.EndsWith("\\")) { trimCount++; } AssetDatabase.DisallowAutoRefresh(); try { int debugCount = 0; foreach (var codeFileInfo in codeFileInfos) { var file = WriteCodeFile(codeFileInfo, directory); var fileFullName = file.FullName; Debug.Assert(fileFullName.StartsWith(projectDirFullName)); var projectRelativePath = fileFullName.Substring(trimCount).Replace('\\', '/'); AssetDatabase.ImportAsset(projectRelativePath); var monoScript = AssetDatabase.LoadAssetAtPath <MonoScript>(projectRelativePath); if (monoScript == null) { Logger.LogError($"Failed to find generated file at path {projectRelativePath}"); continue; } EditorAssetUtilities.AddLabel(monoScript, GENERATED_SCRIPT_LABEL); debugCount++; } AssetDatabase.Refresh(); if (debugCount == codeFileInfos.Length) { Logger.Log($"Successfully generated {debugCount.ToString()} code files."); } else { Logger.Log($"{debugCount.ToString()} of {codeFileInfos.Length.ToString()} files were successfully generated."); } } finally { AssetDatabase.AllowAutoRefresh(); } }