this code is maybe lagging the main thread, help public static void…
- User
this code is maybe lagging the main thread, help
public static void ExportGOToOBJ(Transform[] selection, string mainPath, string filename) { int objCount = 0; ArrayList mfList = new ArrayList(); for (int i = 0; i < selection.Length; i++) { Component[] meshfilter = selection[i].GetComponentsInChildren(typeof(MeshFilter)); for (int m = 0; m < meshfilter.Length; m++) { objCount++; mfList.Add(meshfilter[m]); } } if (objCount > 0) { MeshFilter[] mf = new MeshFilter[mfList.Count]; for (int i = 0; i < mfList.Count; i++) { mf[i] = (MeshFilter)mfList[i]; } if(filename.Equals("")) { filename = selection[0].gameObject.name; } int indexstrip = filename.LastIndexOf('/'); if(indexstrip >= 0) { filename = filename.Substring(indexstrip + 1).Trim(); } MeshesToFile(mf, mainPath, filename); Debug.Log("Exported: " + mainPath + "/"+ filename); #if UNITY_EDITOR UnityEditor.AssetDatabase.SaveAssets(); UnityEditor.AssetDatabase.Refresh(); #endif } else { Debug.Log("Error exporting. Make sure you selected the object."); } }}
- Assistant
Your provided code appears to be a method for exporting GameObjects to an OBJ file in Unity. The lagging you are experiencing on the main thread during the operation could be due to various factors such as heavy iteration, file I/O operations, or unnecessary allocations.
Here are a few suggestions to improve the performance and prevent lagging on the main thread:
-
Avoid Using
ArrayList:ArrayListis not type-safe and incurs more overhead. Use aList<MeshFilter>instead for better performance and type safety.List<MeshFilter> mfList = new List<MeshFilter>(); -
Batch Operations: If
MeshesToFileis a costly operation (especially the file I/O), consider moving it to a background thread, where possible. Unity does not allow certain operations on a background thread, but for custom file handling, you can useTask.Run()orCoroutinepatterns. -
Reduce Debug Log Frequency: Multiple debug logs can slow down operations if invoked inside loops. Collect logs and perform output afterward if needed.
-
Profile the Code: Use the Unity Profiler to identify where the significant time is being spent in your function. This might help in optimizing the specific methods or sections that are performing slow.
-
String Operations: Avoid concatenating strings or performing multiple string manipulations if they are not needed. In general, string manipulation should be minimized in performance-critical sections.
Below is a refactored version of your code that incorporates some suggestions:
using System.Collections.Generic; using UnityEngine; public static class ExporterUtility { public static void ExportGOToOBJ(Transform[] selection, string mainPath, string filename) { List<MeshFilter> mfList = new List<MeshFilter>(); foreach (var trans in selection) { var meshFilter = trans.GetComponentsInChildren<MeshFilter>(); mfList.AddRange(meshFilter); } int objCount = mfList.Count; if (objCount > 0) { if (string.IsNullOrEmpty(filename)) { filename = selection[0].gameObject.name; } int indexStrip = filename.LastIndexOf('/'); if (indexStrip >= 0) { filename = filename.Substring(indexStrip + 1).Trim(); } // Consider running this in separate task or Coroutine if it is heavy MeshesToFile(mfList.ToArray(), mainPath, filename); Debug.Log($"Exported: {mainPath}/{filename}"); #if UNITY_EDITOR UnityEditor.AssetDatabase.SaveAssets(); UnityEditor.AssetDatabase.Refresh(); #endif } else { Debug.LogWarning("Error exporting. Make sure you selected the object."); } } private static void MeshesToFile(MeshFilter[] meshFilters, string path, string filename) { // Your file-writing logic here. } }Further Considerations:
- If
MeshesToFileis doing file writing, consider doing that in an asynchronous manner. Handling file operations in aCoroutinemight prevent frame drops if it takes a significant amount of time. - It is crucial to test the new code to see if there are any unintended side effects and to ensure it performs correctly and efficiently.
- Always PROFILE to find bottlenecks, particularly if running in the Editor, as the UI may compound performance issues.
-