UGUI之Text镜像效果

博客迁移

个人博客站点,欢迎访问,www.jiingfengji.tech

UGUI之Text镜像效果

先上效果图:
这里写图片描述

脚本Mirror直接挂在Text下,distance是镜像距离

Mirror脚本如下:

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

[AddComponentMenu("UI/Effects/Mirror")]
public class Mirror : BaseMeshEffect
{
	//距离,限制范围0-30
    [Range(0,30)]
    public float distance;

    public override void ModifyMesh(VertexHelper vh)
    {
        if (!IsActive() || vh.currentVertCount == 0)
        {
            return;
        }
        List<UIVertex> vertexs = new List<UIVertex>();
        vh.GetUIVertexStream(vertexs);
        UIVertex vt;
        int count = vertexs.Count;
        float miny = vertexs[0].position.y;
        float maxy = vertexs[0].position.y;
        for (int i = 1; i < count; i++)
        {
            if (vertexs[i].position.y < miny)
            {
                miny = vertexs[i].position.y;
            }
            else if (vertexs[i].position.y > maxy)
            {
                maxy = vertexs[i].position.y;
            }
        }
        float uiElementHeight = maxy - miny;
        float mirrorMinY = -maxy + 2 * miny - distance;
        Color32 top = GetComponent<Text>().color;
        Color32 bottom = new Color(top.r, top.g, top.b, 0);
        for (int i = 0; i < count; i++)
        {
            vt = vertexs[i];
            vertexs.Add(vt);
            Vector3 v = vt.position;
            v.y = -v.y + 2 * miny - distance;
            vt.position = v;
			
			//透明度效果
            vt.color = Color32.Lerp(bottom, top, (vt.position.y - mirrorMinY) / uiElementHeight);
            vertexs[i + count] = vt;
        }
        vh.Clear();
        vh.AddUIVertexTriangleStream(vertexs);
    }
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值