public static void GenerateStaticCollectionScript(ScriptableObjectCollection collection) { string fileName = $"{collection.GetCollectionType().Name}Static"; bool isGeneratingCustomStaticFile = ScriptableObjectCollectionSettings.Instance.IsGeneratingCustomStaticFile(collection); if (isGeneratingCustomStaticFile) { fileName = ScriptableObjectCollectionSettings.Instance.GetGeneratedStaticFileName(collection); } string nameSpace = collection.GetCollectionType().Namespace; string finalFolder = ScriptableObjectCollectionSettings.Instance.GetStaticFileFolderForCollection(collection); if (string.IsNullOrEmpty(finalFolder)) { Debug.LogError("Static Code Generation folder not assigned, please assign it on the ScriptableObjectCollectionSettings"); EditorGUIUtility.PingObject(ScriptableObjectCollectionSettings.Instance); Selection.objects = new Object[] { ScriptableObjectCollectionSettings.Instance }; return; } AssetDatabaseUtils.CreatePathIfDontExist(finalFolder); using (StreamWriter writer = new StreamWriter(Path.Combine(finalFolder, $"{fileName}.cs"))) { int indentation = 0; List <string> directives = new List <string>(); directives.Add(typeof(CollectionsRegistry).Namespace); directives.Add(collection.GetType().Namespace); directives.AddRange(GetCollectionDirectives(collection)); if (!isGeneratingCustomStaticFile) { AppendHeader(writer, ref indentation, nameSpace, "", collection.GetCollectionType().Name, true, false, directives.Distinct().ToArray()); } else { AppendHeader(writer, ref indentation, nameSpace, "", fileName, false, false, directives.Distinct().ToArray()); } GeneratedStaticFileType staticFileTypeForCollection = ScriptableObjectCollectionSettings.Instance.GetStaticFileTypeForCollection(collection); if (staticFileTypeForCollection == GeneratedStaticFileType.DirectAccess) { WriteDirectAccessCollectionStatic(collection, writer, ref indentation); } else { WriteTryGetAccessCollectionStatic(collection, writer, ref indentation); } indentation--; AppendFooter(writer, ref indentation, nameSpace); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
public static bool CreateNewEmptyScript(string fileName, string parentFolder, string nameSpace, string classAttributes, string classDeclarationString, params string[] directives) { AssetDatabaseUtils.CreatePathIfDontExist(parentFolder); string finalFilePath = Path.Combine(parentFolder, $"{fileName}.cs"); if (File.Exists(Path.GetFullPath(finalFilePath))) { return(false); } using (StreamWriter writer = new StreamWriter(finalFilePath)) { bool hasNameSpace = !string.IsNullOrEmpty(nameSpace); int indentation = 0; foreach (string directive in directives) { if (string.IsNullOrWhiteSpace(directive)) { continue; } writer.WriteLine($"using {directive};"); } writer.WriteLine(); if (hasNameSpace) { writer.WriteLine($"namespace {nameSpace}"); writer.WriteLine("{"); indentation++; } if (!string.IsNullOrEmpty(classAttributes)) { writer.WriteLine($"{GetIndentation(indentation)}{classAttributes}"); } writer.WriteLine($"{GetIndentation(indentation)}{classDeclarationString}"); writer.WriteLine(GetIndentation(indentation) + "{"); indentation++; indentation--; writer.WriteLine(GetIndentation(indentation) + "}"); if (hasNameSpace) { writer.WriteLine("}"); } } return(true); }
public static void GenerateStaticCollectionScript(ScriptableObjectCollection collection) { CollectionsRegistry.Instance.ValidateGUIDs(); if (!CanGenerateStaticFile(collection, out string errorMessage)) { Debug.LogError(errorMessage); return; } SerializedObject collectionSerializedObject = new SerializedObject(collection); string fileName = collectionSerializedObject.FindProperty("generatedStaticClassFileName").stringValue; string nameSpace = collectionSerializedObject.FindProperty("generateStaticFileNamespace").stringValue; string finalFolder = collectionSerializedObject.FindProperty("generatedFileLocationPath").stringValue; bool writeAsPartial = collectionSerializedObject.FindProperty("generateAsPartialClass").boolValue; AssetDatabaseUtils.CreatePathIfDontExist(finalFolder); using (StreamWriter writer = new StreamWriter(Path.Combine(finalFolder, $"{fileName}.cs"))) { int indentation = 0; List <string> directives = new List <string>(); directives.Add(typeof(CollectionsRegistry).Namespace); directives.Add(collection.GetType().Namespace); directives.Add(typeof(List <>).Namespace); directives.Add("System.Linq"); directives.AddRange(GetCollectionDirectives(collection)); string className = collection.GetItemType().Name; if (!writeAsPartial) { className = fileName; } AppendHeader(writer, ref indentation, nameSpace, "", className, writeAsPartial, false, directives.Distinct().ToArray() ); WriteDirectAccessCollectionStatic(collection, writer, ref indentation); indentation--; AppendFooter(writer, ref indentation, nameSpace); } AssetDatabase.SaveAssets(); AssetDatabase.Refresh(); }
public static T CreateScriptableObjectOfType <T>(string parentFolderPath, bool createFolderForThisCollection, string targetName) where T : ScriptableObject { T targetCollection = ScriptableObject.CreateInstance <T>(); targetCollection.name = targetName; string targetFolderPath = parentFolderPath; if (createFolderForThisCollection) { targetFolderPath = Path.Combine(targetFolderPath, $"{targetName}"); } AssetDatabaseUtils.CreatePathIfDontExist(Path.Combine(targetFolderPath, "Items")); string collectionAssetPath = Path.Combine(targetFolderPath, $"{targetName}.asset"); AssetDatabase.CreateAsset(targetCollection, collectionAssetPath); return(targetCollection); }