一种是alpha检测
一种是设置collider
参考:
https://zhuanlan.zhihu.com/p/34204396
下面给出第二种方案代码
1///按钮多边形点击方案,注意Canvas模式应该是Screen Space - Camera 需设置 Render Camera 2///选中按钮右键UI->变更为多边形按钮,编辑子物体的Collider2D即可 3///如果无法编辑Collider 则是Unity编辑器bug 切换下Unity编辑器的布局模式,即可恢复正常 4 5using UnityEngine; 6using UnityEngine.UI; 7#if UNITY_EDITOR 8using UnityEditor; 9#endif 10 11[RequireComponent(typeof(PolygonCollider2D))] 12public class NonRectangularButtonImage : Image 13{ 14 private PolygonCollider2D areaPolygon; 15 16 protected NonRectangularButtonImage() 17 { 18 useLegacyMeshGeneration = true; 19 } 20 21 private PolygonCollider2D Polygon 22 { 23 get 24 { 25 if (areaPolygon != null) 26 return areaPolygon; 27 28 areaPolygon = GetComponent<PolygonCollider2D>(); 29 return areaPolygon; 30 } 31 } 32 33 protected override void OnPopulateMesh(VertexHelper vh) 34 { 35 vh.Clear(); 36 } 37 38 public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera) 39 { 40 return Polygon.OverlapPoint(eventCamera.ScreenToWorldPoint(screenPoint)); 41 } 42 43#if UNITY_EDITOR 44 protected override void Reset() 45 { 46 base.Reset(); 47 transform.localPosition = Vector3.zero; 48 var w = rectTransform.sizeDelta.x * 0.5f + 0.1f; 49 var h = rectTransform.sizeDelta.y * 0.5f + 0.1f; 50 Polygon.points = new[] 51 { 52 new Vector2(-w, -h), 53 new Vector2(w, -h), 54 new Vector2(w, h), 55 new Vector2(-w, h) 56 }; 57 } 58#endif 59} 60#if UNITY_EDITOR 61[CustomEditor(typeof(NonRectangularButtonImage), true)] 62public class CustomRaycastFilterInspector : Editor 63{ 64 public override void OnInspectorGUI() 65 { 66 } 67} 68 69public class NonRectAngularButtonImageHelper 70{ 71 [MenuItem("GameObject/UI/变更为多边形按钮")] 72 public static void CreateNonRectAngularButtonImage() 73 { 74 var goRoot = Selection.activeGameObject; 75 if (goRoot == null) 76 return; 77 78 var button = goRoot.GetComponent<Button>(); 79 80 if (button == null) 81 { 82 Debug.Log("Selecting Object is not a button!"); 83 return; 84 } 85 86 // 关闭原来button的射线检测 87 var graphics = goRoot.GetComponentsInChildren<Graphic>(); 88 foreach (var graphic in graphics) 89 { 90 graphic.raycastTarget = false; 91 } 92 93 var polygon = new GameObject("NonRectangularButtonImage"); 94 polygon.AddComponent<PolygonCollider2D>(); 95 polygon.AddComponent<NonRectangularButtonImage>(); 96 polygon.transform.SetParent(goRoot.transform, false); 97 polygon.transform.SetAsLastSibling(); 98 //设置锚点方案,注意rect transfrom的框必须包住多边形,否则超出的部分无效 99 polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, 0, 0); 100 polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, 0, 0); 101 polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, 0, 0); 102 polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, 0, 0); 103 polygon.GetComponent<RectTransform>().anchorMin = new Vector2(0, 0); 104 polygon.GetComponent<RectTransform>().anchorMax = new Vector2(1, 1); 105 } 106} 107 108#endif