Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by hellojbb1234 · Apr 01, 2021 at 03:21 PM · erroreditoreditor-scripting

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();
         }
 
 
     }
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by djenningsais · Apr 01, 2021 at 06:25 PM

@hellojbb1234

 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

199 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges