/** * Render the given text element in world space. * * @param element The text element. * @param style The style to use when rendering. */ protected void RenderWorldTextElement(WorldSpaceTextElement element, GUIStyle style) { // project the world point onto the screen Vector2 ptScreen = Camera.main.WorldToScreenPoint(element.pos); // off the screen if (ptScreen.x < 0 || ptScreen.y < 0 || ptScreen.x >= Screen.width || ptScreen.y >= Screen.height) { return; } // render a single line if (element.text.Length == 1) { Rect bounds = new Rect(ptScreen.x - 1000, Screen.height - ptScreen.y - 1000, 2000, 2000); RenderText(element.text[0], element, bounds, style); } // render each line else { for (int i = 0; i < element.text.Length; ++i) { Rect bounds = new Rect(ptScreen.x - 1000, Screen.height - ptScreen.y - 1000 + style.fontSize * i, 2000, 2000); RenderText(element.text[i], element, bounds, style); } } }
/** * Adds text lines to the world. * * @param text The lines of text to add. * @param time The number of seconds to keep it in the world. If negative then it will remain until cleared. * @param color The color of the text. * @param fontSize The size of the font. */ public void AddWorldText(string[] text, Vector3 pos, float time, Color color, int fontSize) { WorldSpaceTextElement element = new WorldSpaceTextElement(); element.color = color; element.text = text; element.time = time; element.pos = pos; element.fontSize = fontSize; m_worldTextElements.Add(element); }
/** * Renders the all the world text elements. */ protected void RenderWorldText() { if (m_worldTextElements.Count == 0) { return; } GUIStyle style = new GUIStyle(); style.fontStyle = FontStyle.Bold; style.alignment = TextAnchor.MiddleCenter; // render each element for (int i = 0; i < m_worldTextElements.Count; ++i) { WorldSpaceTextElement element = m_worldTextElements[i]; style.fontSize = element.fontSize; RenderWorldTextElement(element, style); } }