- Home /
Im having an issue with a custom editor script pausing the whole editor with the little applicaton.message?
I tried two seperate scripts to try a different approach and see which is easier to logically go about finding and it pauses on only one of them. I sat for at least a minute waiting for it to finish but it didn't. This is the code for the actual script that runs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[ExecuteInEditMode]
public class FaceFinder : MonoBehaviour
{
public Face[] faced;
public void FindFaces()
{
List<Face> faces = new List<Face>();
Mesh mesh = this.GetComponent<MeshFilter>().sharedMesh;
Debug.Log("Finding");
for(int i =0; i < mesh.triangles.Length; i += 3)
{
Vector3 v1 = mesh.vertices[mesh.triangles[i + 0]];
Vector3 v2 = mesh.vertices[mesh.triangles[i + 1]];
Vector3 v3 = mesh.vertices[mesh.triangles[i + 2]];
Vector3[] sorted = returnOrdered(v1, v2, v3);
Vector3 line1 = (sorted[1] - sorted[0]);
Vector3 line2 = (sorted[1] - sorted[2]);
Vector3 cross = Vector3.Cross(line2, line1);
for (int j = 0; j < mesh.triangles.Length; i += 3)
{
if (j != i)
{
Vector3 vertex1 = mesh.vertices[mesh.triangles[j + 0]];
Vector3 vertex2 = mesh.vertices[mesh.triangles[j + 1]];
Vector3 vertex3 = mesh.vertices[mesh.triangles[j + 2]];
Vector3[] sorted1 = returnOrdered(vertex1, vertex2, vertex3);
Vector3 line3 = (sorted[1] - sorted[0]);
Vector3 line4 = (sorted[1] - sorted[2]);
Vector3 cross2 = Vector3.Cross(line3, line4);
if (cross == cross2)
{
//Vector3[] matched = FindAndRemoveMatched(sorted, sorted1);
Face f = new Face();
Vector3[] matched = { v1, v2, v3, vertex1 };
f.vertices = matched;
f.triangles = new int[] { i, j };
faces.Add(f);
}
}
}
}
faced = faces.ToArray();
}
public Vector3[] FindAndRemoveMatched(Vector3[] array1,Vector3[] array2)
{
List<Vector3> newList = new List<Vector3>();
newList.AddRange(array1);
newList.AddRange(array2);
Vector3[] combined = newList.ToArray();
for(int i =0; i < combined.Length; i++)
{
for(int k = i + 1; k < combined.Length; k++)
{
if (combined[i] == combined[k])
{
newList.Remove(combined[i]);
}
}
}
Debug.Log(newList.Count);
return newList.ToArray();
}
Vector3[] returnOrdered(Vector3 v1, Vector3 v2, Vector3 v3)
{
bool sorted = false;
Vector3[] vecArry = new Vector3[3] { v1, v2, v3 };
while (sorted == false)
{
sorted = true;
for (int i = 0; i < vecArry.Length - 1; i++)
{
if (vecArry[i].magnitude > vecArry[i + 1].magnitude)
{
vecArry[i] = vecArry[i + 1];
sorted = false;
}
}
}
return vecArry;
}
}
[System.Serializable]
public class Face{
public Vector3[] vertices;
//Should only be two but whatever.
public int[] triangles;
public Face()
{
}
}
Here is the script for the editor button former.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(FaceFinder))]
public class FaceFinderEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
FaceFinder generateField = (FaceFinder)target;
if (GUILayout.Button("Find"))
{
generateField.FindFaces();
}
}
}
Answer by djenningsais · Apr 01, 2021 at 06:25 PM
for(int i =0; i < mesh.triangles.Length; i += 3)
{
...
for (int j = 0; j < mesh.triangles.Length; i += 3)
Not sure how large triangles.Length is but nesting a for loop inside of a for loop of the same magnitude is potentially detrimental to performance.
returnOrdered nests a for loop within a while loop.
Currently implemented you have two nested for loops within a nested for loop.
If line 38 was uncommented you would have another nested loop running.
See if you can reduce some loop layers.
For some practice on this concept I would go check out HackerRank.com.
Your answer
Follow this Question
Related Questions
Check for compilation errors in Editor Script 0 Answers
Why I get this Debug error: 'fileLength == processed' when calling CreatePrefab? 0 Answers
Unity Editor Custom script 2 Answers
How to exclude "Selection & Handles" and other editor-specific code in builds? 2 Answers
How do I fix error code cs1056: Unexpected Character 1 Answer