GuiTextOverParent
From Unify Community Wiki
(Difference between revisions)
(Created page with "== What is it == A simple way of placing text over a parent object, say a 2D sprite, by Opless. == Code == <syntaxhighlight lang="csharp"> using UnityEngine...") |
Latest revision as of 21:24, 7 September 2015
[edit] What is it
A simple way of placing text over a parent object, say a 2D sprite, by Opless.
[edit] Code
using UnityEngine; using System.Collections; [ExecuteInEditMode] public class GuiTextOverParent : MonoBehaviour { // Use this for initialization void Start () { } // Update is called once per frame void Update () { var parent = transform.parent; if (parent == null) { Debug.LogError ("No parent"); return; } var screenpoint = Camera.main.WorldToViewportPoint (parent.transform.position); this.transform.position = screenpoint; } }
[edit] Instructions
- Create your GuiText component in a game object (you'll have to do this manually in Unity5)
- Make it a child of a sprite
- Voila it works