Author Topic: Advanced Anchors at runtime  (Read 2140 times)

Carbonite

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Advanced Anchors at runtime
« on: June 18, 2014, 04:19:07 PM »
I haven't been able to find anything on setting advanced anchors at runtime. SetAnchor seems to only setup a unified anchor. I added an overload to fix this. If it is the correct way, could we get it added to the next update?

        /// <summary>
   /// Anchor this rectangle to the specified transform using an advanced anchor system.
   /// </summary>
   
   public void SetAnchor (GameObject leftGO, int left, GameObject bottomGO, int bottom, GameObject rightGO, int right, GameObject topGO, int top)
   {
      Transform tl = (leftGO != null) ? leftGO.transform : null;
      Transform tr = (rightGO != null) ? rightGO.transform : null;
      Transform tt = (topGO != null) ? topGO.transform : null;
      Transform tb = (bottomGO != null) ? bottomGO.transform : null;
      
      leftAnchor.target = tl;
      rightAnchor.target = tr;
      topAnchor.target = tt;
      bottomAnchor.target = tb;
      
      leftAnchor.relative = 0f;
      rightAnchor.relative = 1f;
      bottomAnchor.relative = 0f;
      topAnchor.relative = 1f;
      
      leftAnchor.absolute = left;
      rightAnchor.absolute = right;
      bottomAnchor.absolute = bottom;
      topAnchor.absolute = top;
      
      ResetAnchors();
      UpdateAnchors();
   }

Carbonite

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Advanced Anchors at runtime
« Reply #1 on: June 18, 2014, 04:33:32 PM »
Well I just realized, if it's advanced, I will need to set the relatives as well. sorry for the confusion. Is this the correct way?

/// <summary>
   /// Anchor this rectangle to the specified transform using the advanced system. Relative values are a scale from 0 to 1, 0 being left/bottom, 1 being right/top.
   /// </summary>
   
   public void SetAnchor (GameObject leftGO, int leftA, float leftR, GameObject bottomGO, int bottomA, float bottomR,
                          GameObject rightGO, int rightA, float rightR, GameObject topGO, int topA, float topR)
   {
      Transform tl = (leftGO != null) ? leftGO.transform : null;
      Transform tr = (rightGO != null) ? rightGO.transform : null;
      Transform tt = (topGO != null) ? topGO.transform : null;
      Transform tb = (bottomGO != null) ? bottomGO.transform : null;
      
      leftAnchor.target = tl;
      rightAnchor.target = tr;
      topAnchor.target = tt;
      bottomAnchor.target = tb;
      
      leftAnchor.relative = leftR ;
      rightAnchor.relative = rightR ;
      bottomAnchor.relative = bottomR ;
      topAnchor.relative = topR ;
      
      leftAnchor.absolute = leftA;
      rightAnchor.absolute = rightA;
      bottomAnchor.absolute = bottomA;
      topAnchor.absolute = topA;
      
      ResetAnchors();
      UpdateAnchors();
   }
« Last Edit: June 18, 2014, 05:04:56 PM by Carbonite »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Advanced Anchors at runtime
« Reply #2 on: June 18, 2014, 05:57:09 PM »
When function has that many parameters, it's generally better to reconsider writing it. You can set both relative and absolute on an anchor like so:
  1. leftAnchor.target = target;
  2. leftAnchor.Set(relative, absolute);

Carbonite

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Advanced Anchors at runtime
« Reply #3 on: June 18, 2014, 06:10:45 PM »
copy. So use 8 lines of code for all four sides, plus the resetAnchor and UpdateAnchor calls instead of a beast of a function call. Was that anywhere in your tutorials? Sorry if it was, I looked for hours. Thanks for the reply.
« Last Edit: June 18, 2014, 06:50:00 PM by Carbonite »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Advanced Anchors at runtime
« Reply #4 on: June 19, 2014, 05:51:06 PM »
For now yeah. I actually added a single function for that that takes all 3 -- the target, relative and absolute that you will find in the next update. And yes, you still need to call ResetAnchors() and UpdateAnchors() afterwards.

Carbonite

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 7
    • View Profile
Re: Advanced Anchors at runtime
« Reply #5 on: June 21, 2014, 04:00:01 AM »
Thank you. I love your work btw. Making my life so much easier.