using UnityEngine; using System.Collections; public class FloatingTextUp2D : MonoBehaviour { private UILabel __label; // from ScriptIncluded private Vector3 __pos; private Vector3 __initPos; public Camera guiCamera = null; public Camera worldCamera = null; public GameObject targetObject = null; public int tweenTime = 2; public int tweenDistance = 50; // class spec function // public void destroySelf() { Destroy( gameObject ); } public void initAndStartMoving( Vector3 initPos_, Camera mainCam_ = null, Camera guiCam_ = null ) { if( mainCam_ != null ) worldCamera = mainCam_; if( guiCam_ != null ) guiCamera = guiCam_; startMoveUp( initPos_, 2, 50 ); } public void startMoveUp( Vector3 initPos_, int tweenDuration_, int tweenEnd_ ) { __initPos = initPos_; calculatePosition( initPos_ ); TweenPosition _tp = this.tweenPosition; _tp.duration = tweenDuration_; _tp.from = transform.localPosition; _tp.to = _tp.from + Vector3.up * tweenEnd_; _tp.eventReceiver = gameObject; _tp.callWhenFinished = "destroySelf"; } public void calculatePosition( Vector3 pos_ ) { __pos = worldCamera.WorldToViewportPoint( pos_ ); __pos = guiCamera.ViewportToWorldPoint( __pos ); __pos.z = 0f; transform.position = __pos; } // Unity3d reaction function // public void Awake() { __label = GetComponent(); } void Start() { if( guiCamera == null ) guiCamera = NGUITools.FindCameraForLayer( gameObject.layer ); if( worldCamera == null ) worldCamera = Camera.mainCamera; __label.MakePixelPerfect(); // why bigger??? if( targetObject != null ) { startMoveUp( targetObject.transform.position, tweenTime, tweenDistance ); } } // Property helper // public TweenPosition tweenPosition { get { TweenPosition _tp = GetComponent(); if( _tp == null ) _tp = gameObject.AddComponent(); return _tp; } } }