Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Swipe to cycle array of images.

Discussion in 'Scripting' started by sr3ds, Jun 30, 2015.

  1. sr3ds

    sr3ds

    Joined:
    Apr 29, 2015
    Posts:
    5
    Hi,
    i'm implementing a swipe movement on a UI Image that cycles a full set of images, giving the sensation on having a 3d model instead of a simple 2d sprite.

    But i'm having some problems on associating the swipe to the images, as when the index i use gets to negative in swiping to negative numbers, the index doesn't follow pictures name anymore.


    I don't know if this is the right approach, but it is almost completely working.
    That's the "criminal" part of the script:
    Code (CSharp):
    1.  
    2. private string StartingPicName;
    3.     private float swipeSpeed = 0.05f;
    4.  
    5.     private Image image;
    6.     private float startX = -1;
    7.     private int startPic = -1;
    8.     private int currentPic = 0;
    9.  
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         image = GameObject.FindGameObjectWithTag("Image").GetComponent<Image>();
    15.         //Debug.Log("Nome: " + image.name);
    16.         StartingPicName = GetComponentInChildren <Image>().sprite.name;
    17.    
    18.    
    19.         Debug.Log("il numero di partenza dell'immagine e' " + StartingPicName);
    20.     }
    21.  
    22.     void Update()
    23.     {
    24.         Debug.Log("Ora StartPic vale " + startPic);
    25.  
    26.             if (Input.GetMouseButtonDown (0)) {
    27.  
    28.        
    29.  
    30.             //index calculation on swipe
    31.             startX = Input.mousePosition.x;
    32.  
    33.          
    34.             startPic = currentPic;
    35.          
    36.        
    37.        
    38.  
    39.             //my index picture calculation on list
    40.        
    41.  
    42.         }
    43.          
    44.             if (startX != -1)
    45.             {
    46.            
    47.                     currentPic = (int)Mathf.Round(( startX - Input.mousePosition.x) * swipeSpeed + startPic)%24;
    48.                
    49.                     Debug.Log("CurrentPic is: " + currentPic);
    50.                     ImageSwitch();
    51.  
    Thanks for helping out.

    P.S the final ImageSwitch() just launches the actual function that loads the sprite.
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    add 24 before doing the %24
    that way it's always positive
     
  3. sr3ds

    sr3ds

    Joined:
    Apr 29, 2015
    Posts:
    5
    Yeah, i read your solution only now, but that's what i did yesterday getting it all of a sudden. Thanks anyway :)