From 1ef67d5fbaf794ea8163c45556e1b37dc9893b98 Mon Sep 17 00:00:00 2001 From: herbst Date: Wed, 22 Mar 2017 14:16:03 +0100 Subject: [PATCH] mono spacing text works with right alignment now, example scene --- Examples/TextEffects/TextEffects.unity | Bin 36328 -> 40736 bytes Scripts/Effects/MonoSpacing.cs | 21 +++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/Examples/TextEffects/TextEffects.unity b/Examples/TextEffects/TextEffects.unity index 678592152b4d7c92a865f8e342cbdbebbb23d320..e49b674f48433261581e942327e5ca420080fd66 100644 GIT binary patch delta 3183 zcmb7`ZA?>F7{^ab!Kx@lPzwrHp^AzM;u4p|@lqE^bQ=mH$==#YD_syuTTmR>3gRG; zOn0*Qx(LopHs`YCx`~b`@e^(?A!b>~KH(SRG>iSPWbD)Se{P?{og*Z!dN}9)?s?wM zbIx<_qjTngTjmrYnqL&+fM59D3oh<>~R#i&|!T<1)P5rKRpJ zH_Ol7v^xidh(bnK&@#~IXTm??848Q z_lKO9Fe?%5@gIc==){B>;^>pmo-nJGruWh!6Hu53YDKGVb}vPzi?*R7TYH#Aq3#%- z=c3gxvqD`hp68+MW#$G8;CVjU*xUBy=)^c!3wqLbms!OeAx;Ys(cS!k;Ur+SNeFb} zM`l}LQ8~E#$zENd8%!yzVm1K=g}T0KW&wD%h4t-WHU!Uw4*Imj zh(2)?uo3{a#D1P=TO!0Y&RspTF>FtjyV6hIr*k4=V7=(cNEx%J-7w-}mWR_+fjRJ9 zY-bk8hzL=_MqXj~b*5qD%IVwNFkei7sippsStU-cTA8cN9)T6GzOR_svkZO9Zw;1` zz7jBfWnf=qF&qO_OOwqkkRA6LD=Ahm8wQKA5eKua4#P+&E8J*EQgkh^~pBCbA)7 zac0LRiqAv*CWfPcY7?57*>El@d8reaxz`zyeB1fBo%HsAsX)GCW?Qd}2yva+FjxTd zu|LHyF>Feahf1OHd4neoZP4x<=r?IKO^40Na6tc266JVv;T+Ll_S3zRJ94zIKn9vD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kq zJ~{BwE^hSuM54u}2f{&%Xc}_qQ>|e|-*jq9amcmLo>r9&#p7i^fBYyDFCtD|)y6GW zd9Whg>(r-DU$lrn9S>#E=bgoQ`A_f$Dv5gicmsanJ);V5muhXhc)n899PqM;k8d7Yc{9IBNS>IEfw;e@lQNRsLR~SW-Jq|J2z6lD1b)swexs#*{)G7}0ND&doA}4`zgi s9{(>q?m^{5d2?x>Azs(wUSs!8_8T?hgKfs{X~Q<%dnJ&m8@t;550W@CVE_OC diff --git a/Scripts/Effects/MonoSpacing.cs b/Scripts/Effects/MonoSpacing.cs index b012da0..55086bc 100644 --- a/Scripts/Effects/MonoSpacing.cs +++ b/Scripts/Effects/MonoSpacing.cs @@ -1,10 +1,10 @@ -/// Credit Deeperbeige +/// Credit herbst / derived from LetterSpacing by Deeperbeige /// Sourced from - http://forum.unity3d.com/threads/adjustable-character-spacing-free-script.288277/ /* -Produces an simple tracking/letter-spacing effect on UI Text components. +Produces an simple mono-spacing effect on UI Text components. -Set the spacing parameter to adjust letter spacing. +Set the spacing parameter to adjust mono spacing. Negative values cuddle the text up tighter than normal. Go too far and it'll look odd. Positive values spread the text out more than normal. This will NOT respect the text area you've defined. Zero spacing will present the font with no changes. @@ -50,17 +50,21 @@ namespace UnityEngine.UI.Extensions /// Note, Vertex Count has changed in 5.2.1+, is now 6 (two tris) instead of 4 (tri strip). public class MonoSpacing : BaseMeshEffect { + public bool useHalfCharWidth = false; public float halfCharWidth = 1; [SerializeField] private float m_spacing = 0f; + RectTransform rectTransform; + protected MonoSpacing() { } #if UNITY_EDITOR protected override void OnValidate() { spacing = m_spacing; + rectTransform = GetComponent().GetComponent(); base.OnValidate(); } #endif @@ -86,7 +90,7 @@ namespace UnityEngine.UI.Extensions Text text = GetComponent(); if (text == null) { - Debug.LogWarning("LetterSpacing: Missing Text component"); + Debug.LogWarning("MonoSpacing: Missing Text component"); return; } @@ -120,9 +124,10 @@ namespace UnityEngine.UI.Extensions for (int lineIdx=0; lineIdx < lines.Length; lineIdx++) { string line = lines[lineIdx]; - float lineOffset = (line.Length -1) * letterOffset * alignmentFactor; + float lineOffset = (line.Length - 1) * letterOffset * (alignmentFactor) - (alignmentFactor - 0.5f) * rectTransform.rect.width; + + var offsetX = -lineOffset + letterOffset / 2 * (1 - alignmentFactor * 2); - var offsetX = - lineOffset + letterOffset / 2; for (int charIdx = 0; charIdx < line.Length; charIdx++) { int idx1 = glyphIdx * 6 + 0; @@ -144,10 +149,10 @@ namespace UnityEngine.UI.Extensions // pos = Vector3.right * (letterOffset * (charIdx) - lineOffset); float charWidth = (vert2.position - vert1.position).x; - var smallChar = charWidth < halfCharWidth; + var smallChar = useHalfCharWidth && (charWidth < halfCharWidth); var smallCharOffset = smallChar ? -letterOffset/4 : 0; - + vert1.position += new Vector3(-vert1.position.x + offsetX + -.5f * charWidth + smallCharOffset, 0, 0); vert2.position += new Vector3(-vert2.position.x + offsetX + .5f * charWidth + smallCharOffset, 0, 0); vert3.position += new Vector3(-vert3.position.x + offsetX + .5f * charWidth + smallCharOffset, 0, 0);