Author Topic: problem with label color  (Read 4471 times)

aidji

  • Guest
problem with label color
« on: December 21, 2012, 04:22:24 AM »
Hi i have a problem with my scripts(im very begineer):
my problem is with Floating text driver with this line:
  1. ft.Init ( textValue, testColor , target );
testcolor set the value of my prefab label, but i dont see the label displaying correctly! and i change it to
  1. ft.Init ( textValue, Color.gray, target );
it works fine but i would have this value changeable through variable.

FloatingTextDriver
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class FloatingTextDriver : MonoBehaviour {
  5.         public GameObject target;
  6.         public GameObject Prefab;
  7.         public UIFont font;
  8.         public Transform parent;
  9.         public float duration;
  10.         public float height;
  11.         public float scale;
  12.         public string textValue;
  13.         public Color testColor;
  14.  
  15.        
  16.         public void OnClick() {
  17.                 //aa
  18.                 //FloatingText ft = Instantiate ( ftt ) as FloatingText;
  19.  
  20.                 GameObject ft1 = Instantiate( Prefab) as GameObject;
  21.                 FloatingText ft = ft1.GetComponentInChildren<FloatingText>();
  22.                
  23.                 //Hashtable ht = new Hashtable();
  24.                 //ht.Add("y", 200);
  25.                 //ht.Add("oncomplete", "DestroyMe");
  26.                 //iTween.MoveTo(ft1, ht);
  27.                 Vector3 size =Prefab.transform.localScale * scale;
  28.                
  29.                 //ft.transform.parent = parent;
  30.                 //ft.transform.localScale = size;
  31.                
  32.                 ft.SpawnAt ( target, size, parent );
  33. //              ft.Target = target;
  34. //              ft.Text = "text ok";
  35. //             
  36.                 ft.Init ( textValue, testColor , target );
  37.                 ft.FollowTarget = false;
  38.                 ft.Following();
  39.                 //animation
  40.                 TweenPosition tp = ft.TweenPosition;
  41.                 tp.duration = duration;
  42.                 tp.from = ft.transform.localPosition;
  43.                 tp.to = tp.from + Vector3.up * height;
  44.                 StartCoroutine(Do (ft1));
  45.                 //Invoke("DestroyMe", duration);
  46.                
  47.                 //TweenPosition.Begin(gameObject, 12, Vector3.zero);
  48.         //      yield return new WaitForSeconds( 2.0f );
  49. //              ft.Font = font;
  50.                
  51.  
  52.         }
  53.        
  54.         IEnumerator Do(GameObject a) {
  55.         yield return new WaitForSeconds(duration);
  56.                 Destroy(a);
  57.     }
  58.        
  59.         public void showText ()
  60.         {
  61.                 GameObject ft1 = Instantiate( Prefab) as GameObject;
  62.                 FloatingText ft = ft1.GetComponentInChildren<FloatingText>();
  63.                 Vector3 size =Prefab.transform.localScale * scale;
  64.                 ft.SpawnAt ( target, size, parent );
  65.                 ft.Init ( textValue, Color.black, target );
  66.                 ft.FollowTarget = false;
  67.                 ft.Following();//animation
  68.                 TweenPosition tp = ft.TweenPosition;
  69.                 tp.duration = duration;
  70.                 tp.from = ft.transform.localPosition;
  71.                 tp.to = tp.from + Vector3.up * height;
  72.                 StartCoroutine(Do (ft1));
  73.         }
  74. }
  75.  
  76.  
