if any filenames are empty for exporter then give a unique filename. using UnityEngine;…
- User
if any filenames are empty for exporter then give a unique filename.
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
public 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]; if (!string.IsNullOrEmpty(mesh.name)) { try { ExportRuntime(mesh, SanitizeFilename(mesh.name)); } 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) { Directory.CreateDirectory(@ExportFolderPath + @"\" + SanitizeFilename(GameObject.FindGameObjectWithTag("TerrainOBJ").name)); ex.ExportObj(Gameobject, @ExportFolderPath + @"\" + SanitizeFilename(GameObject.FindGameObjectWithTag("TerrainOBJ").name), FolderName); } else { Debug.LogError("ExporterRuntime component not found on the GameObject."); } }}
- Assistant
To ensure that a unique filename is generated for any empty filenames during the export process, you can modify your
ExportWithProgressmethod. Here's an approach you can take:- Check if the mesh name is empty or null before calling
SanitizeFilename. - If it is empty, create a unique filename by appending a timestamp or a counter to ensure it will be unique.
Here’s the modified section of your code:
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."); }Explanation:
- If the
mesh.nameis empty or null, a unique filename is generated usingGuid.NewGuid(), which creates a unique identifier. - Alternatively, you could use a timestamp or a simple counter to create a unique name.
- The rest of the export logic remains unchanged, ensuring that every exported file has a unique name even if the original filename is empty.
This modification will prevent any issues related to empty filenames when exporting your game objects in Unity.
- Check if the mesh name is empty or null before calling