Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: AssyriaGameStudio on June 16, 2015, 09:44:48 AM

Title: Anchors issue - background encompass content
Post by: AssyriaGameStudio on June 16, 2015, 09:44:48 AM
Ok so I'm trying to use anchors to position some items essentially my structure is this:

Background of the prompt.

 -- A texture that is anchored to the top of the background at 0, left of the background at 0, right of the background at 0 and has no bottom anchor, with an aspect of 2.
 --Some text, anchored to the left of the background at 0, right of the background at 0 and the top anchor is set to the bottom of the texture (aka should be right below it.)
-- Some more text anchored to the bottom of the previous text, with left and right anchored to the background.
-- Finally I anchor the bottom of the background to the bottom of the text (so it encompases the full text / images).

(Basically I want everything to stick to the background width, be arranged below the previous item regardless of size, and for the background to encompass the lot). At this point I should note that the text is being updated at runtime, but before the item is enabled (although in the same frame).

Now, after trying OnEnable with the anchors that hasn't worked; I presumed because the anchoring happened in the wrong order.

I've now tried to include anchor updating in my script:

  1.         public UISprite background_sprite;
  2.         public UITexture header_image;
  3.         public UILabel time_label;
  4.         public UILabel event_description;
  5.  

  1.        
  2.  
  3. background_sprite.UpdateAnchors();
  4.  
  5. header_image.aspectRatio = 1.0f / orca_event.cover_image_ratio;
  6. time_label.text = orca_event.event_time.ToString();
  7. event_description.text = orca_event.event_description;
  8.  
  9. header_image.UpdateAnchors();  
  10. time_label.UpdateAnchors();
  11. event_description.UpdateAnchors();
  12. background_sprite.UpdateAnchors();
  13.  
  14.  

Most of this all works as expected; apart from the background which is still over-sized / has a height higher than that of the content (loads of space a the bottom). It'd be great to know what I'm doing wrong as for the life of me I don't know what I'm doing wrong.
Title: Re: Anchors issue - background encompass content
Post by: ArenMook on June 16, 2015, 12:56:56 PM
Anchors should be automatically updating in the proper order, assuming there are no circular dependencies. The only key is that if you change the width of something manually, and it's anchored, then it won't work. You need to change the anchors instead. I can only suggest checking your anchoring dependencies. A anchored to B, when B is anchored to C, and C anchored back to A would most certainly cause a problem. Simplifying the hierarchy usually works. Making sure that all of those elements are siblings, and not children of one another should help. Using a UITable is another option.
Title: Re: Anchors issue - background encompass content
Post by: AssyriaGameStudio on June 17, 2015, 07:24:29 AM
So I think the issue is that it is circular.

I have the background anchored to the top of the first item.

I then have the background also anchored to the bottom of the last item (so as to encompass a few items all anchored together). I've get the pivot point of the background to the top center thinking it would just scale the height via the anchor, but I guess this won't work.

If circular anchors aren't suitable then what would be the idea way to implement this instance where by I have a background that needs to scale to cover the backing of multiple anchored elements?

Essentially my structure is:

Top of background (anchored to the top of the first text item).
-- Text.
-- Text anchored to the bottom of the above.
-- Text anchored to the bottom of the above.
Bottom of background (anchored to the bottom of the last text item).

Is there a way of figuring out the height from the top of the first item to the bottom of the last and just setting the background height to that? I've attempted finding the difference between top_text.GetSides(transform)[1].y and bottom_text.GetSides(transform)[3].y but that doesn't appear to return the correct height value?
Title: Re: Anchors issue - background encompass content
Post by: AssyriaGameStudio on June 17, 2015, 08:47:25 AM
Ok it looks like -(event_description.GetSides(transform)[1].y - event_description.height) but only if I wait 1 frame between changing the "event_description" text.

Is there anyway to force the text update and GetSides update instantly without having to wait a frame?

I also appear to be having similar issues with some of the other text items anchored to text items they're anchored to. The bottom isn't anchored reported correctly in all instances when I call UpdateAnchors(), despite having updated the text of the given UILabel prior to this and having called Update() on the label prior to calling GetSides.

Full Code:


  1.        
  2. public void Init (SomeClass evt)
  3.         {
  4.                 the_thing = evt;
  5.                 time_label.text = the_thing.event_time.ToString();
  6.                 distance_label.text = the_thing.Distance();
  7.                 description.text = the_thing.description;
  8.  
  9. // time_label is anchored to the top of background_sprite
  10.                 time_label.UpdateAnchors();
  11.                 time_label.Update();
  12.  
  13. // distance_label is anchored to the bottom of time_label
  14.                 distance_label.UpdateAnchors();
  15.                 distance_label.Update();
  16.  
  17. // description is anchored to the bottom of distance_label
  18.                 description.UpdateAnchors();
  19.                 description.Update();
  20.  
  21.                 StartCoroutine(ResizeBG());
  22.         }
  23.  
  24.         IEnumerator ResizeBG()
  25.         {
  26.                 yield return new WaitForEndOfFrame();
  27. // Gets the local position at the bottom of the final text item to determine the background height.
  28.                 int height_val = Mathf.RoundToInt(-description.GetSides(transform)[3].y);
  29.                 background_sprite.height = height_val;
  30.         }
  31.  

Edit: I've also attempted calling ProcessText () and UpdateNGUIText (); on the labels ahead of calling the anchor functions in the hope it was just that the text was updating after the anchors were updated, however this didn't change anything / the background sprite still ended up the wrong size is a 1 frame wait wasn't included.

I should also mention at this point that this "group of text items" forms part of an item to be placed in a UITable list, so I need all of the height and stuff updated immediately so that I can Reposition the table holding them once Init on all the items is complete.
Title: Re: Anchors issue - background encompass content
Post by: ArenMook on June 18, 2015, 07:48:10 AM
You need to rethink your structure to remove the circular dependency. Create an invisible widget container if you need -- ALT+SHIFT+W. Background can envelop the content using EnvelopContent script instead of anchoring. Widgets should be anchored to the invisible container, not the background.