Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: caiorosisca on May 26, 2015, 03:13:00 PM

Title: [HOW TO]Anchor > Set to Current Position by code
Post by: caiorosisca on May 26, 2015, 03:13:00 PM
I'm making some kind of system to navigate through pages, looks like the image below.

(http://s4.postimg.org/y5fqbxbl8/pagesystem.jpg)

When trying to anchor everything to be responsible to screen size and work on tablets with different resolutions (which is what I need) I found out that anchoring each object to it's parent and setting the anchor to 'Set to Current Position' works fine.
What I would like to know is if there's any kind of way to imitate UIWidget's  anchor's 'Set to Current Position' functionality by code, as most of the elements are created dynamically calling a function that does that would be really helpfull.
I've digged the code and ended up in UIRectEditor at this point:

  1. // "Set to Current" choice
  2.                 if (newOrigin == 4)
  3.                 {
  4.                         newOrigin = 3;
  5.  
  6.                         Vector3[] sides = targetRect.GetSides(myRect.cachedTransform);
  7.  
  8.                         float f0, f1;
  9.  
  10.                         if (IsHorizontal[index])
  11.                         {
  12.                                 f0 = sides[0].x;
  13.                                 f1 = sides[2].x;
  14.                         }
  15.                         else
  16.                         {
  17.                                 f0 = sides[3].y;
  18.                                 f1 = sides[1].y;
  19.                         }
  20.  
  21.                         // Final position after both relative and absolute values are taken into consideration
  22.                         float final = Mathf.Floor(0.5f + Mathf.Lerp(0f, f1 - f0, rel.floatValue) + abs.intValue);
  23.  
  24.                         rel.floatValue = final / (f1 - f0);
  25.                         abs.intValue = 0;
  26.  
  27.                         serializedObject.ApplyModifiedProperties();
  28.                         serializedObject.Update();
  29.                 }
  30.  

But I don't know how to turn it into a method I can use.
Title: Re: [HOW TO]Anchor > Set to Current Position by code
Post by: ArenMook on May 26, 2015, 03:25:06 PM
Have you tried searching for the exact text in code? It will point you to exactly the function that's being called.
Title: Re: [HOW TO]Anchor > Set to Current Position by code
Post by: caiorosisca on May 26, 2015, 03:44:21 PM
Have you tried searching for the exact text in code? It will point you to exactly the function that's being called. It should be prefixed with the [ContextMenu] tag with the text within.

I tried, and only found this exact code when declaring the editor“s list at:

  1. static protected string[] HorizontalList = new string[] { "Target's Left", "Target's Center", "Target's Right", "Custom", "Set to Current Position" };
  2. static protected string[] VerticalList = new string[] { "Target's Bottom", "Target's Center", "Target's Top", "Custom", "Set to Current Position" };
  3.  

Couldn't find any ContextMenu stuff linked to it.
Title: Re: [HOW TO]Anchor > Set to Current Position by code
Post by: caiorosisca on May 28, 2015, 12:23:21 PM
Any help on this?
Title: Re: [HOW TO]Anchor > Set to Current Position by code
Post by: mukarillo on May 28, 2015, 12:56:25 PM
I'm having the same issue, can you help us @ArenMook??

Thanks!
Title: Re: [HOW TO]Anchor > Set to Current Position by code
Post by: Arthros on May 28, 2015, 01:04:00 PM
Hello,

I'm glad to see I'm not the only one running through this issue.

I currently have a whole scene full of elements that I want to anchor them all to current position. But I can't seem to find an easy way to do it all at once, without having to go through every single widget and anchoring all the four sides to current position.

Thanks
Title: Re: [HOW TO]Anchor > Set to Current Position by code
Post by: ArenMook on June 01, 2015, 08:04:34 PM
My bad on the ContextMenu part, I was thinking of a function. That code is a little confusing I admit, but do a find on HorizontalList or VeriticalList in that file and you will be taken to this part:
  1.                 // "Set to Current" choice
  2.                 if (newOrigin == 4)
  3.                 {
  4.                         newOrigin = 3;
  5.  
  6.                         Vector3[] sides = targetRect.GetSides(myRect.cachedTransform);
  7.  
  8.                         float f0, f1;
  9.  
  10.                         if (IsHorizontal[index])
  11.                         {
  12.                                 f0 = sides[0].x;
  13.                                 f1 = sides[2].x;
  14.                         }
  15.                         else
  16.                         {
  17.                                 f0 = sides[3].y;
  18.                                 f1 = sides[1].y;
  19.                         }
  20.  
  21.                         // Final position after both relative and absolute values are taken into consideration
  22.                         float final = Mathf.Floor(0.5f + Mathf.Lerp(0f, f1 - f0, rel.floatValue) + abs.intValue);
  23.  
  24.                         rel.floatValue = final / (f1 - f0);
  25.                         abs.intValue = 0;
  26.  
  27.                         serializedObject.ApplyModifiedProperties();
  28.                         serializedObject.Update();
  29.                 }
I believe that's what you were looking for?
Title: Re: [HOW TO]Anchor > Set to Current Position by code
Post by: MoProductions on June 23, 2016, 01:00:48 PM
Is there an example of this being implemented in a runtime situation?  I'm getting close but can't quite get it fully finished.  If I do I'll post it.