Skip to content

Instantly share code, notes, and snippets.

@GuyGinat
Last active November 18, 2024 18:00
Show Gist options
  • Save GuyGinat/612892b9c389420d7d9cd5534b0e520d to your computer and use it in GitHub Desktop.
Save GuyGinat/612892b9c389420d7d9cd5534b0e520d to your computer and use it in GitHub Desktop.
Unity to Obsidian Note Generator
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
public class ObsidianNoteGenerator : EditorWindow
{
private string outputPath = "Assets/ObsidianNotes";
private string targetFolder = "Assets";
[MenuItem("Tools/Obsidian Note Generator")]
public static void ShowWindow()
{
GetWindow<ObsidianNoteGenerator>("Obsidian Note Generator");
}
private void OnGUI()
{
GUILayout.Label("Output Path", EditorStyles.boldLabel);
outputPath = GUILayout.TextField(outputPath);
GUILayout.Label("Target Folder", EditorStyles.boldLabel);
if (GUILayout.Button("Select Folder"))
{
targetFolder = EditorUtility.OpenFolderPanel("Select Target Folder", "Assets", "");
if (!string.IsNullOrEmpty(targetFolder))
{
if (targetFolder.StartsWith(Application.dataPath))
{
targetFolder = "Assets" + targetFolder.Substring(Application.dataPath.Length);
}
else
{
targetFolder = "Assets";
}
}
}
GUILayout.Label("Selected Folder: " + targetFolder);
if (GUILayout.Button("Generate Notes"))
{
GenerateObsidianNotes();
}
}
private void GenerateObsidianNotes()
{
if (!Directory.Exists(outputPath))
{
Directory.CreateDirectory(outputPath);
}
string[] scriptFiles = Directory.GetFiles(targetFolder, "*.cs", SearchOption.AllDirectories);
Dictionary<string, List<string>> references = new Dictionary<string, List<string>>();
foreach (var file in scriptFiles)
{
string content = File.ReadAllText(file);
string className = Path.GetFileNameWithoutExtension(file);
references[className] = new List<string>();
foreach (var otherFile in scriptFiles)
{
if (file == otherFile) continue;
string otherContent = File.ReadAllText(otherFile);
string otherClassName = Path.GetFileNameWithoutExtension(otherFile);
if (Regex.IsMatch(otherContent, $@"\b{className}\b"))
{
references[className].Add(otherClassName);
}
}
}
foreach (var kvp in references)
{
string filePath = Path.Combine(outputPath, kvp.Key + ".md");
using (StreamWriter sw = new StreamWriter(filePath))
{
sw.WriteLine($"# {kvp.Key}");
sw.WriteLine();
sw.WriteLine("## References");
foreach (var reference in kvp.Value)
{
sw.WriteLine($"- [[{reference}]]");
}
}
}
AssetDatabase.Refresh();
Debug.Log("Obsidian notes generated successfully.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment