Author Topic: understanding anchors at runtime  (Read 8225 times)

friken

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
understanding anchors at runtime
« on: November 04, 2014, 07:28:00 PM »
I've looked at SetAnchor() and rightAnchor, topAnchor, absolute, relative values... etc and I just don't quite get it. In the editor this setup is exactly what I want, but I am trying to recreate at runtime (stays correct aspect and top-right anchor on any screen aspect):



I've tried a number of different SetAnchors() etc and obviously I'm not understanding the parameters. Further, I don't understand why why I make a prefab with the above settings it always drops the anchor back to none.


BeShifty

  • Jr. Member
  • **
  • Thank You
  • -Given: 5
  • -Receive: 7
  • Posts: 52
    • View Profile
Re: understanding anchors at runtime
« Reply #1 on: November 04, 2014, 09:25:52 PM »
Notice that your Target field is bold, this means it's different from the prefab. One limitation of all prefabs in Unity is that you can't reference objects in the scene (and not in the prefab), since that object doesn't exist separately in the project, meaning you can't guarantee its existence when you instantiate the prefab. The prefab therefore has anchoring None. You have a couple options:

A) Put a panel at the root of the prefab and treat it as the screen, since panels assume the screen's dimensions (unless the panel also has anchoring set)
B) Assign the anchor at runtime (could be nicely component-ized if it looks for a root/panel in the runtime parent)

friken

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: understanding anchors at runtime
« Reply #2 on: November 04, 2014, 09:36:03 PM »
Thx for the response. I get that the target connection to something in the scene won't save on the prefab, but the type itself is being set to none. I expected the prefab to keep the type and everything but the target and runtime set just the target.

Option A, I would end up with a bunch of panels instead of reusing the one. so OptionB is for me.

Option B is where I am confused how the anchors work. I've tried working w runtime assigning the anchor without luck.

friken

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: understanding anchors at runtime
« Reply #3 on: November 04, 2014, 10:25:53 PM »
I did get this working... however, I find it a bit confusing and the documentation I think could be better. What I needed is something like this:

        background.SetAnchor(uiRoot);
        background.leftAnchor.Set(1f, -475); //right edge of screen minus width of sprite
        background.rightAnchor.Set(1f, 0); //no idea why this is 1f? wouldn't 0f be right edge since the call is to rightAnchor?
        background.topAnchor.Set(1f, -150);//150 is offset from top edge
        background.bottomAnchor.Set(1f, -400);  // 400 = -150 offset - height of sprite
        background.ResetAnchors();
        background.UpdateAnchors();

7 calls of somewhat cryptic params for something that imo could be a simple single call:

UISprite.SetAnchor(Transform parent, bool left, bool right, bool top, bool bottom);

Some things can be assumed like a right anchor anchors to right side of sprite (what's the point to anchor it starting offscreen?) left anchor to the left side of the sprite, etc.
anchor both right/left or top/bottom and assume stretching, etc or add a stretch bool. If multiple sides are anchored without stretch then center between the two relative to starting position %s. If you are setting anchors in code chances are you already set the position you want the sprite in prior to anchoring. The editor tool does this for you perfectly well, how about a simple helper to do the same and make the same assumptions. Keep the tedious way for those that have the rare exception.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: understanding anchors at runtime
« Reply #4 on: November 06, 2014, 01:24:09 AM »
left/right anchors are horizontal: 0 = left, 1 = right.
top/bottom anchors are vertical: 0 = bottom, 1 = top.