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
2
Question by TriBolt72 · Oct 22, 2015 at 08:16 PM · uitextcolorgradientunity 5.2

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

Hello! I recently upgraded Unity to 5.2.2 and ran into an issue with my current gradient script. Somebody was nice enough to upload this for everyone, so I did not write it myself.

You can find it here... https://www.reddit.com/r/Unity3D/comments/2ephku/unity_46_text_gradient_effect/

This being said, I'm not fully aware of how things work with BaseVertexEffect or how to move it over to BaseMeshEffect. I'm fairly new to programming, so I struggle to find solutions to stuff like this. I saw a post about it here...

http://forum.unity3d.com/threads/basevertexeffect-change-to-basemesheffect.338455/

There is a suggestion offered that seemed to work for some people, but I am not sure how to apply that to the script I'm using. I don't know if it would work for mine, or if there is a better script I should be using. Any help is appreciated. Thanks.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
11
Best Answer

Answer by koshik · Oct 23, 2015 at 11:56 AM

I just hit the same issue and here's my update:

 using UnityEngine;
 using System.Collections.Generic;
 using UnityEngine.UI;
 
 [AddComponentMenu("UI/Effects/Gradient")]
 public class Gradient : BaseMeshEffect
 {
     public Color32 topColor = Color.white;
     public Color32 bottomColor = Color.black;
 
     public override void ModifyMesh(VertexHelper helper)
     {
         if (!IsActive() || helper.currentVertCount == 0)
             return;
 
         List<UIVertex> vertices = new List<UIVertex>();
         helper.GetUIVertexStream(vertices);
 
         float bottomY = vertices[0].position.y;
         float topY = vertices[0].position.y;
 
         for (int i = 1; i < vertices.Count; i++)
         {
             float y = vertices[i].position.y;
             if (y > topY)
             {
                 topY = y;
             }
             else if (y < bottomY)
             {
                 bottomY = y;
             }
         }
 
         float uiElementHeight = topY - bottomY;
 
         UIVertex v = new UIVertex();
 
         for (int i = 0; i < helper.currentVertCount; i++)
         {
             helper.PopulateUIVertex(ref v, i);
             v.color = Color32.Lerp(bottomColor, topColor, (v.position.y - bottomY) / uiElementHeight);
             helper.SetUIVertex(v, i);
         }
     }
 }
Comment
Add comment · Show 4 · Share
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 TriBolt72 · Oct 23, 2015 at 06:54 PM 0
Share

Thank you so much! It works perfectly.

avatar image SpacePilot1000 · Nov 03, 2015 at 03:23 AM 0
Share

I'd also like to say thanks, this is a good update!

avatar image rogerluiz · Nov 20, 2015 at 02:07 PM 0
Share

Thanks man, works fine

avatar image rrabi · Apr 30, 2016 at 12:58 AM 0
Share

Thankyou!!

avatar image
3

Answer by DanielEsposito · Nov 25, 2015 at 08:56 PM

More optional script:

 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 5 · Share
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 koshik · Nov 26, 2015 at 09:38 AM 0
Share

$$anonymous$$y goal was just a quick fix - thanks for extending it :)

I noticed that after this update each letter is rendered as a separate mesh (previously the the whole text was rendered on one rectangle). That gives even more possibilities!

avatar image Amonsat · Apr 01, 2016 at 09:17 AM 0
Share

It's work with 5.3. But override "shadow" and "ouline" effects.

avatar image adrianDD Amonsat · Apr 02, 2016 at 08:01 PM 0
Share

$$anonymous$$ove the Gradient component above Shadow and Outline in the object's Inspector.

avatar image Amonsat adrianDD · Apr 05, 2016 at 11:51 AM 0
Share

Thanx. It work

avatar image DaveF · Apr 18, 2016 at 09:49 PM 0
Share

Any chance per-line gradient is possible, ins$$anonymous$$d of gradient over entire paragraph?

avatar image
1

Answer by GibTreaty · Apr 30, 2016 at 07:19 PM

Modified @DanielEsposito's version to use Unity's Gradient class for the coloring/alpha and added a few blend modes so that the colors can blend with other effects.

