Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by · Dec 27, 2015 at 09:24 AM · textcoloruser interfaceverticesgradient

UI Text 2 Color Horizontal Fill With Sliding Dividing Line

I have a UI text object where I would like the following things to happen:

  1. Apply a 2 color fill to the text, where the left side of the phrase will be one color, and the right side of the phrase will be another.

  2. Make the dividing line between the two colors adjustable, so that I can position the color transition anywhere in the phrase, even in the middle of a character.

  3. Animate this dividing line so that the color of the text transitions from one to the other by sliding the transition from left to right.

Scouring the internet, I was able to to find the code below that will generate a gradient across a UI text object, however, it is a smooth gradient, whereas I need a sharp transition. I spent a few hours trying to find ways to modify it to what I need, but I not experienced enough in Unity and Vertices to do so.

If anyone can point me in the correct direction, that would be appreciated. If there is another, or easier way of accomplishing the 3 points above, I would love to know that too.

Thanks!

 using UnityEngine;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 [AddComponentMenu("UI/Effects/Gradient")]
 public class Gradient: BaseMeshEffect
 {
     public enum Type
     {
         Vertical,
         Horizontal
     }
     [SerializeField]
     public Type GradientType = Type.Vertical;
 
     [SerializeField]
     [Range(-1.5f, 1.5f)]
     public float Offset = 0f;
 
     [SerializeField]
     private Color32 StartColor = Color.white;
     [SerializeField]
     private Color32 EndColor = Color.black;
 
     public override void ModifyMesh(VertexHelper helper)
     {
         if (!IsActive() || helper.currentVertCount == 0)
             return;
                 
         List<UIVertex> _vertexList = new List<UIVertex>();
         helper.GetUIVertexStream(_vertexList);
 
         int nCount = _vertexList.Count;
         switch (GradientType)
         {
             case Type.Vertical:
                 {
                     float fBottomY = _vertexList[0].position.y;
                     float fTopY = _vertexList[0].position.y;
                     float fYPos = 0f;
 
                     for (int i = nCount - 1; i >= 1; --i)
                     {
                         fYPos = _vertexList[i].position.y;
                         if (fYPos > fTopY)
                             fTopY = fYPos;
                         else if (fYPos < fBottomY)
                             fBottomY = fYPos;
                     }
 
                     float fUIElementHeight = 1f / (fTopY - fBottomY);
                     UIVertex v = new UIVertex();
 
                     for (int i = 0; i < helper.currentVertCount; i++)
                     {
                         helper.PopulateUIVertex(ref v, i);
                         v.color = Color32.Lerp(EndColor, StartColor,  (v.position.y - fBottomY) * fUIElementHeight - Offset);
                         helper.SetUIVertex(v, i);
                     }
                 }
                 break;
             case Type.Horizontal:
                 {
                     float fLeftX = _vertexList[0].position.x;
                     float fRightX = _vertexList[0].position.x;
                     float fXPos = 0f;
 
                     for (int i = nCount - 1; i >= 1; --i)
                     {
                         fXPos = _vertexList[i].position.x;
                         if (fXPos > fRightX)
                             fRightX = fXPos;
                         else if (fXPos < fLeftX)
                             fLeftX = fXPos;
                     }
 
                     float fUIElementWidth = 1f / (fRightX - fLeftX);
                     UIVertex v = new UIVertex();                                       
 
                     for (int i = 0; i < helper.currentVertCount; i++)
                     {
                         helper.PopulateUIVertex(ref v, i);
                         v.color = Color32.Lerp(EndColor, StartColor, (v.position.x - fLeftX) * fUIElementWidth - Offset);
                         helper.SetUIVertex(v, i);
                     }                                       
                 }
                 break;
             default:
                 break;
         }
     }
 }



Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image · Dec 29, 2015 at 07:14 PM 0
Share

I spent some more time trying to figure this out, and still can't get it resolved. Every solution seems to work around the vertices already applied to the text. It might require adding or modifying the vertices, which I don't know how to do.

I appreciate any help.

Thanks!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Gradient Text in Unity 5.2.2 "BaseVertexEffect is obsolete use BaseMeshEffect instead" 3 Answers

Color.white not so much white... 3 Answers

changing text color in GUI.Label 3 Answers

Vertex colour based on distance from transform center 2 Answers

How can I create a gradient color background for my game, without using skybox? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges