Adjustable Character Spacing - free script

I was dismayed to find that the new 4.6 uGUI Text component supports line spacing, but not letter spacing. Especially since the later versions of NGUI had this option. I figured that when the UI source came out, I could probably add it pretty easily. Nope - the text layout engine isn’t part of the open source stuff, and you can’t get at the code or a suitable API for it.

My best workaround was to simply modify existing fonts and add extra space to every character. This is as difficult to manage as it sounds, and impractical for a recent design I’m working to which has lots of differently spaced headings. I would need a different font for each different spacing.

Then by chance I looked at the source for the Shadow effect, which uses a BaseVertexEffect to modify the list of vertices that make the geometry of the object being drawn. An hour or two of hacking, and I’ve got a component that spaces text out via much the same technique (although it doesn’t generate any extra verts). Add the component to a UI textfield, adjust the spacing value, job done.

I thought I should share the effect here, as I’ve seen a few other threads asking the same basic question.

While this effect works in the editor, plays nicely on mobile and integrates well with the new UI system, it does have its limits. Full details in the comments at the top of the code, but don’t expect miracles. If you can improve some part of it, feel free to contribute!

Grab the effect from BitBucket. You can either clone the repo if you know your way around GIT, or you can just get a zip directly from the downloads section on the left. Thanks to Andrew Pavlovich for setting up the repo, and to everyone else who has contributed improvements.

https://bitbucket.org/AcornGame/adjustable-character-spacing

36 Likes

Very nice, just what i was looking for, since my fonts look like shit in unity :smile:
All the letters overlapping… i really wonder what happens internally with the kerning.

Man, you are awesome! It works perfectly! Thank you very much!

Thank you! Saved me from having to use images with the separation set by a graphic designer.

Thank you, very simple and nice.

This works wonderfully, thanks!

Thanks for the component - works great! I did notice that it doesn’t play nicely with the shadow component at first, but I found that if you apply this letter spacing before the shadow (make sure Letter Spacer comes before Shadow in the component list on the GameObject), everything works fine.

This is awesome! Thanks!

Thank you for sharing! :slight_smile:

In case anyone needs a solution that works with rich text and word wrapping, take a look at TextMesh Pro. It includes control over character spacing, line spacing and paragraph spacing as well as kerning.

TextMesh Pro also offers more text styles like superscript, subscript, underline, strikethrough and several additional rich text tags. Below are a few examples.


The size tag supports different formats like <size=36> or <size=+12> or <size=-12> or <size=%150>.


This last example uses the Rich Text Tag <align=left> or <align=center> or <align=right> or <align=justified> and

Again, these are just some of the rich text tags available. These features all work with the New UI as well as the normal Mesh Renderer. TextMesh Pro also features an advanced text rendering system and a lot more. Beta releases of TextMesh Pro for Unity 4.6 & 5.0 are available to registered users on the TMP user forum.

2 Likes

Thanks you so much, your free script works really nice.

Thanks, Deeperbeige. This is extremely useful.

Can someone please help me? Where do I put this script?

To the GameObject where your Text component attached at.

This is SO FUCKING GOOD. YOU ARE FUCKING MASTER.

I could not write better code myself. If you ever want a job contact us.

Fucking amazing. The best thing I have ever seen on the forum. Than you so much. This should cost €200-

Thank you. You are too good.

One other thing: I noticed that this effect doesn’t quite work as expected when Unity inserts automatic line breaks (for example, when your text area wraps). However, I think I found a workable solution using the Text component’s “cachedTextGenerator” member. You can do something like this in ModifyVertices, after getting the Text component:

string str = text.text;

// Artificially insert line breaks for automatic line breaks.
IList<UILineInfo> lineInfos = text.cachedTextGenerator.lines;
for(int i = lineInfos.Count - 1; i > 0; i--)
{
    // Insert a \n at the location Unity wants to automatically line break.
    // Also, remove any space before the automatic line break location.
    str = str.Insert(lineInfos[i].startCharIdx, "\n");
    str = str.Remove(lineInfos[i].startCharIdx - 1, 1);
}

string[] lines = str.Split('\n');
// Rest of the code down here.

You want to iterate over the array backwards because the Insert operation changes the index values of the string. You also want to NOT include the 0th element because that will always be 0 (the first line’s starting character is always 0). You don’t want to insert a line break for the first line.

2 Likes

We released an asset to solve kerning for Unity UI, it inherits from the Text element creating a new element called KernedText: Unity Asset Store - The Best Assets for Game Making

Working great. Thank you…

v v thanks for your effort
it solved the issue
unity should include this feature builtin

Wonderful!