123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using UnityEditor;
- using UnityEngine;
- namespace ProjectExporter
- {
- public class ProjectExporterController
- {
- private static readonly string ProjectRoot = Path.GetDirectoryName(Application.dataPath);
- // Finds assets based on a search filter (e.g., "t:Scene") and builds a hierarchical model.
- public AssetModel GetAssets(string filter)
- {
- var root = new AssetModel("Assets", "Assets");
- var assetGuids = AssetDatabase.FindAssets(filter, new[] { "Assets" });
- foreach (var guid in assetGuids)
- {
- var path = AssetDatabase.GUIDToAssetPath(guid);
- if (filter.Contains("t:ScriptableObject") && !path.EndsWith(".asset"))
- {
- continue;
- }
- var parts = path.Split('/');
- var currentNode = root;
- for (int i = 1; i < parts.Length; i++) // Start at 1 to skip "Assets"
- {
- var part = parts[i];
- var child = currentNode.Children.FirstOrDefault(c => c.Name == part);
- if (child == null)
- {
- var currentPath = string.Join("/", parts.Take(i + 1));
- child = new AssetModel(currentPath, part);
- currentNode.Children.Add(child);
- }
- currentNode = child;
- }
- }
- return root;
- }
- // Lists all files in the ProjectSettings directory.
- public AssetModel GetProjectSettingsFiles()
- {
- var settingsRootPath = Path.Combine(ProjectRoot, "ProjectSettings");
- var root = new AssetModel(settingsRootPath, "ProjectSettings");
- if (Directory.Exists(settingsRootPath))
- {
- // Filter to only include *.asset files.
- var files = Directory.GetFiles(settingsRootPath, "*.asset", SearchOption.AllDirectories);
- foreach (var file in files)
- {
- var relativePath = Path.GetRelativePath(ProjectRoot, file);
- root.Children.Add(new AssetModel(relativePath, Path.GetFileName(file)));
- }
- }
- return root;
- }
- // Scans the entire Assets folder for .meta files and builds a hierarchical model.
- public AssetModel GetAllMetaFiles()
- {
- var assetsRootPath = Application.dataPath; // This is the "Assets" folder
- var root = new AssetModel("Assets", "Assets");
- var files = Directory.GetFiles(assetsRootPath, "*.meta", SearchOption.AllDirectories);
-
- foreach (var file in files)
- {
- var relativePath = "Assets/" + Path.GetRelativePath(assetsRootPath, file).Replace('\\', '/');
- var parts = relativePath.Split('/');
- var currentNode = root;
- for (int i = 1; i < parts.Length; i++) // Start at 1 to skip "Assets"
- {
- var part = parts[i];
- var child = currentNode.Children.FirstOrDefault(c => c.Name == part);
- if (child == null)
- {
- var currentPath = string.Join("/", parts.Take(i + 1));
- child = new AssetModel(currentPath, part);
- currentNode.Children.Add(child);
- }
- currentNode = child;
- }
- }
- return root;
- }
-
- private string FindPythonExecutable()
- {
- var venvPath = Path.Combine(ProjectRoot, "venv", "bin", "python3");
- if (File.Exists(venvPath))
- {
- return venvPath;
- }
- venvPath = Path.Combine(ProjectRoot, "venv", "bin", "python");
- if (File.Exists(venvPath))
- {
- return venvPath;
- }
- return "python3";
- }
- private string GetConversionScriptPath()
- {
- var guids = AssetDatabase.FindAssets("convert_scene");
- if (guids.Length == 0)
- {
- UnityEngine.Debug.LogError("Conversion script 'convert_scene.py' not found.");
- return null;
- }
- var path = AssetDatabase.GUIDToAssetPath(guids[0]);
- return Path.Combine(ProjectRoot, path);
- }
- // Executes the Python conversion script for the selected assets.
- public void ExportAssets(AssetModel rootNode, string exportType)
- {
- var assetsToExport = new List<string>();
- CollectSelectedAssets(rootNode, assetsToExport);
-
- var pythonExecutable = FindPythonExecutable();
- var conversionScript = GetConversionScriptPath();
- if (string.IsNullOrEmpty(conversionScript)) return;
- var outputDirectory = Path.Combine(ProjectRoot, "Library", exportType);
- Directory.CreateDirectory(outputDirectory);
- EditorUtility.DisplayProgressBar("Exporting...", "Starting export process...", 0f);
- try
- {
- for (int i = 0; i < assetsToExport.Count; i++)
- {
- var relativeAssetPath = assetsToExport[i];
- var progress = (float)i / assetsToExport.Count;
- EditorUtility.DisplayProgressBar("Exporting...", $"Processing {Path.GetFileName(relativeAssetPath)}", progress);
- var absoluteAssetPath = Path.Combine(ProjectRoot, relativeAssetPath);
- var outputJsonPath = Path.Combine(outputDirectory, relativeAssetPath.Replace('/', Path.DirectorySeparatorChar) + ".json");
-
- var fileOutputDir = Path.GetDirectoryName(outputJsonPath);
- if (!Directory.Exists(fileOutputDir))
- {
- Directory.CreateDirectory(fileOutputDir);
- }
- var process = new Process
- {
- StartInfo = new ProcessStartInfo
- {
- FileName = pythonExecutable,
- Arguments = $"\"{conversionScript}\" \"{absoluteAssetPath}\" \"{outputJsonPath}\"",
- RedirectStandardOutput = true,
- RedirectStandardError = true,
- UseShellExecute = false,
- CreateNoWindow = true
- }
- };
- process.Start();
- string output = process.StandardOutput.ReadToEnd();
- string error = process.StandardError.ReadToEnd();
- process.WaitForExit();
- if (process.ExitCode != 0)
- {
- UnityEngine.Debug.LogError($"Error exporting {relativeAssetPath}: {error}");
- }
- else
- {
- UnityEngine.Debug.Log($"Successfully exported {relativeAssetPath}: {output}");
- }
- }
- }
- finally
- {
- EditorUtility.ClearProgressBar();
- }
- }
- // Recursively collects the paths of all selected assets.
- private void CollectSelectedAssets(AssetModel node, List<string> selectedPaths)
- {
- // If the node itself is a file and is selected, add it.
- if (node.Children.Count == 0 && node.IsSelected)
- {
- selectedPaths.Add(node.Path);
- }
- // Recurse into children
- foreach (var child in node.Children)
- {
- CollectSelectedAssets(child, selectedPaths);
- }
- }
- }
- }
|