MIF_E31230838/Assets/Scripts/MenuSlider.cs

151 lines
4.2 KiB
C#

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems; // Wajib ada
using System.Collections;
public class MenuSlider : MonoBehaviour
{
[Header("Hubungkan UI di Sini")]
public ScrollRect scrollRect; // Tarik Object Card_Window
public RectTransform contentHolder; // Tarik Object Content_Holder
[Header("Setting")]
public float snapSpeed = 10f; // Kecepatan geser kartu
// Variabel internal (Jangan diubah manual)
private float[] cardPositions;
private int currentIndex = 0;
private float targetX = 0;
private bool isInitialized = false;
void Start()
{
// Delay sebentar biar Layout Group selesai menghitung ukuran
Invoke("InitializePositions", 0.1f);
}
// Hitung posisi koordinat setiap kartu
void InitializePositions()
{
int childCount = contentHolder.childCount;
cardPositions = new float[childCount];
// Ambil lebar kartu pertama + spasi
float cardWidth = contentHolder.GetChild(0).GetComponent<RectTransform>().rect.width;
float spacing = contentHolder.GetComponent<HorizontalLayoutGroup>().spacing;
float step = cardWidth + spacing;
for (int i = 0; i < childCount; i++)
{
// Posisi X selalu minus karena geser ke kiri
cardPositions[i] = -(i * step);
}
isInitialized = true;
}
void Update()
{
if (!isInitialized) return;
// Logika Geser Halus (Lerp)
Vector2 currentPos = contentHolder.anchoredPosition;
float newX = Mathf.Lerp(currentPos.x, targetX, Time.deltaTime * snapSpeed);
contentHolder.anchoredPosition = new Vector2(newX, currentPos.y);
}
// --- FUNGSI TOMBOL NEXT ---
public void NextCard()
{
// 1. Bunyi SFX
if (Audio.instance != null) Audio.instance.PlayButtonSound();
// 2. Animasi Tombol
GameObject tombol = EventSystem.current.currentSelectedGameObject;
if (tombol != null)
{
StartCoroutine(AnimasiDanPindah(tombol.transform, true));
}
else
{
MoveLogic(true);
}
}
// --- FUNGSI TOMBOL PREV ---
public void PrevCard()
{
// 1. Bunyi SFX
if (Audio.instance != null) Audio.instance.PlayButtonSound();
// 2. Animasi Tombol
GameObject tombol = EventSystem.current.currentSelectedGameObject;
if (tombol != null)
{
StartCoroutine(AnimasiDanPindah(tombol.transform, false));
}
else
{
MoveLogic(false);
}
}
// Animasi Tombol Next/Prev (Squash -> Pindah Logic -> Stretch)
IEnumerator AnimasiDanPindah(Transform tombol, bool isNext)
{
float timer = 0;
float durasi = 0.1f;
// FASE 1: SQUASH (Ditekan)
while (timer < durasi)
{
timer += Time.deltaTime;
float t = timer / durasi;
float scaleX = Mathf.Lerp(1f, 1.2f, t);
float scaleY = Mathf.Lerp(1f, 0.8f, t);
tombol.localScale = new Vector3(scaleX, scaleY, 1f);
yield return null;
}
// -- PINDAHKAN KARTU SEKARANG (Biar responsif) --
MoveLogic(isNext);
timer = 0;
// FASE 2: STRETCH (Membal Balik)
while (timer < durasi)
{
timer += Time.deltaTime;
float t = timer / durasi;
float scaleX = Mathf.Lerp(1.2f, 1f, t); // Balik ke 1
float scaleY = Mathf.Lerp(0.8f, 1f, t); // Balik ke 1
tombol.localScale = new Vector3(scaleX, scaleY, 1f);
yield return null;
}
// Pastikan normal
tombol.localScale = Vector3.one;
}
// Logika Matematika Geser Index
void MoveLogic(bool isNext)
{
if (isNext)
{
if (currentIndex < cardPositions.Length - 1)
{
currentIndex++;
targetX = cardPositions[currentIndex];
}
}
else
{
if (currentIndex > 0)
{
currentIndex--;
targetX = cardPositions[currentIndex];
}
}
}
}