using UnityEngine; using System.Collections; public class FloatingText2D : MonoBehaviour { private UILabel __label; private Vector3 __pos; private Transform __t; public bool followTarget = true; public Vector3 defaultSize = new Vector3(1,1,1); public GameObject targetObject; public Camera worldCamera; public Camera guiCamera; public TweenPosition tweenPos; public void init( string text_, GameObject gameObj_ ) { this.text = text_; this.target = gameObj_; } public void init( string text_, Color clr_, GameObject gameObj_ ) { this.color = clr_; this.text = text_; this.target = gameObj_; } public void spawnAt( GameObject targetObj_, Vector3 size_, Transform parent_ ) { target = targetObj_; __t.parent = parent_; defaultSize = size_; } public void followingObject() { __pos = worldCamera.WorldToViewportPoint( targetObject.transform.position ); __pos = guiCamera.ViewportToWorldPoint( __pos ); __pos.z = 0f; transform.position = __pos; } // Use this for initialization void Awake() { __t = transform; __label = GetComponent(); } void Start() { if( guiCamera == null ) guiCamera = NGUITools.FindCameraForLayer( gameObject.layer ); __label.transform.localScale = new Vector3( __t.transform.localScale.x * defaultSize.x, __t.transform.localScale.y * defaultSize.y, 1f ); } void LateUpdate() { if( followTarget ) followingObject(); } // property // public string text { get { return __label.text; } set { __label.text = value; } } public Color color { get { return __label.color; } set { __label.color = value; } } public Vector3 size { get { return __label.transform.localScale; } set { __label.transform.localScale = value; } } public TweenPosition tweenPosition { get { TweenPosition _tp = GetComponent(); if( _tp == null ) _tp = targetObject.AddComponent(); return _tp; } } public GameObject target { get { return targetObject; } set { targetObject = value; worldCamera = NGUITools.FindCameraForLayer( targetObject.layer ); } } }