下面的组件做一个记录,可以使ui自己设置单个文字上下颜色,最终效果为,仿照艺术字

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

[AddComponentMenu("UI/Effect/UIColorGradient")]
public class UIColorGradient : BaseMeshEffect
{
    public static List<UIVertex> helpList = new List<UIVertex>();

private bool isText;
    private bool isCheck;

[SerializeField]
    private Color32 topColor = Color.yellow;
    [SerializeField]
    private Color32 bottomColor = Color.red;

protected override void OnEnable()
    {
        base.OnEnable();
        if (this.gameObject.GetComponent<Text>() != null)
            isText = true;
        else
           isText = false;
    }

private void ModifyMeshText(VertexHelper vertexHelper)
    {
        helpList.Clear();
        vertexHelper.GetUIVertexStream(helpList);
        int count = helpList.Count;
        if (count > 0)
        {
            for (int i = 0; i < helpList.Count;)
            {
                float bottomY = helpList[i].position.y;
                float topY = bottomY;
                float dis = 1f;
                for (int k = 1; k < 6; k++)
                {
                    float y = helpList[k + i].position.y;
                    if (y > topY)
                    {
                        topY = y;
                    }
                    else if (y < bottomY)
                    {
                        bottomY = y;
                    }
                }
                dis = topY - bottomY;
                for (int k = 0; k < 6; k++)
                {
                    UIVertex vertText = helpList[k + i];
                    vertText.color = Color32.Lerp(bottomColor, topColor, (vertText.position.y - bottomY) / dis);
                    helpList[k + i] = vertText;
                }
                i += 6;
            }
            vertexHelper.Clear();
            vertexHelper.AddUIVertexTriangleStream(helpList);
            helpList.Clear();
        }
    }

public override void ModifyMesh(VertexHelper vertexHelper)
    {
        if (!IsActive())
        {
            return;
        }

if(isText)
        {
            ModifyMeshText(vertexHelper);
            return;
        }

helpList.Clear();
        vertexHelper.GetUIVertexStream(helpList);
        int count = helpList.Count;
        if (count > 0)
        {
            Vector3 pos = helpList[0].position;

float bottomY = pos.y;
            float topY = pos.y;

for (int i = 1; i < count; i++)
            {
                float y = helpList[i].position.y;
                if (y > topY)
                {
                    topY = y;
                }
                else if (y < bottomY)
                {
                    bottomY = y;
                }
            }
            float uiElementHeight = topY - bottomY;
          
            for (int i = 0; i < count; i++)
            {
                UIVertex uIVertex = helpList[i];
                Color32 color32 = Color32.Lerp(bottomColor, topColor, (uIVertex.position.y - bottomY) / uiElementHeight);
                uIVertex.color = color32;
                helpList[i] = uIVertex;
            }
            vertexHelper.Clear();
            vertexHelper.AddUIVertexTriangleStream(helpList);
        }
        helpList.Clear();
    }
}

unity text颜色渐变相关推荐

  1. Unity设置字体颜色渐变

    unity开发程序在一些必要的的程序的时候需要实现一些简单的渐变效果,之前不太懂都是直接让美术那边出图片用图片来代替,用着用着就让工程中多了很多没有必要的图片.后面找到了一种很好优化包的方法就是直接用 ...

  2. 颜色渐变丶渲染效果类---(Unity自学笔记)

    using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;/*** 颜色渐变特效类*/ public clas ...

  3. CSS 文字,边框实现从左至右颜色渐变

    微信小程序开发交流qq群   173683895    承接微信小程序开发.扫码加微信. 1.文本从左至右颜色渐变 效果图: 2.边框从左至右颜色渐变 效果图: 实现代码: 1.文本从左至右颜色渐变实 ...

  4. html字体渐变颜色的设置颜色代码,使用CSS3实现字体颜色渐变的实现

    在使用Animation.css的时候发现它的官网字体会渐变,看了一下他的css很有趣 .site__title { color: #f35626; background-image: -webkit ...

  5. android 监听webView滑动距离和标题栏颜色渐变

    重写webView之 X5WebView import android.annotation.SuppressLint; import android.content.Context; import ...

  6. iOS 动画绘制线条颜色渐变的折线图

    效果图 .................... 概述 现状 折线图的应用比较广泛,为了增强用户体验,很多应用中都嵌入了折线图.折线图可以更加直观的表示数据的变化.网络上有很多绘制折线图的demo,有 ...

  7. 设计师妹子问:字体颜色渐变,你能实现?

    早上设计师妹子把设计稿交付过来,乍一看,上面的文字颜色渐变的,而且文字内容是动态的,也就是我们无法用图片来代替. 作为一个有责任担当的汉子,坚决不能说不行. 想起了CSS3 擅长做颜色渐变.所以赶紧查 ...

  8. mui案例:导航栏 颜色渐变

    mui导航栏 · 滚动渐变 · 代码参考 代码: <!DOCTYPE html> <html><head><meta charset="utf-8& ...

  9. CSS文字颜色渐变 - 案例篇

    (含效果图.代码)css3 文字颜色渐变 - 案例篇 效果图: demo 代码如下: <doctype HTML!> <html> <head><title& ...

  10. 文字color颜色渐变(可一直变换) - 代码篇

    文字color颜色渐变(可一直变换) - 代码篇 一.应用场景 · 举例: https://www.iconfont.cn/ 站内的 [字体图标][如下图1所示] 某些客户要求的 "炫酷拽& ...

最新文章

  1. 揭秘5G+AI时代的机器人世界!七大核心技术改变人类生活【附下载】| 智东西内参...
  2. Android开发者必须深入学习的10个应用开源项目
  3. xshell 5如何选中即复制,右键即粘贴
  4. C51 特殊功能寄存器英文全称
  5. 【IOS学习基础】OC类的相关
  6. SAP Spartacus category navigation按钮之间的间隔问题
  7. android手机如何提速,安卓手机上网如何提速
  8. 共享单车项目计划书_2020年共享单车商业计划书模板
  9. 物联网感知-分布式光纤振动传感主机实现基本原理
  10. 一道被前端忽略的基础题,不信看你会几题
  11. Bandizip下载网盘地址
  12. python画美图_Python爬虫下手,就得从高清美图开始!
  13. win11更新特别慢怎么办
  14. 好想与你天长地久,共度岁月
  15. 罗杨老师带你了解谷歌编程之夏(GSoC)活动全流程
  16. 张飞是一员粗中有细的猛将
  17. A_Prn_Barcode 应用code128码的 注意事项
  18. 山东移动用明道云赋能一线,打通业务创新最后一公里
  19. threejs进阶,管道几何体高级应用,geometry几何体应用,可拖拽改变形状的管道
  20. 京东商品详情页API接口、京东详情API接口、京东商品销量API接口、京东商品列表API接口、京东APP详情API接口、京东SKU信息接口,京东API接口

热门文章

  1. python方法怎么调用_python函数怎么调用自身?
  2. 求齐次线性方程组的基础解系matlab,MATLAB学习笔记:齐次线性方程组的基础解系...
  3. 尚乘集团联手其蛛网生态圈成员及战略伙伴布局进驻元宇宙
  4. maya中英转换插件1.3 更新发布及教程
  5. pentaho-server-ce-7.0安装配置说明
  6. ZPL条码及参数说明
  7. 时间戳 天数 php,php时间戳转天数
  8. 神秘诡异的量子世界是如何毁掉科学家三观的?
  9. 用python制作动态二维码_用Python制作动态二维码
  10. 字节跳动CEO梁汝波接棒张一鸣一年:使命是我们前进的动力