- Home /
5.5.1f1 new UI system: Button in scrollview is not working on Android.
It works perfectly in the editor, but on Android it only sometimes registers an OnClick when I press it with my finger.
I'm on Unity 5.5.1f1
Is this a known issue? I'll post some videos of it if no-one has seen this before.
The scrollview is based on the default one from the Create\UI\scrollview menu Ive added a vertical layout group to it, to which I add prefabs that contain buttons. These are the buttons that are not working. The canvas is in front of the camera in worldspace and is scaled down to 0.02 in X and Y
VVVVVVVVVVV UPDATE VVVVVVVVVVVVV
I wrote my own workaround using 2D colliders instead of buttons The colliders are registering hits just fine. But when I wrote a class to not register hits if the scrollview moves, I get similar behaviour! Heres the crude little workaround class I wrote..
public class ScrollButton : MonoBehaviour
{
public StoreItem Item;
bool FingerDownOnMe;
public void BeOnClicked()
{
VisualDebuggerRef.SetTxt("a store button" + " " + Random.Range(0, 10000));
FingerDownOnMe = true;
}
// This is called on all these objects whenever the scrollview moves
public void ScrollviewMoved()
{
FingerDownOnMe = false;
}
void Update()
{
// 'UppedthisFrame' will be true whenever any touchs phase == TouchPhase.Ended
if (Refs.man.Clickdetect.UppedthisFrame && FingerDownOnMe)
{
FingerDownOnMe = false;
Item.ButtonHit();
}
}
}
I'm guessing Unities UI classes work in a somewhat similar way to my workaround one. The problem is that the scrollview is registering as being moved when it should not be. This may be because of my test device which is an S7. Maybe the touch resolution is so high it is registering movement that is smaller than one pixel??
Also maybe the fact the the entire canvas has been scaled down could be affecting this? Say Unities classes have a distance threshold, if they are not taking into account scale then working on a scaled down canvas might make the distances they are using too big, so the threshold is reached too easily
Im going to add a distance limit to my workaround...
VVVVVVVVVVVVV UPDATE2 VVVVVVVVVVVVVVVV
This workaround code using colliders works perfectly..
public class ScrollButton : MonoBehaviour
{
const float scrollThreshold = 0.25f;
public StoreItem Item;
bool FingerDownOnMe;
Vector3 HitPos;
public void BeOnClicked()
{
//VisualDebuggerRef.SetTxt("a store button" + " " + Random.Range(0, 10000));
FingerDownOnMe = true;
HitPos = transform.position;
}
void Update()
{
// 'UppedthisFrame' will be true whenever any touchs phase == TouchPhase.Ended
if (Refs.man.Clickdetect.UppedthisFrame)
{
if (FingerDownOnMe && Mathf.Abs((transform.position - HitPos).y) < scrollThreshold) Item.ButtonHit();
FingerDownOnMe = false;
}
}
}
I'm going to report this issue as a bug to Unity
The issue is most likely related to the prefabs, as they can't access gameObjects present in your hierarchy. If you've assigned the OnClick method from any of the Objects which are present in your hierarchy then the buttons won't work, as at RUNTI$$anonymous$$E, they will loose those reference to OnClick method.
The OP has down voted my comment and your answer without giving any reasons. I don't know what he has in $$anonymous$$d.
Answer by deltron1830 · Feb 27, 2017 at 09:44 PM
This is a bug in Unity. See my updates
ps. I have reported it
UPDATE.. from unity QA
" Hey,
We have been able to reproduce this bug and have sent it for resolution with our developers.
We highly appreciate your contribution. If you have further questions, feel free to contact us.
Regards, [Name removed] QA Team "
Answer by dakshesh1010 · Feb 27, 2017 at 06:20 AM
I did something similar once, instead of buttons as prefabs, I used entire panels (which had so may buttons and images and what not) as prefabs! And I just dragged those prefabs into different scenes.
@aaditya007 is indeed right there. You can use prefabs maybe, but you still have to refer to the method you need to call on OnClick(). If that doesn't work, just don't use prefabbed buttons. Instead make one, and duplicate the same to make new ones. I know it sounds dumb (or not so efficient), but the best practice in such cases (according to me) is to have a central method like a ButtonClickListener() in a Game Manager class, and resolve your button click from there so that you don't need to find the right script and object every time when you need a new button. You know you just need to call the ButtonClickListener() on every button, and that function in-turn resolves everything else!
Your answer
Follow this Question
Related Questions
UI button shows in editor but not on mobile? 0 Answers
Button with Animation disappear in Android build 0 Answers
UI works in editor, but not on mobile device 1 Answer
[5.4 - Unity2D - Android] UI Button Requires Child Text? 0 Answers
UI Button event triggers don't work on mobile but work in eaditor? 0 Answers