2修
This commit is contained in:
@@ -12,6 +12,23 @@ public class SpriteNumberDisplay : MonoBehaviour
|
||||
public Sprite plusSprite;
|
||||
public Sprite minusSprite;
|
||||
|
||||
[Header("string")]
|
||||
public string en_combo = "COMBO";
|
||||
[Tooltip("COMBO 标签字形高度(像素)。宽度按各字母 sprite 宽高比自动计算。")]
|
||||
public int en_combo_fontSize = 20;
|
||||
public Color en_combo_color = Color.white;
|
||||
public float en_combo_yOffset= 0f;
|
||||
|
||||
[Header("COMBO letter sprites")]
|
||||
[Tooltip("字母 C 的 sprite")]
|
||||
public Sprite letterC_sprite;
|
||||
[Tooltip("字母 O 的 sprite")]
|
||||
public Sprite letterO_sprite;
|
||||
[Tooltip("字母 M 的 sprite")]
|
||||
public Sprite letterM_sprite;
|
||||
[Tooltip("字母 B 的 sprite")]
|
||||
public Sprite letterB_sprite;
|
||||
|
||||
[Header("Layout")]
|
||||
public Vector2 digitSize = new Vector2(28f, 42f);
|
||||
public Vector2 decimalPointSize = new Vector2(12f, 42f);
|
||||
@@ -19,6 +36,8 @@ public class SpriteNumberDisplay : MonoBehaviour
|
||||
public float spacing = 2f;
|
||||
public bool centerAlign = true;
|
||||
public bool preserveAspect = true;
|
||||
[Tooltip("Keep all visible glyphs at the configured height. Width is recalculated from each sprite aspect ratio.")]
|
||||
public bool forceUniformHeight = true;
|
||||
public Color color = Color.white;
|
||||
[Tooltip("Canvas sorting order used by generated character sprites.")]
|
||||
public int characterSortingOrder = 0;
|
||||
@@ -26,6 +45,9 @@ public class SpriteNumberDisplay : MonoBehaviour
|
||||
private readonly List<Image> glyphImages = new List<Image>();
|
||||
private string currentText = string.Empty;
|
||||
|
||||
// 数字上方用 sprite 拼接的 "COMBO" 标签所用的 glyph 池。
|
||||
private readonly List<Image> comboLabelImages = new List<Image>();
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
Refresh();
|
||||
@@ -39,6 +61,12 @@ public class SpriteNumberDisplay : MonoBehaviour
|
||||
decimalPointSize.y = Mathf.Max(0f, decimalPointSize.y);
|
||||
percentSize.x = Mathf.Max(0f, percentSize.x);
|
||||
percentSize.y = Mathf.Max(0f, percentSize.y);
|
||||
|
||||
// 编辑器中改动 combo 标签相关字段时,已生成的字母 glyph 实时更新(不新建,避免编辑期实例化)。
|
||||
if (comboLabelImages.Count > 0 && isActiveAndEnabled)
|
||||
{
|
||||
RefreshComboLabel();
|
||||
}
|
||||
}
|
||||
|
||||
public void SetNumber(int value)
|
||||
@@ -83,7 +111,7 @@ public class SpriteNumberDisplay : MonoBehaviour
|
||||
|
||||
char c = currentText[i];
|
||||
Sprite sprite = ResolveSprite(c);
|
||||
Vector2 size = ResolveSize(c);
|
||||
Vector2 size = ResolveSize(c, sprite);
|
||||
|
||||
if (sprite == null)
|
||||
{
|
||||
@@ -105,6 +133,94 @@ public class SpriteNumberDisplay : MonoBehaviour
|
||||
|
||||
x += size.x + spacing;
|
||||
}
|
||||
|
||||
RefreshComboLabel();
|
||||
}
|
||||
|
||||
// 用字母 sprite(C/O/M/B)在数字上方拼接 "COMBO" 标签。字形高度、颜色、Y 偏移由 Inspector 字段控制。
|
||||
private void RefreshComboLabel()
|
||||
{
|
||||
string label = en_combo ?? string.Empty;
|
||||
|
||||
// 计算总宽度并按需扩容 glyph 池。
|
||||
float height = Mathf.Max(1f, en_combo_fontSize);
|
||||
float totalWidth = 0f;
|
||||
int visibleCount = 0;
|
||||
for (int i = 0; i < label.Length; i++)
|
||||
{
|
||||
Sprite s = ResolveComboLetterSprite(label[i]);
|
||||
if (s == null) continue;
|
||||
totalWidth += ComboLetterWidth(s, height);
|
||||
visibleCount++;
|
||||
}
|
||||
if (visibleCount > 1) totalWidth += spacing * (visibleCount - 1);
|
||||
|
||||
EnsureComboImageCount(label.Length);
|
||||
|
||||
float x = centerAlign ? -totalWidth * 0.5f : 0f;
|
||||
for (int i = 0; i < comboLabelImages.Count; i++)
|
||||
{
|
||||
Image image = comboLabelImages[i];
|
||||
if (image == null) continue;
|
||||
|
||||
Sprite sprite = i < label.Length ? ResolveComboLetterSprite(label[i]) : null;
|
||||
if (sprite == null)
|
||||
{
|
||||
image.gameObject.SetActive(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
float w = ComboLetterWidth(sprite, height);
|
||||
RectTransform rect = image.rectTransform;
|
||||
image.gameObject.SetActive(true);
|
||||
image.sprite = sprite;
|
||||
image.color = en_combo_color;
|
||||
image.preserveAspect = preserveAspect;
|
||||
ApplyCharacterSortingOrder(image);
|
||||
rect.sizeDelta = new Vector2(w, height);
|
||||
rect.anchorMin = new Vector2(0.5f, 0.5f);
|
||||
rect.anchorMax = new Vector2(0.5f, 0.5f);
|
||||
rect.pivot = new Vector2(0.5f, 0.5f);
|
||||
|
||||
// 上方定位:数字 glyph 以 y=0 居中、高约 digitSize.y,标签放其上半高 + 半个字形高 + 偏移处。
|
||||
float baseY = digitSize.y * 0.5f + height * 0.5f;
|
||||
rect.anchoredPosition = new Vector2(x + w * 0.5f, baseY + en_combo_yOffset);
|
||||
|
||||
x += w + spacing;
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureComboImageCount(int count)
|
||||
{
|
||||
while (comboLabelImages.Count < count)
|
||||
{
|
||||
GameObject glyph = new GameObject("combo_letter_" + comboLabelImages.Count, typeof(RectTransform), typeof(Canvas), typeof(Image));
|
||||
RectTransform rect = glyph.transform as RectTransform;
|
||||
rect.SetParent(transform, false);
|
||||
|
||||
Image image = glyph.GetComponent<Image>();
|
||||
image.raycastTarget = false;
|
||||
comboLabelImages.Add(image);
|
||||
}
|
||||
}
|
||||
|
||||
private Sprite ResolveComboLetterSprite(char c)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case 'C': case 'c': return letterC_sprite;
|
||||
case 'O': case 'o': return letterO_sprite;
|
||||
case 'M': case 'm': return letterM_sprite;
|
||||
case 'B': case 'b': return letterB_sprite;
|
||||
default: return null;
|
||||
}
|
||||
}
|
||||
|
||||
private float ComboLetterWidth(Sprite sprite, float height)
|
||||
{
|
||||
if (sprite == null || sprite.rect.height <= 0f) return height;
|
||||
float aspect = sprite.rect.width / sprite.rect.height;
|
||||
return height * aspect;
|
||||
}
|
||||
|
||||
private void ApplyCharacterSortingOrder(Image image)
|
||||
@@ -154,7 +270,7 @@ public class SpriteNumberDisplay : MonoBehaviour
|
||||
continue;
|
||||
}
|
||||
|
||||
total += ResolveSize(text[i]).x;
|
||||
total += ResolveSize(text[i], ResolveSprite(text[i])).x;
|
||||
visibleCount++;
|
||||
}
|
||||
|
||||
@@ -181,10 +297,19 @@ public class SpriteNumberDisplay : MonoBehaviour
|
||||
return null;
|
||||
}
|
||||
|
||||
private Vector2 ResolveSize(char c)
|
||||
private Vector2 ResolveSize(char c, Sprite sprite)
|
||||
{
|
||||
if (c == '.') return decimalPointSize;
|
||||
if (c == '%') return percentSize;
|
||||
return digitSize;
|
||||
Vector2 baseSize = digitSize;
|
||||
if (c == '.') baseSize = decimalPointSize;
|
||||
else if (c == '%') baseSize = percentSize;
|
||||
|
||||
if (!forceUniformHeight || sprite == null || sprite.rect.height <= 0f)
|
||||
{
|
||||
return baseSize;
|
||||
}
|
||||
|
||||
float height = Mathf.Max(0f, baseSize.y);
|
||||
float aspect = sprite.rect.width / sprite.rect.height;
|
||||
return new Vector2(height * aspect, height);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user