71 lines
1.7 KiB
C#
71 lines
1.7 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class MenuMateriSlide : MonoBehaviour
|
|
{
|
|
[Header("Scroll")]
|
|
public ScrollRect scrollRect;
|
|
public RectTransform content;
|
|
public int totalCard = 6;
|
|
public float duration = 0.3f;
|
|
|
|
private int currentIndex = 0;
|
|
private bool isSliding = false;
|
|
|
|
void Start()
|
|
{
|
|
GoToCard(0, false);
|
|
}
|
|
|
|
public void Next()
|
|
{
|
|
if (isSliding) return;
|
|
if (currentIndex < totalCard - 1)
|
|
{
|
|
currentIndex++;
|
|
GoToCard(currentIndex, true);
|
|
}
|
|
}
|
|
|
|
public void Prev()
|
|
{
|
|
if (isSliding) return;
|
|
if (currentIndex > 0)
|
|
{
|
|
currentIndex--;
|
|
GoToCard(currentIndex, true);
|
|
}
|
|
}
|
|
|
|
void GoToCard(int index, bool animate)
|
|
{
|
|
// Beritahu NarasiAudio untuk ganti clip
|
|
NarasiAudio narasi = GetComponent<NarasiAudio>();
|
|
if (narasi != null) narasi.OnCardChanged(index);
|
|
|
|
float target = (float)index / Mathf.Max(1, totalCard - 1);
|
|
if (animate)
|
|
StartCoroutine(SlideTo(target));
|
|
else
|
|
scrollRect.horizontalNormalizedPosition = target;
|
|
}
|
|
|
|
IEnumerator SlideTo(float target)
|
|
{
|
|
isSliding = true;
|
|
float start = scrollRect.horizontalNormalizedPosition;
|
|
float elapsed = 0f;
|
|
|
|
while (elapsed < duration)
|
|
{
|
|
elapsed += Time.deltaTime;
|
|
scrollRect.horizontalNormalizedPosition = Mathf.Lerp(start, target, elapsed / duration);
|
|
yield return null;
|
|
}
|
|
|
|
scrollRect.horizontalNormalizedPosition = target;
|
|
isSliding = false;
|
|
}
|
|
}
|