在网上找了找有shader实现,也有下面这种重绘图片顶点的做法。

参考文章:
Unity中实现引导的镂空效果
https://blog.csdn.net/lllll__/article/details/104263122
Unity3D UGUI 性能耗费最小的一种UI渲染方式RawImage实现,圆角矩形,圆形,多边形等图片
https://www.geek-share.com/detail/2752643497.html
SimpleRoundedImage-不使用mask实现圆角矩形图片
https://www.geek-share.com/detail/2745953780.html

  • 处理方式:将上面两个操作合并了了下,做个缝合怪【狗头】。

  • 变更示意图:

  • 最终效果:


  • 代码:
using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;
/// <summary>
/// 镂空背景图
/// </summary>
public class UIHollowOutImage : Graphic
{public float Radius = 10f;//内切圆半径 图片的一半差不多就是一个圆了 这里相当于图片十分之一的长度public int TriangleNum = 6;//每个扇形三角形个数 个数越大弧度越平滑public RectTransform inner_trans;private RectTransform outer_trans;//背景区域private Vector2 inner_rt;//镂空区域的右上角坐标private Vector2 inner_lb;//镂空区域的左下角坐标private Vector2 outer_rt;//背景区域的右上角坐标private Vector2 outer_lb;//背景区域的左下角坐标[Header("是否实时刷新")][Space(25)]public bool realtimeRefresh;[Header("是否显示镂空")][Space(25)]public bool ShowHollowOut = true;protected override void Awake(){base.Awake();outer_trans = GetComponent<RectTransform>();//计算边界CalcBounds();}protected override void OnPopulateMesh(VertexHelper vh){vh.Clear();float tw = Mathf.Abs(inner_lb.x - inner_rt.x);//图片的宽float th = Mathf.Abs(inner_lb.y - inner_rt.y);//图片的高float twr = tw / 2;float thr = th / 2;if (Radius < 0)Radius = 0;float radius = tw / Radius;//半径这里需要动态计算确保不会被拉伸if (radius > twr)radius = twr;if (radius < 0)radius = 0;if (TriangleNum <= 0)TriangleNum = 1;UIVertex vert = UIVertex.simpleVert;vert.color = color;//0 outer左下角vert.position = new Vector2(outer_lb.x, outer_lb.y);vh.AddVert(vert);//1 outer左上角vert.position = new Vector2(outer_lb.x, outer_rt.y);vh.AddVert(vert);//2 outer右上角vert.position = new Vector2(outer_rt.x, outer_rt.y);vh.AddVert(vert);//3 outer右下角vert.position = new Vector2(outer_rt.x, outer_lb.y);vh.AddVert(vert);//4 inner左下角vert.position = new Vector3(inner_lb.x, inner_lb.y);vh.AddVert(vert);//5 inner左上角vert.position = new Vector3(inner_lb.x, inner_rt.y);vh.AddVert(vert);//6 inner右上角vert.position = new Vector3(inner_rt.x, inner_rt.y);vh.AddVert(vert);//7 inner右下角vert.position = new Vector3(inner_rt.x, inner_lb.y);vh.AddVert(vert);//不显示镂空if (ShowHollowOut == false){vh.AddTriangle(0, 1, 2);vh.AddTriangle(2, 3, 0);return;}//绘制三角形vh.AddTriangle(0, 1, 4);vh.AddTriangle(1, 4, 5);vh.AddTriangle(1, 5, 2);vh.AddTriangle(2, 5, 6);vh.AddTriangle(2, 6, 3);vh.AddTriangle(6, 3, 7);vh.AddTriangle(4, 7, 3);vh.AddTriangle(0, 4, 3);//内四边形顶点List<Vector2> commonPointOutside = new List<Vector2>();//共点列表 Vector2 point1 = new Vector2(inner_lb.x, inner_lb.y);//左下共点Vector2 point2 = new Vector2(inner_lb.x, inner_lb.y + radius);//决定首次旋转方向的点commonPointOutside.Add(point1);commonPointOutside.Add(point2);point1 = new Vector2(inner_lb.x, inner_rt.y);//左上共点point2 = new Vector2(inner_lb.x + radius, inner_rt.y);commonPointOutside.Add(point1);commonPointOutside.Add(point2);point1 = new Vector2(inner_rt.x, inner_rt.y);//右上共点point2 = new Vector2(inner_rt.x, inner_rt.y - radius);commonPointOutside.Add(point1);commonPointOutside.Add(point2);point1 = new Vector2(inner_rt.x, inner_lb.y);//右下共点point2 = new Vector2(inner_rt.x - radius, inner_lb.y);commonPointOutside.Add(point1);commonPointOutside.Add(point2);//圆心公共点List<Vector2> commonPoint = new List<Vector2>();//共点列表 point1 = new Vector2(inner_lb.x + radius, inner_lb.y + radius);//左下共点point2 = new Vector2(inner_lb.x, inner_lb.y + radius);//决定首次旋转方向的点commonPoint.Add(point1);commonPoint.Add(point2);point1 = new Vector2(inner_lb.x + radius, inner_rt.y - radius);//左上共点point2 = new Vector2(inner_lb.x + radius, inner_rt.y);commonPoint.Add(point1);commonPoint.Add(point2);point1 = new Vector2(inner_rt.x - radius, inner_rt.y - radius);//右上共点point2 = new Vector2(inner_rt.x, inner_rt.y - radius);commonPoint.Add(point1);commonPoint.Add(point2);point1 = new Vector2(inner_rt.x - radius, inner_lb.y + radius);//右下共点point2 = new Vector2(inner_rt.x - radius, inner_lb.y);commonPoint.Add(point1);commonPoint.Add(point2);Vector2 pos2;float degreeDelta = (float)(Mathf.PI / 2 / TriangleNum);//每一份等腰三角形的角度 默认6份List<float> degreeDeltaList = new List<float>() { Mathf.PI, Mathf.PI / 2, 0, (float)3 / 2 * Mathf.PI };for (int j = 0; j < commonPoint.Count; j += 2){float curDegree = degreeDeltaList[j / 2];//当前的角度AddVert(commonPointOutside[j], tw, th, vh, vert);//添加扇形区域所有三角形公共顶点//TODO :这里改成外面的点int thrdIndex = vh.currentVertCount;//当前三角形第二顶点索引int TriangleVertIndex = vh.currentVertCount - 1;//一个扇形保持不变的顶点索引List<Vector2> pos2List = new List<Vector2>();for (int i = 0; i < TriangleNum; i++){curDegree += degreeDelta;if (pos2List.Count == 0){AddVert(commonPoint[j + 1], tw, th, vh, vert);}else{vert.position = pos2List[i - 1];vert.uv0 = new Vector2(pos2List[i - 1].x + 0.5f, pos2List[i - 1].y + 0.5f);}pos2 = new Vector2(commonPoint[j].x + radius * Mathf.Cos(curDegree), commonPoint[j].y + radius * Mathf.Sin(curDegree));AddVert(pos2, tw, th, vh, vert);vh.AddTriangle(TriangleVertIndex, thrdIndex, thrdIndex + 1);thrdIndex++;pos2List.Add(vert.position);}}}protected Vector2[] GetTextureUVS(Vector2[] vhs, float tw, float th){int count = vhs.Length;Vector2[] uvs = new Vector2[count];for (int i = 0; i < uvs.Length; i++){uvs[i] = new Vector2(vhs[i].x / tw + 0.5f, vhs[i].y / th + 0.5f);//矩形的uv坐标  因为uv坐标原点在左下角,vh坐标原点在中心 所以这里加0.5(uv取值范围0~1)}return uvs;}protected void AddVert(Vector2 pos0, float tw, float th, VertexHelper vh, UIVertex vert){vert.position = pos0;vert.uv0 = GetTextureUVS(new[] { new Vector2(pos0.x, pos0.y) }, tw, th)[0];vh.AddVert(vert);}/// <summary>/// 计算边界/// </summary>private void CalcBounds(){if (inner_trans == null){return;}Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(outer_trans, inner_trans);inner_rt = bounds.max;inner_lb = bounds.min;outer_rt = outer_trans.rect.max;outer_lb = outer_trans.rect.min;}private void Update(){if (realtimeRefresh == false){return;}//计算边界CalcBounds();//刷新SetAllDirty();}
}

