-
-
Save kirill2400/d03709e8473f60932476f3f55a38ae99 to your computer and use it in GitHub Desktop.
Apply gradient to whole TMP text, but no for each character. Now correct for multiline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ---------------------------------------------------------------------------- | |
| // The MIT License | |
| // TextGradient https://gist.github.com/mopsicus/9d344451ca614d7e9937bc0c6da2b21d | |
| // Copyright (c) 2020 Mopsicus <mail@mopsicus.ru> | |
| // ---------------------------------------------------------------------------- | |
| using System.Collections; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using TMPro; | |
| using UnityEngine; | |
| /// <summary> | |
| /// Apply gradient to whole text | |
| /// </summary> | |
| public class TextGradient : MonoBehaviour { | |
| /// <summary> | |
| /// Do gradient text | |
| /// </summary> | |
| private void Start () { | |
| ApplyGradient (); | |
| } | |
| /// <summary> | |
| /// Gradient method | |
| /// Get gradient by steps, and make vertext gradient array from colors | |
| /// Apply to each character | |
| /// </summary> | |
| void ApplyGradient () { | |
| TMP_Text textComponent = GetComponent<TMP_Text> (); | |
| textComponent.ForceMeshUpdate (); | |
| TMP_TextInfo textInfo = textComponent.textInfo; | |
| int count = textInfo.characterCount; | |
| int count = textInfo.lineInfo.Max(t => t.characterCount); | |
| int lineCount = textInfo.lineCount; | |
| Color[] steps = GetGradients (textComponent.colorGradient.topLeft, textComponent.colorGradient.topRight, count + 1); | |
| VertexGradient[] gradients = new VertexGradient[steps.Length]; | |
| for (int i = 0; i < steps.Length - 1; i++) { | |
| gradients[i] = new VertexGradient (steps[i], steps[i + 1], steps[i], steps[i + 1]); | |
| } | |
| Color32[] colors; | |
| int lineIndex = 0; | |
| while (lineIndex < lineCount) | |
| { | |
| int index = 0; | |
| var currentLineInfo = textInfo.lineInfo[lineIndex]; | |
| while (index < currentLineInfo.characterCount) | |
| { | |
| var charIndex = currentLineInfo.firstCharacterIndex + index; | |
| int materialIndex = textInfo.characterInfo[charIndex].materialReferenceIndex; | |
| colors = textInfo.meshInfo[materialIndex].colors32; | |
| int vertexIndex = textInfo.characterInfo[charIndex].vertexIndex; | |
| if (textInfo.characterInfo[charIndex].isVisible) | |
| { | |
| colors[vertexIndex + 0] = gradients[index].bottomLeft; | |
| colors[vertexIndex + 1] = gradients[index].topLeft; | |
| colors[vertexIndex + 2] = gradients[index].bottomRight; | |
| colors[vertexIndex + 3] = gradients[index].topRight; | |
| textComponent.UpdateVertexData(TMP_VertexDataUpdateFlags.Colors32); | |
| } | |
| index++; | |
| } | |
| lineIndex++; | |
| } | |
| } | |
| /// <summary> | |
| /// Split gradient by steps from color to color | |
| /// </summary> | |
| /// <param name="start">Start color</param> | |
| /// <param name="end">End color</param> | |
| /// <param name="steps">Steps count</param> | |
| /// <returns>Array of colors</returns> | |
| public static Color[] GetGradients (Color start, Color end, int steps) { | |
| Color[] result = new Color[steps]; | |
| float r = ((end.r - start.r) / (steps - 1)); | |
| float g = ((end.g - start.g) / (steps - 1)); | |
| float b = ((end.b - start.b) / (steps - 1)); | |
| float a = ((end.a - start.a) / (steps - 1)); | |
| for (int i = 0; i < steps; i++) { | |
| result[i] = new Color (start.r + (r * i), start.g + (g * i), start.b + (b * i), start.a + (a * i)); | |
| } | |
| return result; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment