Author Topic: Changing a UISprite sprite within same atlas causes anchors to break  (Read 4391 times)

greyhoundgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 39
    • View Profile
Situation. I have Widgets A, B and C nested in each other in that order
B is anchored to A flush with its sides. C is anchored to B, a percent of its size

Everything works correctly with my setup until I call this code
  1.         sprite.spriteName = newSpriteFrameName;
  2.         UISpriteData spriteData = sprite.atlas.GetSprite(newSpriteFrameName);
  3.         sprite.aspectRatio = (float)spriteData.width / (float)spriteData.height;
  4.         sprite.Update();
  5.  

I have tried calling UpdateAndResetAnchors on B and C but it did not help. If I poke at the anchors while the thing is running, eventually it figures out what it was supposed to do. Thoughts?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing a UISprite sprite within same atlas causes anchors to break
« Reply #1 on: June 01, 2015, 08:33:09 PM »
Aspect ratio setting only works if the sprite is partially anchored. If all 4 sides are anchored (as is the case with uniform anchoring), then the aspect ratio cannot be maintained. You can force all anchors to update by using A.BroadcastMessage("UpdateAnchors");

greyhoundgames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 39
    • View Profile
Re: Changing a UISprite sprite within same atlas causes anchors to break
« Reply #2 on: June 03, 2015, 01:25:50 PM »
From what I have seen when you have based-on-height aspect ratio the left and right anchors work, they just basically effect the center which is perfectly fine. My only issue was the anchors not updating. I believe there is more going on here because the sprite who I am changing things on is a child of the guy who's anchors are not working. So B in this case is not having any modification done to it, but its dimensions get stuck at 2x2 when c's sprite changes. I tried the broadcast technique on both A and B and C but it did not help

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing a UISprite sprite within same atlas causes anchors to break
« Reply #3 on: June 07, 2015, 12:34:42 PM »
If you want anchors to update, then you need to set them, not the aspect ratio.
  1. widget.rightAnchor.absolute = Mathf.RoundToInt(aspect * widget.rightAnchor.absolute);