I'm confusing myself editing this c# script for Unity, please correct any weirdness with…
- User
I'm confusing myself editing this c# script for Unity, please correct any weirdness with the filenames and make it clearer when it comes to whats a folder path and whats a filename in the function
using UnityEngine; using System.Collections; using System.Text.RegularExpressions; using System.Collections.Generic; using System; using System.IO;
public class ExportRuntimeExample : MonoBehaviour { public GameObject Sim; private List SimObjects = new List(); public string ExportFolderPath = string.Empty; public UnityEngine.UI.Slider progressBar; // Reference to the UI Slider for progress
codepublic static string SanitizeFilename(string filename) { // Define a regex pattern for invalid characters string invalidCharsPattern = @"[\\/:*?""<>|]"; filename = Regex.Replace(filename, invalidCharsPattern, string.Empty); return filename.Trim(); } void GetSimChildren() { Sim = FindSim(); SimObjects.Clear(); // Add all children to SimObjects foreach (Transform child in Sim.transform) { SimObjects.Add(child.gameObject); } } public GameObject FindSim() { return GameObject.FindGameObjectWithTag("TerrainOBJ"); } public void ExportSimBatch() { // Start the coroutine to export with progress StartCoroutine(ExportWithProgress()); } private IEnumerator ExportWithProgress() { GetSimChildren(); if (SimObjects == null || SimObjects.Count == 0) { Debug.LogWarning("No simulation objects found for export."); yield break; } int totalObjects = SimObjects.Count; for (int i = 0; i < totalObjects; i++) { GameObject mesh = SimObjects[i]; string sanitizedFilename; if (!string.IsNullOrEmpty(mesh.name)) { sanitizedFilename = SanitizeFilename(mesh.name); } else { // Generate a unique filename if the mesh name is empty sanitizedFilename = "UnnamedMesh_" + Guid.NewGuid().ToString(); // Or use a timestamp } try { ExportRuntime(mesh, sanitizedFilename); } catch (Exception ex) { Debug.LogError($"Failed to export {mesh.name}: {ex.Message}"); } // Update the progress bar progressBar.value = (float)(i + 1) / totalObjects; yield return null; // Wait until the next frame to avoid blocking } Debug.Log("Export complete."); } public void ExportRuntime(GameObject Gameobject, string FolderName) { ExporterRuntime ex = this.gameObject.GetComponent<ExporterRuntime>(); if (ex != null) { string FilePath = @ExportFolderPath + @"\" + SanitizeFilename(GameObject.FindGameObjectWithTag("TerrainOBJ").name); Directory.CreateDirectory(FilePath); ex.ExportObj(Gameobject, @ExportFolderPath + @"\" + SanitizeFilename(GameObject.FindGameObjectWithTag("TerrainOBJ").name), FilePath); } else { Debug.LogError("ExporterRuntime component not found on the GameObject."); } } - Assistant