private static void GenerateScript <TClass, TAttribute>(string scriptName, string templateName, string entryFormat) where TAttribute : IAttribute { var templateAssets = AssetDatabase.FindAssets(templateName); if (templateAssets.Length == 0) { return; } var templateGUID = templateAssets[0]; var templateRelativePath = AssetDatabase.GUIDToAssetPath(templateGUID); var templateFormat = (AssetDatabase.LoadAssetAtPath(templateRelativePath, typeof(TextAsset)) as TextAsset) .ToString(); //string templateFullPath = (Application.dataPath.Replace("Assets", string.Empty) + templateRelativePath).Replace("/", "\\"); //string templateFormat = IOUtility.ReadFromFile(templateFullPath); var entriesBuilder = new StringBuilder(); var subTypes = GetAllSubTypes(typeof(TClass)); foreach (var subType in subTypes) { var attributes = (IAttribute[])subType.GetCustomAttributes(typeof(TAttribute), true); if (attributes.Length > 0) { entriesBuilder.AppendFormat(entryFormat, attributes[0].TargetAttributeType.Name, subType.Name); } } var scriptContent = templateFormat .Replace(CLASS_NAME_PLACEHOLDER, scriptName) .Replace(ENTRIES_PLACEHOLDER, entriesBuilder.ToString()); scriptContent = Regex.Replace(scriptContent, @"\r\n|\n\r|\r|\n", Environment.NewLine); // Normalize line endings var scriptPath = GENERATED_CODE_TARGET_FOLDER + scriptName + ".cs"; IOUtility.WriteToFile(scriptPath, scriptContent); }
private static void GenerateScript <TAttributeGroup, TPropertyAttribute>(string scriptName, string templateName, string entryFormat) where TPropertyAttribute : IAttribute { string[] templateAssets = AssetDatabase.FindAssets(templateName); if (templateAssets.Length == 0) { return; } string templateGUID = templateAssets[0]; string templateRelativePath = AssetDatabase.GUIDToAssetPath(templateGUID); string templateFullPath = (Application.dataPath.Replace("Assets", string.Empty) + templateRelativePath).Replace("/", "\\"); string templateFormat = IOUtility.ReadFromFile(templateFullPath); StringBuilder entriesBuilder = new StringBuilder(); List <Type> subTypes = GetAllSubTypes(typeof(TAttributeGroup)); foreach (var subType in subTypes) { IAttribute[] attributes = (IAttribute[])subType.GetCustomAttributes(typeof(TPropertyAttribute), true); if (attributes.Length > 0) { entriesBuilder.AppendFormat(entryFormat, attributes[0].TargetAttributeType.Name, subType.Name); } } string scriptContent = templateFormat .Replace(CLASS_NAME_PLACEHOLDER, scriptName) .Replace(ENTRIES_PLACEHOLDER, entriesBuilder.ToString()); string scriptPath = GENERATED_CODE_TARGET_FOLDER + scriptName + ".cs"; IOUtility.WriteToFile(scriptPath, scriptContent); }