【Unity3D】 DoTween实现飘字的效果

Sequence.Append构建缓动序列,同时Join方法支持并行缓动。利用这个特性,可以实现ugui—Text的飘字缓动效果。

Append是在序列的末端插入一个Tweener,如果前面的Tweener都执行完了,就执行这个Tweener。

Join也是在序列末端插入一个Tweener,不同的是,这个Tweener将与前一个非Join加进来的Tweener并行执行。

飘字效果代码:

public static void FlyTo(Graphic graphic)
{
	RectTransform rt = graphic.rectTransform;
	Color c = graphic.color;
	c.a = 0;
	graphic.color = c;
	Sequence mySequence = DOTween.Sequence();
	Tweener move1 = rt.DOMoveY(rt.position.y + 50, 0.5f);
	Tweener move2 = rt.DOMoveY(rt.position.y + 100, 0.5f);
	Tweener alpha1 = graphic.DOColor(new Color(c.r, c.g, c.b, 1), 0.5f);
	Tweener alpha2 = graphic.DOColor(new Color(c.r, c.g, c.b, 0), 0.5f);
	mySequence.Append(move1);
	mySequence.Join(alpha1);
	mySequence.AppendInterval(1);
	mySequence.Append(move2);
	mySequence.Join(alpha2);
}

调用

Text text = gameObject.GetComponent();
FlyTo(text);

1.首先设置文字的alpha值为0

2.然后文字沿y轴向上缓动,同时alpha从0缓动到1,两个Tweener同时进行

3.停留1秒钟,让玩家看清楚字写的是什么

4.文字继续往上飘,同时alpha从1缓动到0,逐渐消失


效果:

【Unity3D】 DoTween实现飘字的效果_第1张图片




有同学问,这个字体颜色渐变效果怎么弄,这里可以参考雨松MOMO的文章http://www.xuanyusong.com/archives/3471,但是要稍微修改设置color的部分,alpha值不能设进去,否则我们这里的渐变效果就出不来了。代码我就贴出来吧。另外,再加个Outline的效果就很好看了。

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.UI;

namespace MyScripts
{
	[AddComponentMenu("UI/Effects/Gradient")]
	public class Gradient : BaseVertexEffect
	{
		[SerializeField]
		private Color32 topColor = Color.white;
		[SerializeField]
		private Color32 bottomColor = new Color32(255, 153, 0, 255);
		[SerializeField]
		private bool useGraphicAlpha = true;

		public override void ModifyVertices(List vertexList)
		{
			if (!IsActive())
			{
				return;
			}

			int count = vertexList.Count;
			if (count > 0)
			{
				float bottomY = vertexList[0].position.y;
				float topY = vertexList[0].position.y;

				for (int i = 1; i < count; i++)
				{
					float y = vertexList[i].position.y;
					if (y > topY)
					{
						topY = y;
					}
					else if (y < bottomY)
					{
						bottomY = y;
					}
				}

				float uiElementHeight = topY - bottomY;

				for (int i = 0; i < vertexList.Count; )
				{
					ChangeColor(ref vertexList, i, topColor);
					ChangeColor(ref vertexList, i + 1, topColor);
					ChangeColor(ref vertexList, i + 2, bottomColor);
					ChangeColor(ref vertexList, i + 3, bottomColor);
					i += 4;
				}
			}
		}

		private void ChangeColor(ref List verList, int i, Color32 color)
		{
			UIVertex uiVertex = verList[i];
			if (useGraphicAlpha)
			{
				uiVertex.color = new Color32(color.r, color.g, color.b, uiVertex.color.a);
			}
			else
			{
				uiVertex.color = color;
			}
			verList[i] = uiVertex;
		}
	}
}


你可能感兴趣的:(移动开发,Unity3D,C#)