Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Majicpanda on December 21, 2012, 12:11:36 AM

Title: Instantiating Label Prefabs with TweenPosition Attached
Post by: Majicpanda on December 21, 2012, 12:11:36 AM
Goal: Instantiate a label with text above an object the player clicks and upon completion of the TweenPosition destroy.

Problem: Text instantiates just fine, but the TweenPosition appears to cache the prefab's position when using AddChild() before getting a chance to move the label and use .Active() on it, so all of the labels start from 0,0,0.  Everything works fine without the TweenPosition attached, but I need to somehow get it to use the local position AFTER the label is moved when goldText.GetComponent<TweenPosition>().Play(true) is called.

I've tried adding TweenPosition with AddComponent but can't seem to find the right way to do this.  Seems like an easier way and I'm missing something obvious.

  1.                 GameObject goldText = NGUITools.AddChild(GoldCollectedParent, GoldCollectedLabel);
  2.                 TweenPosition tp = goldText.AddComponent<TweenPosition>();
  3.                
  4.                 Vector3 scale = goldText.transform.localScale;
  5.                 scale.y = 30f;
  6.                 scale.x = 30f;
  7.                 scale.z = 0f;
  8.                 goldText.transform.localScale = scale;         
  9.                 goldText.GetComponent<UILabel>().text = "+" + myValue.ToString();
  10.                 goldText.transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
  11.                 goldText.active = true;                        
  12.                 goldText.GetComponent<TweenPosition>().Reset();
  13.                 goldText.GetComponent<TweenPosition>().Play(true);             
  14.  
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: ArenMook on December 22, 2012, 03:53:59 AM
Remove the tween from your prefab. Use TweenPosition.Begin() to tween your label after you instantiate it.
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: Majicpanda on December 22, 2012, 04:14:40 AM
I was messing with this a bit today.  Is the correct way to do this to use AddChild() and then to set the position to the ScreenToWorld ( target ) and then manually construct the From and To in code?
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: ArenMook on December 22, 2012, 07:38:36 AM
If you use TweenPosition.Begin, "from" is already set for you.
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: Majicpanda on December 22, 2012, 04:07:47 PM
This is a lot harder than I thought converting the object to screen space in nGUI. 

User clicks treasure item, NGUITools.AddChild(panelName, prefab); instantiates the label.  Now that it's in the world how do you get it to the nGUI camera space?  If I use TweenPosition.Begin(transform.parent.gameObject, 1f, new Vector3(startPos.x - 10, startPos.y, startPos.z)); it throws it clear off the screen in a hurry and tries to move it from the World to the nGUI camera I think.

So the problem seems to be... do I need to somehow use WorldToScreen and convert the world object that was just clicked to screen pos and then move the object, SetActive and then TweenPosition?  I can't get the label to the ngui camera in world space.

  1. Vector3 screenPos = Camera.mainCamera.ScreenToWorldPoint( transform.position );
  2. Vector3 guiCameraPosition = guiCamera.ScreenToWorldPoint(screenPos);           
  3.                
  4. goldText.transform.position = new Vector3(guiCameraPosition.x, guiCameraPosition.y, 0.1f);
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: Majicpanda on December 22, 2012, 05:54:03 PM
All good now thanks for the help.

  1.                 //flashy text to show worth of clicked gold
  2.                 GameObject goldText = NGUITools.AddChild(GoldCollectedParent,GoldCollectedLabel);      
  3.                 FloatingText ft = goldText.GetComponentInChildren<FloatingText>();
  4.                 ft.Text = "+" + myValue.ToString();
  5.                
  6.                 Vector3 screenPos = Camera.mainCamera.WorldToScreenPoint( transform.position );
  7.                 Vector3 guiCameraPosition = guiCamera.ScreenToWorldPoint(screenPos);           
  8.                
  9.                 goldText.transform.position = new Vector3(guiCameraPosition.x, guiCameraPosition.y, 0.1f);
  10.                 NGUITools.SetActive(goldText, true);           
  11.                 ft.TweenPositionLeft();
  12.  

Hopefully that's ideal.

Edit.. this somehow broke and I have no idea why.. upgraded to Unity 4 but there are no errors, think it's coincidental.  The objects are where they're supposed to be but the text is simply not there.  Wonder if this has something to do with SetActive() not showing the text? I have the text on another GameObject that I use SetActive() on...
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: ArenMook on December 23, 2012, 07:00:23 AM
FloatingText... sounds like you could have avoided some hassle just by picking up the HUDText package :P

