一种是alpha检测

一种是设置collider

参考:

https://zhuanlan.zhihu.com/p/34204396

下面给出第二种方案代码

///按钮多边形点击方案,注意Canvas模式应该是Screen Space - Camera  需设置 Render Camera
///选中按钮右键UI->变更为多边形按钮,编辑子物体的Collider2D即可
///如果无法编辑Collider 则是Unity编辑器bug 切换下Unity编辑器的布局模式,即可恢复正常 using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif [RequireComponent(typeof(PolygonCollider2D))]
public class NonRectangularButtonImage : Image
{
private PolygonCollider2D areaPolygon; protected NonRectangularButtonImage()
{
useLegacyMeshGeneration = true;
} private PolygonCollider2D Polygon
{
get
{
if (areaPolygon != null)
return areaPolygon; areaPolygon = GetComponent<PolygonCollider2D>();
return areaPolygon;
}
} protected override void OnPopulateMesh(VertexHelper vh)
{
vh.Clear();
} public override bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
{
return Polygon.OverlapPoint(eventCamera.ScreenToWorldPoint(screenPoint));
} #if UNITY_EDITOR
protected override void Reset()
{
base.Reset();
transform.localPosition = Vector3.zero;
var w = rectTransform.sizeDelta.x * 0.5f + 0.1f;
var h = rectTransform.sizeDelta.y * 0.5f + 0.1f;
Polygon.points = new[]
{
new Vector2(-w, -h),
new Vector2(w, -h),
new Vector2(w, h),
new Vector2(-w, h)
};
}
#endif
}
#if UNITY_EDITOR
[CustomEditor(typeof(NonRectangularButtonImage), true)]
public class CustomRaycastFilterInspector : Editor
{
public override void OnInspectorGUI()
{
}
} public class NonRectAngularButtonImageHelper
{
[MenuItem("GameObject/UI/变更为多边形按钮")]
public static void CreateNonRectAngularButtonImage()
{
var goRoot = Selection.activeGameObject;
if (goRoot == null)
return; var button = goRoot.GetComponent<Button>(); if (button == null)
{
Debug.Log("Selecting Object is not a button!");
return;
} // 关闭原来button的射线检测
var graphics = goRoot.GetComponentsInChildren<Graphic>();
foreach (var graphic in graphics)
{
graphic.raycastTarget = false;
} var polygon = new GameObject("NonRectangularButtonImage");
polygon.AddComponent<PolygonCollider2D>();
polygon.AddComponent<NonRectangularButtonImage>();
polygon.transform.SetParent(goRoot.transform, false);
polygon.transform.SetAsLastSibling();
//设置锚点方案,注意rect transfrom的框必须包住多边形,否则超出的部分无效
polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Left, , );
polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Top, , );
polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Right, , );
polygon.GetComponent<RectTransform>().SetInsetAndSizeFromParentEdge(RectTransform.Edge.Bottom, , );
polygon.GetComponent<RectTransform>().anchorMin = new Vector2(, );
polygon.GetComponent<RectTransform>().anchorMax = new Vector2(, );
}
} #endif

