Author Topic: UIButtons inside ScrollView work randomly  (Read 7043 times)

jacky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
UIButtons inside ScrollView work randomly
« on: March 14, 2014, 05:05:02 PM »
I have an incredibly maddening issue. I have a UIScrollView with two pages of buttons inside it. All but one of the buttons don't work.

This is the behavior I am experiencing. The numbers in the parentheses are the levels in the hierarchy (see below).

Scenario 1:
* Deactivate and reactivate the gameobject (1) that is the parent of the buttons, all of the child buttons (2) work (page A).
* Scroll to the next page (page B) and back (page A), the buttons (2) continue to work.
* Scroll to the next page (page B) and deactivate/reactivate the buttons, the ones on that page work (page B) and the SOME of the buttons on the previous page (page A) stop working.
* Go back and forth and the behavior is the same

Scenario 2:
* Deactivate the Box Collider on the parents of the buttons (1) and the buttons work but the menu does not scroll

Here is my hierarchy:

(0)--ScrollView, UIPanel, UIScrollView, UICenterOnChild
(1)----UIDragScrollView, Box Collider w/depth -20
(2)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification
(2)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification
(2)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification
(2)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification
(2)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification
(0)----UIDragScrollView, Box Collider w/depth -20
(1)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification
(2)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification
(2)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification
(2)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification <---- this one works
(2)------UISprite, UIDragScrollView, Box Collider w/depth 3, UIButton w/OnClick notification

EVERYTHING is exactly the same on all of the objects. When I run the scene, the only difference between the one that works and the one that doesn't is the length of the color tween. On all of the broken ones it's .15, the one that works it's .2. I don't know what that could possibly do but it's the only difference I could see.

Any help would be very much appreciated. I can't find ANYTHING that could be causing this seemingly random behavior. If ALL of the buttons did not work it would make a lot more sense but this really doesn't make any sense. Thanks!

jacky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #1 on: March 14, 2014, 06:57:41 PM »
Also, I noticed that the UICenterOnChild.onFinished is being called every click...

jeffpk

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #2 on: March 14, 2014, 07:09:26 PM »
This

"Deactivate the Box Collider on the parents of the buttons (1) and the buttons work but the menu does not scroll"

Suggests you have a depth problem and something invisible is blocking the clicks.  My suspicion is that you ar moving the buttons from behind it to not behind it when you scroll around.

Make sure that the buttons' depth are all set well bigger then the rest of the gui.

jacky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #3 on: March 14, 2014, 07:14:30 PM »
jeffpk,

I looked into that but the strange thing is that ONE button is working. If it was a depth problem I would think that NONE of them should work.

Also, scrolling does not make a difference. It's when I scroll to the other menu and activate/deactivate the other "page" that it stops working.

The fact that activating/deactivating the GameObjects has an effect on it makes me think it could be something in the NGUI code that my scene doesn't like. Not necessarily a bug in the code but something I might have attached and/or in the wrong place.

I looked briefly at the depths but I do need to look more into it so I will update if that makes a difference.

Thanks!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #4 on: March 14, 2014, 08:11:15 PM »
1. Make sure the UICamera is set to "UI" mode, not "World".
2. Enable "Debug" on the UICamera to see what's intercepting your events.

jacky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #5 on: March 17, 2014, 11:43:06 AM »
Debug mode is very helpful! Thank you!

It still doesn't work though :(

1. The camera has been set to UI mode
2. I enabled Debug and it looks like the parent GameObject (level 2 in the hierarchy below) is intercepting the events. I can't get rid of that collider if I want the menu to scroll though.

It looks like something is going on with the colliders but I don't think I quite understand how the colliders work with the UISprite.

I saw in this post that you should use an invisible collider. Does this also apply to items within a ScrollView?
http://www.tasharen.com/forum/index.php?topic=7292.msg34506#msg34506

I added a collider and then checked auto-adjust to match. In the editor, the coordinates of the collider on all of my sprites are exactly the same for every button. They are somehow in the correct place in the Scene View but I have no way of checking the z-depth because the values in the inspector don't seem to mean anything. I tried changing it manually during play mode and that made no difference.

I also tried changing the z-depth of the parent GameObject to +20 and the buttons to -5. I also changed the clipping planes on the camera but that seemed to make no difference.

Also, on the one button that does work, it gets BOTH clicks (the ScrollView click and the button click). I need to find a way to disable that as well.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #6 on: March 17, 2014, 07:17:28 PM »
When you place a collider on the game object, then the collider's "depth" is the highest collider's depth. You don't want that.

Organize your UI like this:

Game Object (parent)
- UIWidget (collider, covering your scroll area)
- UIScrollView
-- UIGrid, if you need it
--- Button 1
--- Button 2
--- Button 3, etc

jacky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #7 on: March 18, 2014, 02:36:10 PM »
So, I narrowed down the problem a bit.

It works fine with the following hierarchy:
- UIScrollView
-- UIGrid (similar behavior with or without this)
--- Item_01
--- Item_02
--- Item_03...

This, however does not work:

- UIScrollView
-- UIGrid (similar behavior with or without this)
--- Page_01
---- Item_01
---- Item_02
---- Item_03...
--- Page_02
---- Item_03
---- Item_04
---- Item_05...

Would you be able to let me know how to arrange things below the UIGrid so that it will work?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #8 on: March 18, 2014, 03:07:57 PM »
As I said, if you place a box collider on a plain game object then it will use the highest depth widget's depth.

Either use a UIWidget instead of a plain game object (ALT+SHIFT+W), or just don't put colliders on a plain game object.

jacky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #9 on: March 18, 2014, 08:37:10 PM »
Ahh, sorry. That works!

One other question. For various reasons I would rather use UIEventTrigger than UIButton. When I use UIButton, it ignores the click when I am scrolling. Not so with UIEventTrigger. I couldn't find an easy fix. Do you know of anything?

Thanks!

- Jacky

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #10 on: March 18, 2014, 09:25:38 PM »
Click is canceled via UICamera.clickNotification being set to "none". I don't have code in front of me as I'm at GDC, so I would suggest looking at where that happens.

jacky

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Re: UIButtons inside ScrollView work randomly
« Reply #11 on: March 19, 2014, 10:35:47 AM »
Great, thanks! Enjoy the conference!