Unity 4 handles object active states completely differently, but using NGUITools.SetActive should work nonetheless. Still, I'd look closer at which objects get activated and which remain inactive.
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: Majicpanda on December 24, 2012, 10:57:24 AM
Last issue I noticed is AddChild instantiates the prefab at localScale 1,1,1 so that was causing some issues.  Is there a way to pass an additional parameter to prevent this or why is that the default?  I would think a prefab text to 32,32,1 for a label would keep its size.  The only way around would be to put the label on another GameObject in my prefab and then have to use scaling like .05,.05,1 which is less intuitive to me.

Right now I've had to create a public Vector3 TextSize {} and set it during runtime... not sure what kind of problems this might create later on though.

I considered HUDText.. funds a bit tight around Christmas right now ;P  I'm just presenting labels on the screen... not tracking objects, but I'll probably end up buying it when we start dev of our TD.


Edit:  Appears changing the label color during runtime makes the text not visible?
  1.                 //flashy text to show worth of clicked gold
  2.                 GameObject ftParent = NGUITools.AddChild(FloatingTextParent, FloatingTextObject);      
  3.                 FloatingText ft = ftParent.GetComponentInChildren<FloatingText>();
  4.                 ft.Text = myPenaltyString.ToString();
  5.                 ft.TextSize = new Vector3(32f,32f,1f);
  6.                 ft.TextColor = ft.myTextColorRed;
  7.  

Everything looks good in inspector, but commenting out ft.TextColor makes everything run fine while changing it makes it invisible unless I initiate a TweenAlpha.. then it suddenly shows up and fades.
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: ArenMook on December 24, 2012, 11:20:24 AM
Add a parent object, and make a prefab out of that parent, not the child. You shouldn't be instantiating widgets directly. Instantiate groups of widgets instead.
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: ArenMook on December 24, 2012, 11:30:31 AM
I dont know what this "TextColor" property is. NGUI widgets have a "color" property. Furthermore, I can't support this "FloatingText" script as I have no idea what it is or who wrote it -- but it sure wasn't me. :P

HUDText... now that one I can support. And all of this is already handled for you in that package.
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: Majicpanda on December 24, 2012, 11:35:09 AM
Ok I appreciate the support.  I wrote the script myself, it's just 1 file.. so you're directly supporting my code ;)  I'll try to muster up the $20 lol.

Edited: Scale works properly.. not sure why it didn't before.

Oh and TextColor is simply: (Where _lbl is the UILabel widget.
  1.         //set the text color
  2.         public Color TextColor {
  3.                 get { return _lbl.color; }
  4.                 set { _lbl.color = value; }
  5.         }
  6.  

Fixed this also lol.

Setting Color.Red instead of making a public Color; on my FloatingText script so that I can do it in the inspector apparently doesn't anger your nGUI as much for some reason.  I just set a local private variable inside the script instead...
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: Majicpanda on December 24, 2012, 07:30:52 PM
Can anyone tell me why the commented out code works and correctly places the object at the targetPos, but using TweenPosition causes the object to tween to the middle of my screen regardless of what I set for the targetPos?  I could do targetPos = new Vector3(999,999,1) and it still animates my object to the middle of the screen.  Baffled.

This code is on a widget that is on a parent GameObject.

  1.         //move text left (used for positive gains)
  2.         public void TweenPositionLeft() {
  3.                 Vector3 startPos = myParent.transform.position;
  4.                 Vector3 targetPos = new Vector3(startPos.x - tweenLeftDistance, startPos.y, startPos.z);
  5.                 print ("startPos : " + startPos + ", targetPos: " + targetPos);
  6.                 TweenPosition.Begin(myParent, 0.5f, targetPos);
  7.                 //myParent.transform.position = targetPos;
  8.         }
  9.  

Apparently you can't tween the parent gameObject, I changed this to:
TweenPosition.Begin(gameObject, 0.25f, new Vector3(-50f, 25f, 0f));
Title: Re: Instantiating Label Prefabs with TweenPosition Attached
Post by: ArenMook on December 24, 2012, 08:24:10 PM
You might be setting it wrong somehow. In any case, if you are starting a new tween, always use the Begin method. If you have a pre-configured script (already attached to an object and you're setting from / to on it via inspector), then you don't have to.