Raycast on Graphic with Custom Mesh,
I'm trying to figure out a way to get the graphics raycast to hit the drawn mesh rather than the RectTransform. I want OnPointerClick to trigger when the orange box drawn by OnPopulateMesh is clicked rather than the invisible box. Assuming I'll have more complicated shapes drawn by OnPopulateMesh, is there an easy way to achieve this?
public class ClickShape : MaskableGraphic, IPointerClickHandler
{
public void OnPointerClick(PointerEventData eventData){
Debug.Log("Shape Clicked");
}
private Vector3[] allVertices = new Vector3[]{new Vector2(200,200),new Vector2(300,200),new Vector2(300,300),new Vector2(200,300)};
private int[] allTriangles = new int[]{0,1,2,0,2,3};
protected override void OnPopulateMesh(VertexHelper vh){
vh.Clear();
UIVertex vert = new UIVertex();
vert.color = this.color;
foreach(Vector3 vertex in allVertices){
vert.position = vertex;
vert.uv0 = vertex;
vh.AddVert(vert);
}
for(int i = 0; i < allTriangles.Length; i+=3){
vh.AddTriangle(allTriangles[i],allTriangles[i+1],allTriangles[i+2]);
}
}
}
,
Answer by MacMillan333 · Jan 31, 2021 at 06:20 PM
One workaround is to use the Physics2DRaycaster. Attach a PolygonCollider2D to your graphic, update its polygon the same time OnPopulateMesh is called, and call Physics2DRaycaster.Raycast when the user clicks.
Your answer
Follow this Question
Related Questions
Where to find the new Dropdown menu 0 Answers
OnGUI behind UI? 0 Answers
Calling coroutine in a method issue. 1 Answer
Player character jumping when I wish to only initiate pause - UI Button element. 1 Answer
Number string to .text returning 0 0 Answers