If a filename is the same then change the filename slightly to make it unique. using…
- User
If a filename is the same then change the filename slightly to make it unique.
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; // Path where exports will be saved public UnityEngine.UI.Slider progressBar; // Reference to the UI Slider for progress
code// Sanitize filename by removing invalid characters public static string SanitizeFilename(string filename) { string invalidCharsPattern = @"[\\/:*?""<>|]"; filename = Regex.Replace(filename, invalidCharsPattern, string.Empty); return filename.Trim(); } // Retrieve the child objects of the simulation void GetSimChildren() { Sim = FindSim(); SimObjects.Clear(); // Add all children to SimObjects list foreach (Transform child in Sim.transform) { SimObjects.Add(child.gameObject); } } // Find the simulation GameObject by tag public GameObject FindSim() { return GameObject.FindGameObjectWithTag("TerrainOBJ"); } // Initiate export process public void ExportSimBatch() { StartCoroutine(ExportWithProgress()); } // Export objects with progress tracking 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; // Use sanitized mesh name or generate a unique filename if empty sanitizedFilename = !string.IsNullOrEmpty(mesh.name) ? SanitizeFilename(mesh.name) : "UnnamedMesh_" + Guid.NewGuid().ToString(); try { // Export the mesh with its sanitized filename 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 for next frame to avoid blocking } Debug.Log("Export complete."); } // Export the mesh to a file public void ExportRuntime(GameObject gameObject, string sanitizedFilename) { ExporterRuntime ex = this.gameObject.GetComponent<ExporterRuntime>(); if (ex != null) { // Create a sub-folder for the terrain's exports using its sanitized name string terrainFolderPath = Path.Combine(ExportFolderPath, SanitizeFilename(FindSim().name)); Directory.CreateDirectory(terrainFolderPath); // Ensure the directory exists // Combine folder and filename to create full file path for export string filePath = Path.Combine(terrainFolderPath, sanitizedFilename + ".obj"); ex.ExportObj(gameObject, terrainFolderPath, filePath); // Export the object } else { Debug.LogError("ExporterRuntime component not found on the GameObject."); } } - Assistant
- User
avoid IOException: Sharing violation at all costs.
private static async Task MeshesToFileAsync(MeshFilter[] mf, string folder, string filename) { Dictionary<string, OMATRuntime> materialList = PrepareFileWrite();
codeusing (StreamWriter sw = new StreamWriter(Path.Combine(folder, $"{filename}.obj"))) //IOException: Sharing violation! { await sw.WriteLineAsync($"mtllib ./{filename}.mtl\n"); for (int i = 0; i < mf.Length; i++) { await sw.WriteAsync(MeshToString(mf[i], materialList)); } } await MaterialsToFileAsync(materialList, folder, filename);}
- Assistant
- User
sharing violation c#
- Assistant
- User
- Assistant