Author Topic: UIAnchor and UIStretch option to only run in Start()  (Read 12557 times)

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
UIAnchor and UIStretch option to only run in Start()
« on: July 20, 2012, 05:30:09 AM »
I found that UIAnchor and UIStretch will update its position on every frame. But for mobile device, it is not necessary, can you add an option to only run on Start()? I know it is easy to change to code, but I wish it can be officially support it.

Xanatus

  • Guest
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #1 on: July 20, 2012, 11:17:10 AM »
UIAnchor and UIStretch only update if positions/scales change.
And if you target a single resolution, why use stretch and anchor at all?
« Last Edit: July 20, 2012, 11:33:20 AM by Xanatus »

yuewah

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 180
    • View Profile
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #2 on: July 20, 2012, 12:41:44 PM »
It supports iphone and iPad resolution, but it will not resize/change the resolution during the gameplay.

vip_prizrak_3

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #3 on: May 30, 2013, 06:59:07 AM »
It's a good idea. In Profiler UIAnchor.Update use 3-7% of CPU Usage.
I disable update after 2 seconds after the start, and CPU usage dropped to 0, and all elements are in their place.

UIAnchor.cs
bool alreaduInPosition = false;
void Start ()
{
   ...
   Invoke("StopUpdate", 2.0f);
}
void StopUpdate()
{
    alreaduInPosition = true;
}
void Update ()
{
    if (alreaduInPosition) return;
    ...
}

It would be very nice if had realized the possibility of disabling after the start.

dlewis

  • Guest
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #4 on: May 30, 2013, 07:51:00 AM »
I have also encountered an issue where UIAnchor would update every frame (lots of them), although it was always moving in the z axis which was weird. I moved off the project before I could do serious debugging of it (but I don't know how/why it would always move towards the camera in the z axis)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #5 on: May 30, 2013, 03:53:28 PM »
I can add an option to remove the UIAnchor after the Update has been called once, but this will prevent the elements from adjusting to screen resolution changes.

Ideally there should be some kind of an event within Unity that notifies of screen size changes...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #6 on: May 30, 2013, 03:58:19 PM »
I've added an option for it to the UIAnchor. You will see it in the next update.

vip_prizrak_3

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 31
    • View Profile
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #7 on: May 31, 2013, 04:50:11 AM »
Thanks)

regeter

  • Guest
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #8 on: August 31, 2013, 03:52:39 PM »
I use 8 buttons that are using the anchors. Everything was great, but now I decided to check "Run only once" on each button. The end result that only some (5 out of the 8) of the buttons now gets correctly aligned.  Should this not be per instance of the script?
I had to re-disable this, and everything is back to normal.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #9 on: August 31, 2013, 08:23:21 PM »
When you're nesting anchors, each anchor won't know who goes first, so you will get potentially weird movements if you let them turn off after only one frame.

Optimally, you'd run them downwards in the hierarchy once and then turn them off.

You can make your own script that runs each anchor's update() and sees if the position changed because of it, if it hasn't then destroy the anchor. Make sure you run this in the right order though.

regeter

  • Guest
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #10 on: September 04, 2013, 09:47:09 AM »
Thank you for your help but I don't think they are in a hierarchy, they are siblings really.

You can see this pictures. Each corner has two circles. With this option enabled to just run once some of the circles are out of place.

regeter

  • Guest
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #11 on: September 04, 2013, 09:49:00 AM »
In the scene view they look ok, but in the actual game they now are out of place.

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #12 on: September 04, 2013, 06:12:22 PM »
Yeah, but I mean in the anchoring hierarchy. I assume one widget is anchored to the screen and then the others anchor to the other ones? Or how is that setup?

There's a chain of dependencies there that needs to be done in the correct order if you want to only run the anchor once.

regeter

  • Guest
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #13 on: September 04, 2013, 08:36:01 PM »
no they are all anchored to the panel...
but you made me check the scene again and I found the issue.
The anchor script can assign a panel gameobject and I had this empty. Now I assigned the correct panel from the hierarchy and the "Run Only Once" option now works fine.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIAnchor and UIStretch option to only run in Start()
« Reply #14 on: September 05, 2013, 05:09:59 AM »
Note that you still have nested anchors. You have the "Anchor" game object, and then your buttons have their own anchors.