Unity UGUI引导镂空效果,添加背景遮罩带内倒角镂空相关推荐

  1. CSS:outline的用法,用outline实现镂空效果

    1.outline的用法 outline用法和border类似, 如下: .outline {outline: 1px solid #000; } 两者表现也类似, 都是给元素的外面画框,但是,作用却 ...

  2. Unity之引导功能遮罩事件穿透

    Unity之新手引导shader遮罩事件穿透 效果图 设计思路 1.新手引导我们期待开发内容不影响正常的功能模块,意思就是分层,新手引导在正常功能之上 2.新手引导层级用一层深色bg显示遮住正常功能层 ...

  3. 为图片添加半透明遮罩效果

    平时为图片添加半透明遮罩效果,我的做法如下: 利用标签i实现背景半透明遮罩.当鼠标hover时, 提高i的背景色透明度值background-color: rgba(0, 0, 0, .6) < ...

  4. 浏览器中遮罩层镂空效果的多种实现方法

    前端开发中我们有时候会做到页面遮罩层镂空的效果,那什么是页面遮罩层镂空效果,我们先来看一看下图的这个效果.下图是我昨天在实际工作中完成的页面效果: 做这个效果的时候有以下注意的地方: 1.兼容IE7及 ...

  5. Unity UGUI基础 之 Scroll View/Scroll Rect 的简单使用,并取消拖拽(滑动内容)效果,拖拽只在Scrollbar 上起作用

    Unity UGUI基础 之 Scroll View/Scroll Rect 的简单使用,并取消拖拽(滑动内容)效果,拖拽只在Scrollbar 上起作用 目录 Unity UGUI基础 之 Scro ...

  6. HTML怎么制作镂空文字遮罩,用纯 CSS 实现镂空效果

    背景被裁剪为文字的前景色.第一次是在 CSS-Tricks 看到的这个用法: 在 CSS-Tricks 网站上,这个玩意用得到处都是. 这样,做简单的图片背景镂空效果便不再困难了.关键代码只有几行. ...

  7. HTML怎么制作镂空文字遮罩,用纯CSS实现镂空效果的示例代码

    近来研究了一下镂空效果. background-clip: text 背景被裁剪为文字的前景色.第一次是在 CSS-Tricks 看到的这个用法: 在 CSS-Tricks 网站上,这个玩意用得到处都 ...

  8. Unity UGUI 效果 之 UI 元素 多边形UI (例如雷达图,圆形,不规则多边形 UI等)显示 的简单实现的几种方法整理

    Unity UGUI 效果 之 UI 元素 多边形UI (例如雷达图,圆形,不规则多边形 UI等)显示 的简单实现的几种方法整理 目录 Unity UGUI 效果 之 UI 元素 多边形UI (例如雷 ...

  9. Unity UGUI 之 实现按钮 Button 长按和双击的功能效果

    Unity UGUI 之 实现按钮 Button 长按和双击的功能效果 目录 Unity UGUI 之 实现按钮 Button 长按和双击的功能效果 一.简单介绍 二.实现原理 三.注意事项 四.效果 ...

最新文章

  1. 【点云重采样Resampling】Python-pcl 基于多项式平滑点云及法线估计的曲面重建
  2. 强行分类提取特征自编码网络例1
  3. 【Paper】2021_Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays
  4. 定义一个计算字符串有效长度的_一个正方形的小抽屉柜,根据设计草图计算出所需四片木板的长度...
  5. 关于低代码自定义表单的思路和想法
  6. 2019年这50个Kafka面试题,你知道答案么
  7. css限制字体三行_讲道理,仅3行核心css代码的rate评分组件,我被自己秀到头皮发麻...
  8. ccie计算机网络英语,最新版CCIE Voice 语音方向 WOLF实验全套语音课程(含版本)...
  9. ICCV2021-PiT-池化操作不是CNN的专属,ViT说:“我也可以”;南大提出池化视觉Transformer(PiT)...
  10. 贺利坚老师汇编课程51笔记:MUL乘法指令
  11. JS根据城市名称获取所在省份
  12. SecureCRT 关键字配色显示
  13. 炒作与现实的博弈,厂商大肆宣传自动驾驶致司机陷入危险
  14. 443端口与80端口的区别
  15. css中实现三角形的几种方式
  16. mysql联合索引和索引优化的理解
  17. HTML中nbsp; ensp; emsp; thinsp; zwnj; zwj;等6种空白空格的区别
  18. IntelliJ IDEA远程调试设置
  19. 推荐一些国内的jQuery CDN免费服务[转]
  20. 哈工大计算机学院博士毕业 要求,哈尔滨工业大学博士毕业要求.doc

热门文章

  1. 软件包被拦截、删除、无法运行,,,卸载掉自带杀毒软件Windows Defender、关闭防火墙,,,网上各种办法都试过了,不起作用。。。最后一招解决
  2. 圣诞节,中国开源世界炸锅了@阿里巴巴
  3. 【ORB-SLAM2配置】使用USB摄像头运行ORB-SLAM2
  4. 使用纯QML写的模仿的酷狗音乐播放器
  5. 一致性成本和非一致性成本
  6. Android 中替换开机动画(附动画包)
  7. 使用Qt5.9实现qq聊天功能(TCP通信)
  8. 摄像头无线传输到服务器方案,智能无线微型咳嗽WIFI摄像头的硬件方案
  9. 钉钉被叫停,影响不到VOIP,个人免费电话仍可继续
  10. 工具分享(1):FTP暴力破解工具 [Python多线程版]