Search Unity

How to make cover flow slider

Discussion in '2D' started by dman8723, Mar 14, 2018.

  1. dman8723

    dman8723

    Joined:
    Dec 18, 2017
    Posts:
    12
    Hello, I am a beginner for programmer, and I have a question
    I am trying to use Scroll Rect to develop a loop menu, and I want to make it as the picture to the menu, how should I do??

    I have tried to use transform.localScale to change the Button size, but there is a "depth" problem which I don't know how to slove it?
     
    Last edited: Mar 14, 2018
  2. dman8723

    dman8723

    Joined:
    Dec 18, 2017
    Posts:
    12
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using DG;
    6. public class ScrollRectSnap4 : MonoBehaviour {
    7.     public RectTransform panel;
    8.     public Button[] bttn;
    9.     public RectTransform center;
    10.     public float[] distance;
    11.     public float[] distReposition;
    12.     private bool dragging = false;
    13.     public int bttnDistance;
    14.     private int minButtonNum;
    15.     private int bttnLenght;
    16.     public float minDistance;
    17.     void Start () {
    18.         bttnLenght = bttn.Length;
    19.         distance = new float[bttnLenght];
    20.         distReposition = new float[bttnLenght];
    21.         bttnDistance = (int)Mathf.Abs(bttn[1].GetComponent<RectTransform>().anchoredPosition.x - bttn[0].GetComponent<RectTransform>().anchoredPosition.x);
    22.     }
    23.     void Update () {
    24.         for (int i = 0; i < bttn.Length; i++) {
    25.             distReposition[i] = center.transform.position.x - bttn[i].transform.position.x;
    26.             distance [i] = Mathf.Abs (distReposition[i]);
    27.             if (distReposition [i] > 1200) {
    28.                 float curX = bttn [i].GetComponent<RectTransform> ().anchoredPosition.x;
    29.                 float curY = bttn [i].GetComponent<RectTransform> ().anchoredPosition.y;
    30.                 Vector2 newAnchoredPos1 = new Vector2 (curX + (bttnLenght * bttnDistance), curY);
    31.                 //Debug.Log (bttn[i].name + "newAnchoredPos1:   " + newAnchoredPos1);
    32.                 bttn [i].GetComponent<RectTransform> ().anchoredPosition = newAnchoredPos1;
    33.             }
    34.             if (distReposition [i] < -1200) {
    35.                 float curX = bttn [i].GetComponent<RectTransform> ().anchoredPosition.x;
    36.                 float curY = bttn [i].GetComponent<RectTransform> ().anchoredPosition.y;
    37.                 Vector2 newAnchoredPos2 = new Vector2 (curX - (bttnLenght * bttnDistance), curY);
    38.                 bttn [i].GetComponent<RectTransform> ().anchoredPosition = newAnchoredPos2;
    39.             }
    40.         }
    41.         minDistance = Mathf.Min (distance);
    42.         for (int a = 0; a < bttn.Length; a++) {
    43.             if (minDistance == distance [a]) {
    44.                 minButtonNum = a;
    45.                 bttn[a].transform.localScale = Vector3.Lerp(bttn[a].transform.localScale,new Vector3(1f,1f,1f),Time.deltaTime*5);
    46.             } else {
    47.                 bttn[a].transform.localScale = Vector3.Lerp(bttn[a].transform.localScale,new Vector3(0.7f,0.7f,0.7f),Time.deltaTime*5);
    48.             }
    49.         }
    50.         if (!dragging) {
    51.             //LerpToBttn (minButtonNum * -bttnDistance);
    52.             LerpToBttn (-bttn[minButtonNum].GetComponent<RectTransform>().anchoredPosition.x);
    53.         }
    54.     }
    55.     void LerpToBttn (float position){
    56.         //Debug.Log ("Position: " + -bttn[minButtonNum].GetComponent<RectTransform>().anchoredPosition.x);
    57.         float newX = Mathf.Lerp (panel.anchoredPosition.x, position, Time.deltaTime * 5f);
    58.         Vector2 newPosition = new Vector2 (newX, panel.anchoredPosition.y);
    59.         panel.anchoredPosition = newPosition;
    60.     }
    61.     public void StartDrag(){
    62.         dragging = true;
    63.     }
    64.     public void EndDrag(){
    65.         dragging = false;
    66.     }
    67. }
    This is the process of my menu, I have tried to make the center Image bigger than others, but I don't know how to change the farest object look smaller
     
  3. Kuptsevych-Yuriy

    Kuptsevych-Yuriy

    Joined:
    Oct 1, 2008
    Posts:
    20