unity不规则按钮解决方案

一种是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
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )