解决方案转载于:UGUI——重写Image类实现进度条 作者:糯米团子滚呀滚

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Sprites;public class childImage :  Image{protected override void OnPopulateMesh(VertexHelper toFill){base.OnPopulateMesh(toFill);if (overrideSprite == null){base.OnPopulateMesh(toFill);return;}if (type == Type.Sliced){GenerateSlicedSprite_(toFill);}}Vector4 GetAdjustedBorders(Vector4 border, Rect rect){for (int axis = 0; axis <= 1; axis++){// If the rect is smaller than the combined borders, then there's not room for the borders at their normal size.// In order to avoid artefacts with overlapping borders, we scale the borders down to fit.float combinedBorders = border[axis] + border[axis + 2];if (rect.size[axis] < combinedBorders && combinedBorders != 0){float borderScaleRatio = rect.size[axis] / combinedBorders;border[axis] *= borderScaleRatio;border[axis + 2] *= borderScaleRatio;}}return border;}static void AddQuad(VertexHelper vertexHelper, Vector2 posMin, Vector2 posMax, Color32 color, Vector2 uvMin, Vector2 uvMax){int startIndex = vertexHelper.currentVertCount;vertexHelper.AddVert(new Vector3(posMin.x, posMin.y, 0), color, new Vector2(uvMin.x, uvMin.y));vertexHelper.AddVert(new Vector3(posMin.x, posMax.y, 0), color, new Vector2(uvMin.x, uvMax.y));vertexHelper.AddVert(new Vector3(posMax.x, posMax.y, 0), color, new Vector2(uvMax.x, uvMax.y));vertexHelper.AddVert(new Vector3(posMax.x, posMin.y, 0), color, new Vector2(uvMax.x, uvMin.y));vertexHelper.AddTriangle(startIndex, startIndex + 1, startIndex + 2);vertexHelper.AddTriangle(startIndex + 2, startIndex + 3, startIndex);}private void GenerateSlicedSprite_(VertexHelper toFill){Vector4 outer, inner, padding, border;if (overrideSprite != null){outer = DataUtility.GetOuterUV(overrideSprite);inner = DataUtility.GetInnerUV(overrideSprite);padding = DataUtility.GetPadding(overrideSprite);border = overrideSprite.border;}else{outer = Vector4.zero;inner = Vector4.zero;padding = Vector4.zero;border = Vector4.zero;}Rect rect = GetPixelAdjustedRect();border = GetAdjustedBorders(border / pixelsPerUnit, rect);padding = padding / pixelsPerUnit;float condition = (border.z + border.x) / rect.width;#region 实际显示sizefloat[] x={0,0,0,0};x[0] = 0;if (fillAmount <condition){x[1] = fillAmount / 2 * rect.width;x[2] = x[1]+ 0;x[3] = x[1]*2;}else{x[1] = border.x;x[2] = rect.width *fillAmount-border.z;x[3] =x[2]+border.z;}float []y ={0+rect.y,rect.height+rect.y};for (int i = 0; i < 4; ++i){x[i] += rect.x;}#endregion#region uv值float[] x_uv = {0,0,0,0 };x_uv[0] =0;if (fillAmount <condition){x_uv[1] = fillAmount*rect.width/2/sprite.rect.size.x;x_uv[2] = 1 - x_uv[1];}else{x_uv[1] = inner.x;x_uv[2] = inner.z;}x_uv[3] = outer.z;float y_uv = 1;#endregiontoFill.Clear();for (int i = 0; i < 3; i++){int i2 = i + 1;AddQuad(toFill,new Vector2(x[i],y[0]),new Vector2(x[i2],y[1]),color,new Vector2(x_uv[i],0),new Vector2(x_uv[i2],y_uv));}}}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.UI;
using UnityEditor;
using UnityEditor.AnimatedValues;
using System.Linq;[CustomEditor(typeof(childImage))]
public class ChildchildImageInspector : ImageEditor
{SerializedProperty m_FillMethod;SerializedProperty m_FillOrigin;SerializedProperty m_FillAmount;SerializedProperty m_FillClockwise;SerializedProperty m_Type;SerializedProperty m_FillCenter;SerializedProperty m_Sprite;SerializedProperty m_PreserveAspect;GUIContent m_SpriteContent;GUIContent m_SpriteTypeContent;GUIContent m_ClockwiseContent;AnimBool m_ShowSlicedOrTiled;AnimBool m_ShowSliced;AnimBool m_ShowFilled;AnimBool m_ShowType;void SetShowNativeSize(bool instant){childImage.Type type = (childImage.Type)m_Type.enumValueIndex;bool showNativeSize = (type == childImage.Type.Simple || type == childImage.Type.Filled);base.SetShowNativeSize(showNativeSize, instant);}protected override void OnEnable(){base.OnEnable();m_SpriteContent = new GUIContent("Source childImage");m_SpriteTypeContent = new GUIContent("childImage Type");m_ClockwiseContent = new GUIContent("Clockwise");m_Sprite = serializedObject.FindProperty("m_Sprite");m_Type = serializedObject.FindProperty("m_Type");m_FillCenter = serializedObject.FindProperty("m_FillCenter");m_FillMethod = serializedObject.FindProperty("m_FillMethod");m_FillOrigin = serializedObject.FindProperty("m_FillOrigin");m_FillClockwise = serializedObject.FindProperty("m_FillClockwise");m_FillAmount = serializedObject.FindProperty("m_FillAmount");m_PreserveAspect = serializedObject.FindProperty("m_PreserveAspect");m_ShowType = new AnimBool(m_Sprite.objectReferenceValue != null);m_ShowType.valueChanged.AddListener(Repaint);var typeEnum = (childImage.Type)m_Type.enumValueIndex;m_ShowSlicedOrTiled = new AnimBool(!m_Type.hasMultipleDifferentValues && typeEnum == childImage.Type.Sliced);m_ShowSliced = new AnimBool(!m_Type.hasMultipleDifferentValues && typeEnum == childImage.Type.Sliced);m_ShowFilled = new AnimBool(!m_Type.hasMultipleDifferentValues && typeEnum == childImage.Type.Filled);m_ShowSlicedOrTiled.valueChanged.AddListener(Repaint);m_ShowSliced.valueChanged.AddListener(Repaint);m_ShowFilled.valueChanged.AddListener(Repaint);SetShowNativeSize(true);}public override void OnInspectorGUI(){serializedObject.Update();SpriteGUI();AppearanceControlsGUI();RaycastControlsGUI();m_ShowType.target = m_Sprite.objectReferenceValue != null;if (EditorGUILayout.BeginFadeGroup(m_ShowType.faded)){EditorGUILayout.PropertyField(m_Type, m_SpriteTypeContent);++EditorGUI.indentLevel;{childImage.Type typeEnum = (childImage.Type)m_Type.enumValueIndex;bool showSlicedOrTiled = (!m_Type.hasMultipleDifferentValues && (typeEnum ==childImage.Type.Sliced|| typeEnum == childImage.Type.Tiled));if (showSlicedOrTiled && targets.Length > 1)showSlicedOrTiled = targets.Select(obj => obj as childImage).All(img => img.hasBorder);m_ShowSlicedOrTiled.target = showSlicedOrTiled;m_ShowSliced.target = (showSlicedOrTiled && !m_Type.hasMultipleDifferentValues && typeEnum == childImage.Type.Sliced);m_ShowFilled.target = (!m_Type.hasMultipleDifferentValues && typeEnum == childImage.Type.Filled);childImage cImage = target as childImage;if (EditorGUILayout.BeginFadeGroup(m_ShowSlicedOrTiled.faded)){if (cImage.hasBorder){EditorGUILayout.PropertyField(m_FillCenter);EditorGUILayout.PropertyField(m_FillAmount);}}EditorGUILayout.EndFadeGroup();if (EditorGUILayout.BeginFadeGroup(m_ShowSliced.faded)){if (cImage.sprite != null && !cImage.hasBorder)EditorGUILayout.HelpBox("This childImage doesn't have a border.", MessageType.Warning);}EditorGUILayout.EndFadeGroup();if (EditorGUILayout.BeginFadeGroup(m_ShowFilled.faded)){EditorGUI.BeginChangeCheck();EditorGUILayout.PropertyField(m_FillMethod);if (EditorGUI.EndChangeCheck()){m_FillOrigin.intValue = 0;}switch ((childImage.FillMethod)m_FillMethod.enumValueIndex){case childImage.FillMethod.Horizontal:m_FillOrigin.intValue = (int)(childImage.OriginHorizontal)EditorGUILayout.EnumPopup("Fill Origin", (childImage.OriginHorizontal)m_FillOrigin.intValue);break;case childImage.FillMethod.Vertical:m_FillOrigin.intValue = (int)(childImage.OriginVertical)EditorGUILayout.EnumPopup("Fill Origin", (childImage.OriginVertical)m_FillOrigin.intValue);break;case childImage.FillMethod.Radial90:m_FillOrigin.intValue = (int)(childImage.Origin90)EditorGUILayout.EnumPopup("Fill Origin", (childImage.Origin90)m_FillOrigin.intValue);break;case childImage.FillMethod.Radial180:m_FillOrigin.intValue = (int)(childImage.Origin180)EditorGUILayout.EnumPopup("Fill Origin", (childImage.Origin180)m_FillOrigin.intValue);break;case childImage.FillMethod.Radial360:m_FillOrigin.intValue = (int)(childImage.Origin360)EditorGUILayout.EnumPopup("Fill Origin", (childImage.Origin360)m_FillOrigin.intValue);break;}EditorGUILayout.PropertyField(m_FillAmount);if ((childImage.FillMethod)m_FillMethod.enumValueIndex > childImage.FillMethod.Vertical){EditorGUILayout.PropertyField(m_FillClockwise, m_ClockwiseContent);}}EditorGUILayout.EndFadeGroup();}--EditorGUI.indentLevel;}EditorGUILayout.EndFadeGroup();SetShowNativeSize(false);if (EditorGUILayout.BeginFadeGroup(m_ShowNativeSize.faded)){EditorGUI.indentLevel++;EditorGUILayout.PropertyField(m_PreserveAspect);EditorGUI.indentLevel--;}EditorGUILayout.EndFadeGroup();NativeSizeButtonGUI();serializedObject.ApplyModifiedProperties();}
}

【Unity3D】制作进度条——让Image同时具有Filled和Sliced的功能相关推荐

  1. 组态王怎么做进度条_三种方法制作进度条效果

    进度条可以说出现在我们生活的方方面面,游戏.视频加载会碰到它,刷新会碰到它,就连网络不顺畅时也会碰到它.进度条不仅仅只是作为信息载入时的标志,还可以运用到片头开场,让观众对接下来的视频内容产生好奇和期 ...

  2. JavaScript计时器制作进度条

    文章目录 前言 一.效果图 二.代码 1.html代码 2.css代码 3.script代码 总结 前言 学习了计时器函数的用法可以用来制作载入进度条,文章分为3部分代码:html.css.scrip ...

  3. python制作进度条显示_Python进度条的制作代码实例

    这篇文章主要介绍了Python进度条的制作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 import sys,time #导入模块 for ...

  4. js - 预加载+监听图片资源加载制作进度条

    这两天遇到一个新需求:一个一镜到底的h5动画.因为功能的特殊性,就要求我们提前监听页面的静态图片是否全部加载完毕.即处理预加载. 总结下来,下次这种需求需要提前注意以下几点: 一.图片而不是背景图 本 ...

  5. 教你用JavaScript制作进度条

    案例介绍 欢迎来到我的小院,我是霍大侠,恭喜你今天又要进步一点点了! 我们来用JavaScript编程实战案例,做一个进度条.进度条数字自动增加,条状图片动画演示进度完成度.通过实战我们将学会函数fu ...

  6. 利用canvas制作进度条实践

    开门见字 之前写过一篇H5新标签progress进度条的使用文章,当时觉得研究的还行,但是发现,这个标签在不同浏览器中样式表现是不一致的,如下: chrome中:,一抹蓝色划过天际: 火狐中:,一片绿 ...

  7. 使用CSS渐变制作进度条

    效果图: HTML结构 <div class="container"> <div class="inner"></div>& ...

  8. html5 video如何添加进度条_教你制作独一无二的进度条视频效果

    VLOG开头经常都会看到进度条,爱心/星星/emoji表情都可以做成进度加载的效果.这究竟是怎么做出来的呢?今天喵酱为大家带来3种方法,轻松制作进度条效果.认真地一步一步查看,保证你也能学会!先来看看 ...

  9. Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条(三十一)

    异步任务相信大家应该不会陌生,那么本章内容MOMO将带领大家学习Unity中的一些异步任务.在同步加载游戏场景的时候通常会使用方法 Application.LoadLevel("yourSc ...

  10. Unity进度条制作

    在制作进度条时,可先准备Image背景图片,基本UI层次结构如下图:准备空节点,取名LoadingWnd,铺满整个界面,在下面加入背景(bg).加入提示信息(TextTips)下面正式制作进度条. 1 ...

最新文章

  1. android 有效载荷大图,避OOM
  2. (0063)iOS开发之SVN server的搭建
  3. threejs 纹理流动_Threejs多重纹理与过程纹理实现
  4. docker-compose的介绍与安装(结合官方文档)
  5. macbook pro启用root的方法
  6. habernet备份mysql_harbor 安装
  7. RPG游戏开发基础教程
  8. python 英语翻译 excel_Python翻译Excel文件
  9. STC 芯片编写串口数据方法。
  10. 基于内外环PD控制的四旋翼飞行器控制系统simulink仿真
  11. 微信支付获取用户真实ip
  12. 安全组-出入战规则设置
  13. 计算机自动关机启机唤醒设置,电脑定时自动关机怎么设置
  14. springboot聚合工程打包报错Compilation failure浅析
  15. 神武服务器维护打副本,9月9日服务器例行维护公告
  16. 华东师范大学 计算机 博士 毕业论文,华东师大:1/4博士生完不成论文难毕业
  17. WebView网页视频统一全屏播放及横竖屏切换
  18. oa系统 云服务器配置,oa系统云服务器配置
  19. 调试多线程 查死锁的bug gcore命令 gdb对多线程的调试 gcore pstack 调试常用命令...
  20. 解决Flutter Android sdkmanager tool not found

热门文章

  1. python — numpy计算矩阵特征值,特征向量
  2. numpy求矩阵特征值与特征向量
  3. Newtonsoft.Json.Linq 简单使用
  4. 地理信息三维可视化技术在城市规划中的应用
  5. Quitting an application - is that frowned upon?
  6. PLUS模型教程2:数据前期准备和土地利用数量预测
  7. 阿里云服务器试用是否划算
  8. [英文邮件写作技巧] 表达感谢,提出问题,描述附件
  9. dmp(数据管理平台)是什么?
  10. Oracle数据泵导入导出dmp数据文件详细教程