feat : Add spacing between letters
This commit is contained in:
@@ -2,6 +2,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using TMPro;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace Unheimlich.UI
|
||||
{
|
||||
@@ -108,25 +109,25 @@ namespace Unheimlich.UI
|
||||
|
||||
private void ArrangeLetters()
|
||||
{
|
||||
float totalWidth = 0f;
|
||||
List<float> letterWidths = new List<float>();
|
||||
HorizontalLayoutGroup layout = gameObject.GetComponent<HorizontalLayoutGroup>();
|
||||
if (layout == null)
|
||||
layout = gameObject.AddComponent<HorizontalLayoutGroup>();
|
||||
|
||||
layout.childAlignment = TextAnchor.MiddleCenter;
|
||||
layout.childForceExpandWidth = false;
|
||||
layout.childForceExpandHeight = false;
|
||||
layout.childControlWidth = true;
|
||||
layout.childControlHeight = true;
|
||||
layout.spacing = 0f;
|
||||
|
||||
foreach (TextMeshProUGUI letter in _letterObjects)
|
||||
{
|
||||
letter.ForceMeshUpdate();
|
||||
float width = letter.preferredWidth;
|
||||
letterWidths.Add(width);
|
||||
totalWidth += width;
|
||||
}
|
||||
ContentSizeFitter fitter = letter.gameObject.GetComponent<ContentSizeFitter>();
|
||||
if (fitter == null)
|
||||
fitter = letter.gameObject.AddComponent<ContentSizeFitter>();
|
||||
|
||||
float currentX = -totalWidth / 2f;
|
||||
|
||||
for (int i = 0; i < _letterObjects.Count; i++)
|
||||
{
|
||||
RectTransform rt = _letterObjects[i].GetComponent<RectTransform>();
|
||||
rt.anchoredPosition = new Vector2(currentX + letterWidths[i] / 2f, 0f);
|
||||
rt.sizeDelta = new Vector2(letterWidths[i] + 2f, _fontSize * 1.5f);
|
||||
currentX += letterWidths[i];
|
||||
fitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
fitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user