Author Topic: Problem Anchoring Label to Game Object In Code.  (Read 2427 times)

DanSuperXeo

  • Guest
Problem Anchoring Label to Game Object In Code.
« on: December 19, 2013, 08:36:30 PM »
Hi,

I'm running into a really weird problem with the new Anchoring System with trying to anchor to a game object in code.

What I'm attempting to do is to respond to a scoring event by instantiating a game object at the desired location and a UILabel which is then anchored to that game object in order to create a floating score display above that point on the play field.

The behavior that I'm getting that I find problematic is that, rather than the label appearing at the location of the game object, it appears in the center of the screen and then goes shooting off to bottom left at high speed.

When the event is triggered, I instantiate a marker game object and a label game object. I parent the Label game object to the NGUI Camera, and the marker keeps a reference to it.

  1.             Object _PopUpGuiObject = Instantiate(m_PopUpScoreGUIDisplayPrefab);
  2.                         Transform _PopUpGuiTransform = _PopUpGuiObject as Transform;
  3.  
  4.                         _PopUpGuiTransform.parent = PopUpParentLocator.Instance.transform;
  5.                         _PopUpGuiTransform.localPosition = Vector3.zero;
  6.                         _PopUpGuiTransform.localScale = Vector3.one;
  7.  
  8.                         _PosMarker.PopUpScoreGUIDisplayTransform = _PopUpGuiTransform;
  9.              
  10.  

The script on the marker sets the text and color for the label and then sets itself as the anchor. It seems no matter whether I set anchor.relative or anchor.absolute or not set any offsets for the anchor at all I get the same behavior.

  1.                         m_PopUpScoreLabel.leftAnchor.target = transform;
  2.                         m_PopUpScoreLabel.rightAnchor.target = transform;
  3.                         m_PopUpScoreLabel.topAnchor.target = transform;
  4.                         m_PopUpScoreLabel.bottomAnchor.target = transform;
  5.                         //m_PopUpScoreLabel.leftAnchor.relative =  (m_PopUpScoreLabel.width /2) * -1 ;
  6.                         //m_PopUpScoreLabel.bottomAnchor.relative = ( m_PopUpScoreLabel.height / 2 ) * -1 ;
  7.                         //m_PopUpScoreLabel.rightAnchor.relative = (m_PopUpScoreLabel.width /2) ;
  8.                         //m_PopUpScoreLabel.topAnchor.relative = ( m_PopUpScoreLabel.height / 2 );
  9.  
  10.                         m_PopUpScoreLabel.hideIfOffScreen = false;
  11.  
  12.                         m_PopUpScoreLabel.AssumeNaturalSize();
  13.  
  14.  

Can you tell me what I'm doing wrong? I can't seem to set any game object as an anchor in code without experiencing this same behavior.

DanSuperXeo

  • Guest
Re: Problem Anchoring Label to Game Object In Code.
« Reply #1 on: December 19, 2013, 09:01:33 PM »
Ok, looking through several pages of the forums I discovered I need to call ResetAnchors, and this keeps the label from zooming off into the distance.

However I'm still experiencing several issues.

Whenever I set the anchor, the widget dimensions of the label changes to a narrower width than I had manually set it to. This causes my score display to wrap when I don't want it do. I would like the label size to stay the same size or at least resize to be the width of the text I've set it to.

The label appears offset to the left from where I would like it to appear. It should appear exactly centered on the object it is anchored too.

« Last Edit: December 19, 2013, 09:42:34 PM by DanSuperXeo »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Problem Anchoring Label to Game Object In Code.
« Reply #2 on: December 20, 2013, 11:53:31 AM »
When a widget is anchored to a game object, relative values have no effect because the game object has no width or height. Relative values only matter when anchoring to a rectangle (widget, panel or camera).

I see your code calls AssumeNaturalHeight, but this will get overwritten by your anchor's settings. I don't see the code where you set the absolute values, and that's what controls the dimensions of your widget.

To make your widget be 200 pixels by 30 pixels, give the following absolute anchor values:
Left: -100
Right: 100
Bottom: -15
Top: 15