FloatingText
  1. using UnityEngine;
  2. using System.Collections;
  3. public class FloatingText : MonoBehaviour {
  4.        
  5.  
  6. private UILabel _lbl;
  7.         private bool _followTarget;
  8.         private Vector3 _pos;
  9.        
  10.         public GameObject t_arget;
  11.         public Camera worldCamera;
  12.         public Camera guiCamera;
  13.        
  14.         private Transform _t;
  15.         //
  16.        
  17.         void Awake() {
  18.                 _t = transform;
  19.                 _lbl = GetComponentInChildren<UILabel>();
  20.                
  21.                 if( _lbl == null )
  22.                         Debug.LogError("could Not Find UILabel" );
  23.         }      
  24.        
  25.         void Start() {
  26.                 guiCamera = NGUITools.FindCameraForLayer ( gameObject.layer );
  27.         }
  28.        
  29.         public Color TextColor {
  30.                 get { return _lbl.color;}
  31.                 set { _lbl.color = value; }
  32.         }
  33.        
  34.         public string Text{
  35.                 get { return _lbl.text;}
  36.                 set { _lbl.text = value; }
  37.         }
  38.        
  39.         public bool FollowTarget {
  40.                 get { return _followTarget; }
  41.                 set { _followTarget = value; }
  42.         }
  43.        
  44.         public GameObject Target {
  45.                 get { return t_arget; }
  46.                 set {
  47.                         t_arget = value;
  48.                         worldCamera = NGUITools.FindCameraForLayer ( t_arget.layer );
  49.                 }
  50.         }
  51.        
  52.         public Vector3 Size {
  53.                 get { return _lbl.transform.localScale; }
  54.                 set {_lbl.transform.localScale = value; }
  55.         }
  56.         public UIFont Font {
  57.                 get {return _lbl.font; }
  58.                 set{  _lbl.font = value; }
  59.         }
  60.         //animation
  61.         public TweenPosition TweenPosition{
  62.                 get {
  63.                         TweenPosition tp =GetComponent<TweenPosition>();
  64.                        
  65.                         if( tp == null )
  66.                                 tp = gameObject.AddComponent<TweenPosition>();
  67.                        
  68.                         return tp;
  69.                 }
  70.                 set {}
  71.         }
  72.        
  73.         public void Init( string txt, Color clr, GameObject go ){
  74.                 TextColor = clr;
  75.                 Text = txt;
  76.                 Target = go;
  77.         }
  78.        
  79.         public void SpawnAt ( GameObject target, Vector3 size, Transform parent ) {
  80.                 Target = target;
  81.                 _t.parent = parent;
  82.                 Size = size;
  83.                
  84.         }
  85.        
  86.         public void Following() {
  87.                 _pos =worldCamera.WorldToViewportPoint ( t_arget.transform.position );
  88.                 if(!guiCamera)
  89.                         guiCamera = NGUITools.FindCameraForLayer ( gameObject.layer );
  90.                 _pos = guiCamera.ViewportToWorldPoint( _pos );
  91.                 _pos.z = 0f;
  92.                 _t.position = _pos;
  93.         }
  94.        
  95.         public void LateUpdate() {
  96.                 if( _followTarget )
  97.                         Following();
  98.         }
  99.        
  100.  
  101. }
  102.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: problem with label color
« Reply #1 on: December 22, 2012, 03:57:41 AM »
You know NGUI has HUDText extension which basically is floating text, right? :P

aidji

  • Guest
Re: problem with label color
« Reply #2 on: December 22, 2012, 12:35:05 PM »
yes but my script is really more basic and i cant buy it im saving money for Tnet plugin :-[, it work just i dont know how to change colors through public variables, it make my text being transparent when i change it color

actually my solution is to duplicate to script on an other prefab and change the color.yellow to white for exemple :(

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: problem with label color
« Reply #3 on: December 23, 2012, 06:47:57 AM »
Yellow to white? o_O What shader are you using on your text?

aidji

  • Guest
Re: problem with label color
« Reply #4 on: December 24, 2012, 09:17:28 PM »
my meaning is i dont want to change the color of the label (its the default label widget made by NGUI, so i didnt added or changed shader)

actually my method is:
one prefab with  ft.Init ( textValue, Color.gray, target );
a second prefab with a second script where is change only Color.white

and i instanciate both of them when i need..i believe its a stupid way to do, but i dont believe a way to pass the color value through a public variable