using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class swipe : MonoBehaviour { public Color[] colors; public GameObject scrollbar, imageContent; private float scroll_pos = 0; private float[] pos; private bool runIt = false; private float time; private Button takeTheBtn; private int btnNumber; private bool isDragging = false; private Vector2 startDragPosition; private Vector2 endDragPosition; // Start is called before the first frame update void Start() { pos = new float[transform.childCount]; float distance = 1f / (pos.Length - 1f); for (int i = 0; i < pos.Length; i++) { pos[i] = distance * i; } } // Update is called once per frame void Update() { if (runIt) { float distance = 1f / (pos.Length - 1f); GecisiDuzenle(distance, pos, takeTheBtn); time += Time.deltaTime; if (time > 1f) { time = 0; runIt = false; } } if (Input.GetMouseButtonDown(0)) { isDragging = true; startDragPosition = Input.mousePosition; } if (Input.GetMouseButtonUp(0)) { isDragging = false; endDragPosition = Input.mousePosition; HandleSwipe(); } if (isDragging) { scroll_pos = scrollbar.GetComponent().value; } else { for (int i = 0; i < pos.Length; i++) { if (scroll_pos < pos[i] + (1f / (pos.Length - 1f) / 2) && scroll_pos > pos[i] - (1f / (pos.Length - 1f) / 2)) { scrollbar.GetComponent().value = Mathf.Lerp(scrollbar.GetComponent().value, pos[i], 0.1f); } } } for (int i = 0; i < pos.Length; i++) { if (scroll_pos < pos[i] + (1f / (pos.Length - 1f) / 2) && scroll_pos > pos[i] - (1f / (pos.Length - 1f) / 2)) { Debug.LogWarning("Current Selected Level" + i); transform.GetChild(i).localScale = Vector2.Lerp(transform.GetChild(i).localScale, new Vector2(1f, 1f), 0.1f); imageContent.transform.GetChild(i).localScale = Vector2.Lerp(imageContent.transform.GetChild(i).localScale, new Vector2(1.2f, 1.2f), 0.1f); imageContent.transform.GetChild(i).GetComponent().color = colors.Length > 1 ? colors[1] : Color.white; for (int j = 0; j < pos.Length; j++) { if (j != i) { imageContent.transform.GetChild(j).GetComponent().color = colors.Length > 0 ? colors[0] : Color.white; imageContent.transform.GetChild(j).localScale = Vector2.Lerp(imageContent.transform.GetChild(j).localScale, new Vector2(0.8f, 0.8f), 0.1f); transform.GetChild(j).localScale = Vector2.Lerp(transform.GetChild(j).localScale, new Vector2(0.8f, 0.8f), 0.1f); } } } } } private void HandleSwipe() { float distance = endDragPosition.x - startDragPosition.x; if (Mathf.Abs(distance) > 50f) // Minimum distance for swipe to be registered { if (distance > 0) { // Swipe Right btnNumber = Mathf.Max(btnNumber - 1, 0); } else { // Swipe Left btnNumber = Mathf.Min(btnNumber + 1, pos.Length - 1); } scroll_pos = pos[btnNumber]; scrollbar.GetComponent().value = scroll_pos; } } private void GecisiDuzenle(float distance, float[] pos, Button btn) { for (int i = 0; i < pos.Length; i++) { if (scroll_pos < pos[i] + (distance / 2) && scroll_pos > pos[i] - (distance / 2)) { scrollbar.GetComponent().value = Mathf.Lerp(scrollbar.GetComponent().value, pos[btnNumber], 1f * Time.deltaTime); } } for (int i = 0; i < btn.transform.parent.transform.childCount; i++) { btn.transform.name = "."; } } public void WhichBtnClicked(Button btn) { btn.transform.name = "clicked"; for (int i = 0; i < btn.transform.parent.transform.childCount; i++) { if (btn.transform.parent.transform.GetChild(i).transform.name == "clicked") { btnNumber = i; takeTheBtn = btn; time = 0; scroll_pos = pos[btnNumber]; runIt = true; } } } }