Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Dray · Nov 16, 2015 at 08:42 AM · mesheditor-scriptinghandlespolygonevent-listener

Click event on virtual poly mesh in scene view

I am currently working on an editor tool that gives me the ability to create a mesh and model it in the scene view. I got a lot of stuff working allready using Unitys built in "Handles" but now I'm at a point, where these don't serve all of the functionality I need anymore.

More specifically I'm trying to create a custom handle, that listens for clicks on certain parts of the mesh, so if I click on one of those parts, I can trigger an event that opens up some tools to edit exactly this part.

These parts themselves wil be very simple; most likely less-than-20-triangle-shapes, like a box for example, but the full mesh has too many, too often changing triangles to figure the right part using the triangle-id.

So my idea is to check for click events on "virtual" polys or something, anyhow I'm not really sure how to accomplish this. Any suggestions?

(Please keep in mind that this is all happening in the editmode/sceneview and not necessarily while the game is running!)

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
Best Answer

Answer by Dray · Nov 17, 2015 at 09:59 PM

I got this resolved by writing a custom function that's pretty similar to the built in handles "DistanceToSomething"-functions.

I looked into the function "HandleUtility.DistanceToPolyLine" and added the point-in-poly-raycasting algorithm.

When I click inside the polygon, the function returns 0f as distance. If I click outside of it it returns the distance to the closest line wich is similar to what HandleUtility.DistanceToPolyLine returns.

This is the function:

 public static float DistanceToPoly (Vector3[] vertices)
     {
         Vector2[] points = new Vector2[vertices.Length];
         for (int i = 0; i < vertices.Length; i++) {
             points[i] = HandleUtility.WorldToGUIPoint(vertices[i]);
         }
         Vector2 mousePosition = Event.current.mousePosition;
         bool flag = false;        
         int nvert = points.Length;
         for(int i = 0, j = nvert - 1; i < nvert; j = i++) {
             if( ( (points[i].y >= mousePosition.y ) != (points[j].y >= mousePosition.y) ) &&
                (mousePosition.x <= (points[j].x - points[i].x) * (mousePosition.y - points[i].y) / (points[j].y - points[i].y) + points[i].x)
                )
                 flag = !flag;
         }
 
         if (!flag)
         {
             float num2 = -1f;
             int num = 1;
             for (int j = 0; j < 4; j++)
             {
                 float num3 = HandleUtility.DistancePointToLineSegment (mousePosition, points [j], points [num++]);
                 if (num3 < num2 || num2 < 0f)
                 {
                     num2 = num3;
                 }
             }
             return num2;
         }
         return 0f;
     }

This works just fine for what I want to do. The polygon can be concave but all vertices must share the same plane. For objects with multiple normals, the function could be called multiple times for every triangle for example.

Please note that I didn't test the function very much yet!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Editor Script Handles.Label problem. Labels don't move with gameobject 1 Answer

How do I acces 4-gons (quads) from script 0 Answers

Mouse click a collision mesh 3 Answers

Handles.DrawLine delayed 1 Answer

Large triangular polygon with a large number of points in the mesh 0 Answers


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