unity不规则按钮解决方案的更多相关文章

  1. Unity不规则按钮点击区域(UGUI)

    文章目录 一. 前言 二. 最终效果 三. 实现 1.创建UICamera 2. UIPolygon节点 3. 编辑碰撞区域 5. 运行测试 6. UIPolygon代码 一. 前言 游戏开发中,可能 ...

  2. Unity 不规则按钮实现

    1.先重写Image类,实现对Image图形范围的重写: 2.对不规则按钮添加Polygon Collider2D组件,调整大小圈中要点击的范围: 3.将重写的Image类添加到不规则按钮上时,需要移 ...

  3. ngui处理不规则按钮点击

    吐个槽  棋牌类游戏做什么中国地图!!!  然后就要用到不规则按钮点击了 你懂的 213的unity虽然已经加入了polygoncollider 2d的支持 但是 但是 但是 是2d的 也就是说如果不 ...

  4. [示例] Firemonkey 不规则按钮实做

    利用 Firemonkey 控件的组合及可塑性,可以做出千变万化的效果及功能,下面展示一个不规则按钮的实做: 效果图: 实做方法: 开一个新工程 Multi-Device Application 放一 ...

  5. 源码推荐(7.17):不规则按钮类似于遥控器按钮,一个可以最大程度简化PageView与TabView切换的第三方框架

    不规则按钮,类似于遥控器按钮,可以单独控制按钮的上下左右(作者:masa_chu) 不规则按钮,类似于遥控器按钮,可以单独控制按钮的上下左右 测试环境:Xcode 6.2,iOS 6.0以上 Lazy ...

  6. Qt5:不规则按钮的实现---通过贴图实现

    在应用开发中,有时候为了美观会在UI界面中增加不规则的按钮 现在我们就来看看Qt中是怎么实现不规则按钮的 /////////////////////////////////////////////// ...

  7. iOS 开源一个高度可定制支持各种动画效果,支持单击双击,小红点,支持自定义不规则按钮的tabbar

    TYTabbarAnimationDemo 业务需求导致需要做一个tabbar,里面的按钮点击带有动画效果,tabbar中间的按钮凸出,凸出部分可以点击,支持badge 小红点等,为此封装了一个高度可 ...

  8. UGUI之不规则按钮的响应区域

    比如一些不规则按钮最好可以设置它的响应区域.如下图所示,用Polygon Collider2D组件圈出精灵响应事件的区域. 注意 IsRaycastLocationValid 的判断区域是RectTr ...

  9. 在UNITY中按钮的高亮用POINT灯实现,效果别具一番风味

    在UNITY中按钮的高亮用POINT灯实现,效果别具一番风味

随机推荐

  1. 34. Find First and Last Position of Element in Sorted Array (JAVA)

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

  2. docker持久化之存储卷

         启动时敲:   docker run -it -v /var/mydata:/abc myos   #前面写真实机里的文件夹,冒号后面写docker里面的文件夹,最后写镜像名 (可以同时挂载 ...

  3. 高可用4层lvs——keepalived

    搭建方式: node01: ipvsadm -C ifconfig eth0:2 down --------------------------------- node01,node04安装keepa ...

  4. 一个有python扩展库的下载网站

    https://www.lfd.uci.edu/~gohlke/pythonlibs/

  5. Linux虚拟机网络设置问题

    使用的是VM 工作站15 和以前的不一样 没有什么虚拟机网络设置 :打开Edit->Virtual NetWork editor, 选中VMnet8,然后点击NAT Setting按钮,再点击D ...

  6. bzoj4129 Haruna’s Breakfast 树上带修莫队+分块

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4129 题解 考虑没有修改的序列上的版本应该怎么做: 弱化的题目应该是这样的: 给定一个序列,每 ...

  7. Textarea随着文本的字数自适应高度,后来发现用 contenteditable 代替textarea 效果更佳

    做移动端项目遇到很多问题,最近比如textarea 随着文本的字数自动撑开高度, 网上也查阅了一些资料发现比较有用的方法 就是获取 textarea的行数和换行符来动态改变textarea的高度  就 ...

  8. PHP环境安全性能检查

    PHP环境安全性能检查 PHP在Linux环境下安全配置是一个复杂的过程,其中涉及到很多的细节设置,在这里发出来一个脚本,通过这个脚本来检测你的PHP环境是否存在安全隐患,从而针对这些对你的PHP环境 ...

  9. 字符编码 python2与python3的区别

    目录 1. 字符编码 2. 文本编辑器存储信息的过程 3. 编码: 1. 编码的历史 2. gb2312和gbk的区别 3. 编码和解码 4. python解释器 解释代码的流程 1. 读取文本到解释 ...

  10. heroinfo_set.all 函数

    如果是 一对多 关系 即使用 heroinfo_set.all  此时关联字段类型通用,即上边的字段通用,但是需要添加many=True的参数heroinfo_set = serializers.Pr ...