/// <summary> /// set text to display /// </summary> /// <param name="text">text</param> /// <param name="time">display tiome</param> /// <param name="color">display color</param> public void setText(string text, float time = 1.0f, Color?color = null) { #if DEBUG_INFO if (!m_debugText) { Debug.LogError("object cannot be null: " + this.name + ", " + this.ToString()); return; } #endif switch (textType) { case TextType.Normal: { if (!m_debugText) { return; } m_debugText.text = text; if (m_debugText.text == "") { m_debugText.enabled = false; } //else m_debugText.enabled = true; } break; case TextType.Float: { FloatTextInfo fti = new FloatTextInfo(); fti.time = time; fti.timer = 0.0f; fti.position = transform.position + Vector3.up * 2; fti.textui = (Text)Instantiate(m_debugText); fti.textui.transform.SetParent(canvas.transform); fti.textui.text = text; if (color.HasValue) { fti.textui.color = color.Value; } m_debugTexts.Add(fti); } break; } }
/// <summary> /// update text float mode /// </summary> private void _updateFloat() { for (int i = 0; i < m_debugTexts.Count; i++) { FloatTextInfo fti = m_debugTexts[i]; Text txt = fti.textui; fti.timer += Time.deltaTime * floatSpeed; if (fti.time < fti.timer) { m_debugTexts.Remove(fti); Destroy(txt.gameObject); continue; } txt.gameObject.SetActive(Visible); txt.enabled = Visible; Vector3 currentPoint = transform.position + Vector3.up * 2; currentPoint.y += fti.timer; Vector3 screenPos = Camera.main.WorldToScreenPoint(currentPoint); txt.transform.position = screenPos; } }