From f411a5268766aa9fe3f9f017cf5abdb55bc1c703 Mon Sep 17 00:00:00 2001 From: Nico Date: Tue, 1 Jul 2025 02:06:41 -0700 Subject: [PATCH] Add FloatingTextSpawner for debugging purposes --- .../Environment/FloatingTextSpawner.cs | 84 +++++++++++++++++++ .../Environment/FloatingTextSpawner.cs.meta | 2 + 2 files changed, 86 insertions(+) create mode 100644 Assets/Scripts/Runtime/Environment/FloatingTextSpawner.cs create mode 100644 Assets/Scripts/Runtime/Environment/FloatingTextSpawner.cs.meta diff --git a/Assets/Scripts/Runtime/Environment/FloatingTextSpawner.cs b/Assets/Scripts/Runtime/Environment/FloatingTextSpawner.cs new file mode 100644 index 0000000..66eb2ab --- /dev/null +++ b/Assets/Scripts/Runtime/Environment/FloatingTextSpawner.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using UnityEngine; + +public class FloatingTextSpawner { + private static GameObject GameObjectContainer = new GameObject($"Floating Text Container"); + private Transform Transform; + private List TextList = new List(); + + public FloatingTextSpawner(Transform transform) { + Transform = transform; + } + + public void SpawnFloatingText(string message, Color color, float duration = 1.5f) { + try { + + GameObject textObj = new GameObject("FloatingText"); + textObj.transform.position = Transform.position + new Vector3(0, 1, 1); // offset above character + + TextMesh textMesh = textObj.AddComponent(); + textMesh.text = message; + textMesh.characterSize = 0.2f; + textMesh.fontSize = 48; + textMesh.fontStyle = FontStyle.Bold; + textMesh.color = color; + textMesh.alignment = TextAlignment.Center; + textMesh.anchor = TextAnchor.MiddleCenter; + + Renderer renderer = textObj.GetComponent(); + renderer.sortingLayerName = "UI"; + renderer.sortingOrder = 0; + + textObj.AddComponent().Init(duration, ref TextList); + UnityEngine.Object.Instantiate(textObj, GameObjectContainer.transform); + } catch (Exception e) { } + } +} + + +public class FloatingText : MonoBehaviour { + private float lifetime; + private float speed = 1f; + private Color originalColor; + private TextMesh textMesh; + + + public void Init(float duration, ref List textList) { + try { + lifetime = duration; + textMesh = GetComponent(); + originalColor = textMesh.color; + + for (int i = textList.Count - 1; i >= 0; i--) { + if (textList[i] == null) + textList.RemoveAt(i); + else + textList[i].position += Vector3.up * 0.5f; + } + + textList.Add(this.transform); + } catch (Exception e) { } + } + + void Update() { + try { + + transform.position += Vector3.up * speed * Time.deltaTime; + + lifetime -= Time.deltaTime; + if (lifetime <= 0) { + Destroy(gameObject); + } else { + float alpha = Mathf.Clamp01(lifetime / 1.5f); + textMesh.color = new Color(originalColor.r, originalColor.g, originalColor.b, alpha); + } + } catch (Exception e) { } + + } +} + + diff --git a/Assets/Scripts/Runtime/Environment/FloatingTextSpawner.cs.meta b/Assets/Scripts/Runtime/Environment/FloatingTextSpawner.cs.meta new file mode 100644 index 0000000..9fbbf89 --- /dev/null +++ b/Assets/Scripts/Runtime/Environment/FloatingTextSpawner.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: a0820d00eb0a7a34e83145fb846387cd \ No newline at end of file