Author Topic: Custom Anchors on hover?  (Read 9783 times)

aholla

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 40
    • View Profile
Custom Anchors on hover?
« on: July 30, 2014, 04:50:59 AM »
Hi, I am changing the anchors on some buttons at runtime which works but when I "hover" over the button the hover state does not account for the new anchors.

eg: My buttons are 100px by 100px. They are anchored. I adjust the anchors so the buttons are not 50px by 50px. When I hover over the buttons, the hover state is displayed at 100px by 100px not honoring its anchors.

Also I am using UI2DSprites.

  1. UI2Dsprite btn;
  2.  
  3. if( btn.isAnchored ) {
  4.     btn.leftAnchor.absolute /= 2;
  5.     btn.rightAnchor.absolute /= 2;
  6.     btn.bottomAnchor.absolute /= 2;
  7.     btn.topAnchor.absolute /= 2;
  8.  
  9.     btn.ResetAnchors();
  10.     btn.UpdateAnchors();
  11. }
  12.  
  13.  

Thanks for any help.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Anchors on hover?
« Reply #1 on: July 30, 2014, 10:41:27 AM »
What happens in the hover state? Are you changing this in the hover, and that's it? Works fine on my end, here is what I tried:

1. ALT+SHIFT+S, ALT+SHIFT+C, anchor (unified)
2. Attached this script to it:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         void OnHover (bool isOver)
  6.         {
  7.                 UIRect rect = GetComponent<UIRect>();
  8.  
  9.                 if (isOver)
  10.                 {
  11.                         rect.leftAnchor.absolute -= 50;
  12.                         rect.rightAnchor.absolute += 50;
  13.                 }
  14.                 else
  15.                 {
  16.                         rect.leftAnchor.absolute += 50;
  17.                         rect.rightAnchor.absolute -= 50;
  18.                 }
  19.                 rect.ResetAnchors();
  20.                 rect.UpdateAnchors();
  21.         }
  22. }
3. Play, hover over -- sprite grows. Hover out -- sprite shrinks.

aholla

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 40
    • View Profile
Re: Custom Anchors on hover?
« Reply #2 on: July 31, 2014, 03:40:39 AM »
AH... So I am not doing anything on the "OnHover" method. I thought that because I had set the achor on my UIButton, any sprites it uses for its states would also use the anchor.

So do I have to attach a custom OnHover to all my buttons? Can I not set it to the UIButton?

Thanks.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Anchors on hover?
« Reply #3 on: July 31, 2014, 11:14:36 AM »
You said you change anchors when you hover over a button. How are you doing it if you're not using OnHover? The UIButton component can change the color of something or change the sprite -- it has no logic for changing anchors. I'm not sure what it is you're doing and where...

aholla

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 40
    • View Profile
Re: Custom Anchors on hover?
« Reply #4 on: August 04, 2014, 11:24:42 AM »
Hi, sorry for the confusion, heres what I am doing.

I have a bunch of UIButtons. I have set the anchors of these via the inspector. I also have a "Manager" script and in its "start" method, I iterate over the buttons and changes their anchors (if dpi is low, I half the size anchors with the code posted above).

So this results in my buttons still being anchored similar to how i set them up in the inspector but their size is halved. This is what I would expect. The problem is that when I "Hover" over these buttons, the UIButton shows the "hover" state which is a different sprite. This hover state does not have the scaled anchor applied to it.

I would have thought that the hover state would have the same anchor settings as the "normal" state but it does not.

The only fix I can see is to use the OnHover callback and set the anchors every time I do a "hover" event.

Does that make sense?
« Last Edit: August 04, 2014, 11:33:22 AM by aholla »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Anchors on hover?
« Reply #5 on: August 04, 2014, 11:49:40 AM »
Why are you doing any of this? Why not just have a Fixed Size UIRoot? Then everything will be scaled automatically.

In any case all the button component does is changes the sprite name of a UISprite. It doesn't change dimensions if the sprite happens to be anchored. All that's changing is the texture that appears in the area covered by the sprite.

aholla

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 40
    • View Profile
Re: Custom Anchors on hover?
« Reply #6 on: August 05, 2014, 04:10:23 AM »
Hi, so I am doing this as it is a special design request that the buttons be smaller on certain devices. I have to manage this manually and hence the scaling of anchors. They are already using a Fixed size root but on some devices they have to change size for different layout designs.

I just testing my code using the older "UISprite" and it works. "UI2DSprite" does not work.

If you create a Sprite with a UIButton component and set a hover sprite and give the buttons a Unified anchor. Then attach a custom component and add the following code to its "Start()" method you will see that as you hover over the button, the new sprite appears and is also scaled based upon the anchor.

  1. void Start() {
  2.     UISprite sp = gameObject.GetComponent<UISprite>();
  3.     if( sp.isAnchored ) {
  4.         sp.leftAnchor.absolute /= 2;
  5.         sp.rightAnchor.absolute /= 2;
  6.         sp.bottomAnchor.absolute /= 2;
  7.         sp.topAnchor.absolute /= 2;
  8.  
  9.         sp.ResetAnchors();
  10.         sp.UpdateAnchors();
  11.     }
  12. }
  13.  

If you do the same as above but add a UI2DSprite, when you hover over the hover state does not appear scaled but in its original size and when you roll off the button the normal state also appears in its original size. The anchors are ignored.

Would it be possible to have the UI2DSprite work like the UISprite?

Also I tried the code you suggested above on my hover but due to my anchors scaling, the buttons move causing an "hoverOn" > "hoverOff" > "hoverOn" > "hoverOff" loop.
« Last Edit: August 05, 2014, 05:06:00 AM by aholla »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Custom Anchors on hover?
« Reply #7 on: August 05, 2014, 01:36:59 PM »
Tried it. Works just fine and the widget doesn't change dimensions on hover, but I had to change your code from UISprite to UIRect, because UI2DSprite is not a UISprite.

1. New scene.
2. ALT+SHIFT+D, resized the widget.
3. Add UIButton script to it.
4. Choose a hover sprite.
5. Turn on Unified anchoring.
6. Attached the script:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         void Start ()
  6.         {
  7.                 UIRect sp = gameObject.GetComponent<UIRect>();
  8.                 if (sp.isAnchored)
  9.                 {
  10.                         sp.leftAnchor.absolute /= 2;
  11.                         sp.rightAnchor.absolute /= 2;
  12.                         sp.bottomAnchor.absolute /= 2;
  13.                         sp.topAnchor.absolute /= 2;
  14.  
  15.                         sp.ResetAnchors();
  16.                         sp.UpdateAnchors();
  17.                 }
  18.         }
  19. }
Everything works as expected. What version of NGUI are you using?

aholla

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 40
    • View Profile
Re: Custom Anchors on hover?
« Reply #8 on: August 07, 2014, 11:25:37 AM »
Hmmm... Im on version 3.6.7, i skipped the latest because of a few other issues.

In the mean time I found another solution, I put all my buttons inside a widget and anchored the widget and scaled that instead. It works so will keep it like that until next time.

Thanks for your help and looking into this.