Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Android Scrollview Fail

Discussion in 'Scripting' started by bommba, Dec 22, 2015.

  1. bommba

    bommba

    Joined:
    Nov 24, 2013
    Posts:
    4
    I´d script a scrollview for android smartphones. When I test it directly per Unity Editor on my cellphone, it functions. But when I export it per apk to my smarthphone, the view goes only down and never up even if I want. Here is the code:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class scrollView : MonoBehaviour
    6. {
    7.  
    8.  
    9.     public Vector2 scrollPosition;
    10.     Touch touch;
    11.     // The string to display inside the scrollview. 2 buttons below add & clear this string.
    12.     string longString = "This is a long-ish string";
    13.     public GameObject button1;
    14.     public GameObject button2;
    15.     public GameObject button3;
    16.     public GameObject button4;
    17.     public GameObject button5;
    18.     public GameObject button6;
    19.     public GameObject button7;
    20.     public float button1Position;
    21.  
    22.     void start() {
    23.         button1.transform.position = new Vector2(0, 0);
    24.     }
    25.  
    26.     void OnGUI()
    27.     {
    28.  
    29.         scrollPosition = GUI.BeginScrollView(new Rect(110, 50, 130, 160), scrollPosition, new Rect(110, 50, 130, 230), GUIStyle.none, GUIStyle.none);
    30.      
    31.         GUI.EndScrollView();
    32.     }
    33.  
    34.     void Update()
    35.     {
    36.         if (Input.touchCount > 0)
    37.         {
    38.             touch = Input.touches[0];
    39.             if (touch.phase == TouchPhase.Moved)
    40.             {
    41.                 scrollPosition.y += touch.deltaPosition.y;
    42.                 button1Position = button1.transform.position.y;
    43.                 if (button1Position < 458 && button1Position > 318)
    44.                 {
    45.                     button1.transform.Translate(scrollPosition * Time.deltaTime * 20);
    46.                     button2.transform.Translate(scrollPosition * Time.deltaTime * 20);
    47.                     button3.transform.Translate(scrollPosition * Time.deltaTime * 20);
    48.                     button4.transform.Translate(scrollPosition * Time.deltaTime * 20);
    49.                     button5.transform.Translate(scrollPosition * Time.deltaTime * 20);
    50.                     button6.transform.Translate(scrollPosition * Time.deltaTime * 20);
    51.                     button7.transform.Translate(scrollPosition * Time.deltaTime * 20);
    52.                 }
    53.                 if (button1Position >= 458)
    54.                 {
    55.                     button1.transform.Translate(0, -4, 0);
    56.                     button2.transform.Translate(0, -4, 0);
    57.                     button3.transform.Translate(0, -4, 0);
    58.                     button4.transform.Translate(0, -4, 0);
    59.                     button5.transform.Translate(0, -4, 0);
    60.                     button6.transform.Translate(0, -4, 0);
    61.                     button7.transform.Translate(0, -4, 0);
    62.                 }
    63.  
    64.                 if (button1Position <= 318)
    65.                 {
    66.                     button1.transform.Translate(0, 4, 0);
    67.                     button2.transform.Translate(0, 4, 0);
    68.                     button3.transform.Translate(0, 4, 0);
    69.                     button4.transform.Translate(0, 4, 0);
    70.                     button5.transform.Translate(0, 4, 0);
    71.                     button6.transform.Translate(0, 4, 0);
    72.                     button7.transform.Translate(0, 4, 0);
    73.                 }                                                                  //21.7
    74.             }
    75.         }
    76.     }
    77. }
    78.  
    Does anybody find a failure?
     
    Last edited: Dec 22, 2015
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,816
  3. bommba

    bommba

    Joined:
    Nov 24, 2013
    Posts:
    4
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,816
    I'm going to guess that some of the issue is due to the hard-coded numbers like 318 and 458 in there... the android device is certainly going to have a different number of pixels than your editor window.

    Put a text UI field onscreen, size it to full so it doesn't fade out, and put in it the values of Screen.width and Screen.height so you can tell at runtime what your screen dimensions are. That might explain the non-scrolling.
     
  5. bommba

    bommba

    Joined:
    Nov 24, 2013
    Posts:
    4
    Thanks but now I can scroll down and up without a stop. How can I set a end when all Screens have a different size?
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class scrollView : MonoBehaviour
    7. {
    8.  
    9.  
    10.     public Vector2 scrollPosition;
    11.     Touch touch;
    12.     // The string to display inside the scrollview. 2 buttons below add & clear this string.
    13.     string longString = "This is a long-ish string";
    14.     public GameObject button1;
    15.     public GameObject button2;
    16.     public GameObject button3;
    17.     public GameObject button4;
    18.     public GameObject button5;
    19.     public GameObject button6;
    20.     public GameObject button7;
    21.     public float button1Position;
    22.     public float button2Position;
    23.     private float button3Position;
    24.     private float button4Position;
    25.     private float button5Position;
    26.     private float button6Position;
    27.     private float button7Position;
    28.     private float screenWidth;
    29.     private float screenHeight;
    30.  
    31.     void start() {
    32.         button1.transform.position = new Vector2(0, 0);
    33.         screenWidth = Screen.width;
    34.         screenHeight = Screen.height;
    35.         //RectTransform rt = gameObject.GetComponent(typeof(RectTransform)) as RectTransform;
    36.         //rt.sizeDelta = new Vector2(screenWidth, screenHeight);
    37.     }
    38.  
    39.     void OnGUI()
    40.     {
    41.  
    42.         scrollPosition = GUI.BeginScrollView(new Rect(110, 50, 130, 160), scrollPosition, new Rect(110, 50, 130, 230), GUIStyle.none, GUIStyle.none);
    43.  
    44.         GUI.EndScrollView();
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.         if (Input.touchCount > 0)
    50.         {
    51.             touch = Input.touches[0];
    52.             if (touch.phase == TouchPhase.Moved)
    53.             {
    54.                 scrollPosition.y += touch.deltaPosition.y;
    55.                 button1Position = button1.transform.position.y;
    56.                 if (button1Position > screenHeight)
    57.                 {
    58.                     button1.transform.Translate(scrollPosition * Time.deltaTime * 20);
    59.                     button2.transform.Translate(scrollPosition * Time.deltaTime * 20);
    60.                     button3.transform.Translate(scrollPosition * Time.deltaTime * 20);
    61.                     button4.transform.Translate(scrollPosition * Time.deltaTime * 20);
    62.                     button5.transform.Translate(scrollPosition * Time.deltaTime * 20);
    63.                     button6.transform.Translate(scrollPosition * Time.deltaTime * 20);
    64.                     button7.transform.Translate(scrollPosition * Time.deltaTime * 20);
    65.                 }
    66.                 if (button1Position >= 458)
    67.                 {
    68.                     button1.transform.Translate(0, -4, 0);
    69.                     button2.transform.Translate(0, -4, 0);
    70.                     button3.transform.Translate(0, -4, 0);
    71.                     button4.transform.Translate(0, -4, 0);
    72.                     button5.transform.Translate(0, -4, 0);
    73.                     button6.transform.Translate(0, -4, 0);
    74.                     button7.transform.Translate(0, -4, 0);
    75.                 }
    76.  
    77.                 if (button1Position <= 318)
    78.                 {
    79.                     button1.transform.Translate(0, 4, 0);
    80.                     button2.transform.Translate(0, 4, 0);
    81.                     button3.transform.Translate(0, 4, 0);
    82.                     button4.transform.Translate(0, 4, 0);
    83.                     button5.transform.Translate(0, 4, 0);
    84.                     button6.transform.Translate(0, 4, 0);
    85.                     button7.transform.Translate(0, 4, 0);
    86.                 }                                                            
    87.             }
    88.         }
    89.     }
    90. }
    91.  
    Another problem is that when I scroll up, it´s very very slowly, but if I scroll down it´s very fast...
     
    Last edited: Dec 23, 2015
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,816
    You would probably extract values from the UI components involved in order to determine this. There is some documentation on it but I haven't any personal experience with that sort of thing. Might want to check in the UI forum.
     
  7. bommba

    bommba

    Joined:
    Nov 24, 2013
    Posts:
    4
    Okay thanks, I´ll try to find something