MIF_E31211846/Assets/Resources/Materi/Swipe_Control.cs

94 lines
2.3 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Swipe_Control : MonoBehaviour
{
public GameObject scrollbar;
private float scroll_pos = 0;
private float[] pos;
private int posisi = 0;
private Scrollbar scrollbarComponent;
void Start()
{
// Pastikan scrollbar tidak null sebelum mengakses komponennya
if (scrollbar != null)
{
scrollbarComponent = scrollbar.GetComponent<Scrollbar>();
}
else
{
Debug.LogError("Scrollbar is not assigned!");
return;
}
// Pastikan scrollbarComponent tidak null setelah mencoba mendapatkannya
if (scrollbarComponent == null)
{
Debug.LogError("Scrollbar component is not found on the assigned GameObject!");
return;
}
int childCount = transform.childCount;
pos = new float[childCount];
float distance = 1f / (childCount - 1f);
for (int i = 0; i < childCount; i++)
{
pos[i] = distance * i;
}
}
public void next()
{
if (posisi < pos.Length - 1)
{
posisi += 1;
scroll_pos = pos[posisi];
}
}
public void prev()
{
if (posisi > 0)
{
posisi -= 1;
scroll_pos = pos[posisi];
}
}
void Update()
{
// Pastikan scrollbarComponent tidak null sebelum menggunakannya
if (scrollbarComponent == null)
{
Debug.LogError("Scrollbar tidak terinisiasi");
return;
}
if (Input.GetMouseButton(0))
{
scroll_pos = scrollbarComponent.value;
}
else
{
for (int i = 0; i < pos.Length; i++)
{
float distance = 1f / (pos.Length - 1f);
if (scroll_pos < pos[i] + (distance / 2) && scroll_pos > pos[i] - (distance / 2))
{
scrollbarComponent.value = Mathf.Lerp(scrollbarComponent.value, pos[i], 0.15f);
posisi = i;
}
}
}
}
public void ButtonBack(string scenename)
{
SceneManager.LoadScene(scenename);
}
}