abstract class : ScriptableObject
CCTextModifier is the base class for all modifiers designed to tweak the character sprites of a CCText
.
You can make your own modifier by extending this class and overriding the Modify
method.
CCText
calls Modify
before it sends vertices and UV to the mesh.
It does not send colors to the mesh, neither does it update them, unless they are reset or the mesh needs to grow.
So if you modify colors, assume they are undefined and assign them to the mesh yourself.
Keep in mind that CCText
only uses sprites for non-whitespace characters.
If you iterate over the text content, do not advance the sprite index whenever you encounter a whitespace character.
Characters undefined by the used CCFont
do get a sprite, but they will be collapsed.
void
Modify a CCText
.
This method is abstract, you must override it.
CCText
The CCText
to modify.
void
Update all CCText
components in the scene that use this modifier.
This method is expensive and creates a temporary array.
using UnityEditor; using UnityEngine; public class CCTextModifierExample : CCTextModifier { [MenuItem("Assets/Create/TextModifier Example")] public static void CreateExample () { AssetDatabase.CreateAsset( ScriptableObject.CreateInstance<CCTextModifierExample>(), "Assets/Modifier Example.asset"); } public Color alternativeColor = Color.red; public override void Modify (CCText text) { bool toggle = false; Color colorA = text.Color, colorB = alternativeColor; Color[] colors = text.colors; for(int i = 0, v = 0, l = text.Length; i < l; i++){ if(text[i] <= ' '){ toggle = true; } else{ if(toggle){ Color temp = colorA; colorA = colorB; colorB = temp; toggle = false; } colors[v] = colorA; colors[v + 1] = colorA; colors[v + 2] = colorA; colors[v + 3] = colorA; v += 4; } } text.mesh.colors = colors; } }The above code defines a modifier that gives every other word an alternative color, which is red by default. It also includes code that adds a menu item to create an asset instance of it.
Create a new asset instance via Assets / Create / TextModifier Example. You'll then find it in the root folder of the project view.
Assign the new modifier to the Modifier property of a CCText
component. Every other
word of the text it displays will instanty become red (unless its material ignores vertex colors).