I've also put it on pastebin http://pastebin.com/WwUh3eZg

 using System.Collections.Generic;
 
 /// <summary>
 /// Modified Gradient effect script from http://answers.unity3d.com/questions/1086415/gradient-text-in-unity-522-basevertexeffect-is-obs.html
 /// -Uses Unity's Gradient class to define the color
 /// -Offset is now limited to -1,1
 /// -Multiple color blend modes
 /// 
 /// Remember that the colors are applied per-vertex so if you have multiple points on your gradient where the color changes and there aren't enough vertices, you won't see all of the colors.
 /// </summary>
 namespace UnityEngine.UI {
     [AddComponentMenu("UI/Effects/Gradient")]
     public class Gradient : BaseMeshEffect {
         [SerializeField]
         Type _gradientType;
 
         [SerializeField]
         Blend _blendMode = Blend.Multiply;
 
         [SerializeField]
         [Range(-1, 1)]
         float _offset = 0f;
 
         [SerializeField]
         UnityEngine.Gradient _effectGradient = new UnityEngine.Gradient() { colorKeys = new GradientColorKey[] { new GradientColorKey(Color.black, 0), new GradientColorKey(Color.white, 1) } };
 
         #region Properties
         public Blend BlendMode {
             get { return _blendMode; }
             set { _blendMode = value; }
         }
 
         public UnityEngine.Gradient EffectGradient {
             get { return _effectGradient; }
             set { _effectGradient = value; }
         }
 
         public Type GradientType {
             get { return _gradientType; }
             set { _gradientType = value; }
         }
 
         public float Offset {
             get { return _offset; }
             set { _offset = value; }
         }
         #endregion
 
         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.Horizontal: {
                         float left = _vertexList[0].position.x;
                         float right = _vertexList[0].position.x;
                         float x = 0f;
 
                         for(int i = nCount - 1; i >= 1; --i) {
                             x = _vertexList[i].position.x;
 
                             if(x > right) right = x;
                             else if(x < left) left = x;
                         }
 
                         float width = 1f / (right - left);
                         UIVertex vertex = new UIVertex();
 
                         for(int i = 0; i < helper.currentVertCount; i++) {
                             helper.PopulateUIVertex(ref vertex, i);
 
                             vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.x - left) * width - Offset));
 
                             helper.SetUIVertex(vertex, i);
                         }
                     }
                     break;
 
                 case Type.Vertical: {
                         float bottom = _vertexList[0].position.y;
                         float top = _vertexList[0].position.y;
                         float y = 0f;
 
                         for(int i = nCount - 1; i >= 1; --i) {
                             y = _vertexList[i].position.y;
 
                             if(y > top) top = y;
                             else if(y < bottom) bottom = y;
                         }
 
                         float height = 1f / (top - bottom);
                         UIVertex vertex = new UIVertex();
 
                         for(int i = 0; i < helper.currentVertCount; i++) {
                             helper.PopulateUIVertex(ref vertex, i);
 
                             vertex.color = BlendColor(vertex.color, EffectGradient.Evaluate((vertex.position.y - bottom) * height - Offset));
 
                             helper.SetUIVertex(vertex, i);
                         }
                     }
                     break;
             }
         }
 
         Color BlendColor(Color colorA, Color colorB) {
             switch(BlendMode) {
                 default: return colorB;
                 case Blend.Add: return colorA + colorB;
                 case Blend.Multiply: return colorA * colorB;
             }
         }
 
         public enum Type {
             Horizontal,
             Vertical
         }
 
         public enum Blend {
             Override,
             Add,
             Multiply
         }
     }
 }
Comment
Add comment · Show 3 · Share
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 Karmate · Jun 20, 2016 at 11:30 AM 0
Share

alt text

alt text

s1.png (104.0 kB)
s2.png (92.2 kB)
avatar image GibTreaty · Jun 20, 2016 at 03:13 PM 0
Share

The issue there is that there aren't enough vertices for it to color between so it's only coloring from the left and right sides of the gradient. :( I don't know any way around that.

avatar image Karmate GibTreaty · Jun 20, 2016 at 04:08 PM 0
Share

me, neither :) thank you for script.

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

47 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Modify individual letters in new Unity UI Text 1 Answer

UI Text.color not assigning? 2 Answers

How to change color of text relative to current color? 1 Answer

How do I color exactly half of a word in TMPro UI text? 0 Answers

UI text color not assigning correctly 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