当前位置:   article > 正文

Unity实现Image透明度渐变(Graphic)_unity image毛玻璃

unity image毛玻璃

实现原理


通过Graphic中OnPopulateMesh函数修改UI传入的顶点数据(color属性中的Alpha值)

UI传入的四个顶点如下:
在这里插入图片描述

OnPopulateMesh函数

当一个UI元素生成顶点数据时会调用OnPopulateMesh(VertexHelper vh)函数,我们可以在这个函数中修改顶点的数据或者获取顶点的数据。

代码实现


FadeImage :

using UnityEngine;
using UnityEngine.UI;

public enum FadeMode
{
	LeftTORight,
	UpToDown,
	LeftUpTORightDown,
	LeftDownTORightUp,
}

public class FadeImage : Image
{
	public FadeMode FadeMode;
	public byte FadeStartAlpha = 0;
	public byte FadeEndAlpha = 255;

	protected override void OnPopulateMesh(VertexHelper toFill)
	{
		base.OnPopulateMesh(toFill);
		UIVertex vertex = new UIVertex();
		for (int i = 0; i < toFill.currentVertCount; i++)
		{
			toFill.PopulateUIVertex(ref vertex, i);

			if (FadeMode == FadeMode.UpToDown)
			{
				if (i == 0 || i == 3)
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha);
					toFill.SetUIVertex(vertex, i);
				}
				else
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha);
					toFill.SetUIVertex(vertex, i);
				}
			}
			else if (FadeMode == FadeMode.LeftTORight)
			{
				if (i == 0 || i == 1)
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha);
					toFill.SetUIVertex(vertex, i);
				}
				else
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha);
					toFill.SetUIVertex(vertex, i);
				}
			}
			else if (FadeMode == FadeMode.LeftDownTORightUp)
			{
				if (i == 0)
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha);
					toFill.SetUIVertex(vertex, i);
				}
				else if (i == 1 || i == 3)
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, (byte)((FadeEndAlpha + FadeStartAlpha) / 2));
					toFill.SetUIVertex(vertex, i);
				}
				else
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha);
					toFill.SetUIVertex(vertex, i);
				}
			}
			else if (FadeMode == FadeMode.LeftUpTORightDown)
			{
				if (i == 1)
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeStartAlpha);
					toFill.SetUIVertex(vertex, i);
				}
				else if (i == 0 || i == 2)
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, (byte)((FadeEndAlpha + FadeStartAlpha) / 2));
					toFill.SetUIVertex(vertex, i);
				}
				else
				{
					vertex.color = new Color32(vertex.color.r, vertex.color.g, vertex.color.b, FadeEndAlpha);
					toFill.SetUIVertex(vertex, i);
				}
			}
		}
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

FadeImageEditor:

using UnityEditor;
using UnityEditor.UI;

[CustomEditor(typeof(FadeImage))]
public class FadeImageEditor : ImageEditor
{
    protected SerializedProperty FadeMode;
    protected SerializedProperty FadeStartAlpha;
    protected SerializedProperty FadeEndAlpha;

    protected override void OnEnable()
    {
        base.OnEnable();
        FadeMode = serializedObject.FindProperty("FadeMode");
        FadeStartAlpha = serializedObject.FindProperty("FadeStartAlpha");
        FadeEndAlpha = serializedObject.FindProperty("FadeEndAlpha");
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        serializedObject.Update();
        EditorGUILayout.PropertyField(FadeMode);
        EditorGUILayout.PropertyField(FadeStartAlpha);
        EditorGUILayout.PropertyField(FadeEndAlpha);
        serializedObject.ApplyModifiedProperties();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

使用效果


在这里插入图片描述
在这里插入图片描述


赞赏与支持


【收藏与点赞】
觉得写的不错的可以收藏点赞转发噢~

【请作者喝杯咖啡】
觉得写的不错的可以请作者喝杯咖啡噢~
在这里插入图片描述


(完)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/116328?site
推荐阅读
相关标签
  

闽ICP备14008679号