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!)

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][1].

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 _= HandleUtility.WorldToGUIPoint(vertices*);*_

* }*
* 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*.y >= mousePosition.y ) != (points[j].y >= mousePosition.y) ) &&
(mousePosition.x <= (points[j].x - points.x) * (mousePosition.y - points.y) / (points[j].y - points.y) + points.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!
[1]: Point in polygon - Wikipedia