// File: ScrollViewImageLoader.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
public class ScrollViewImageLoader : MonoBehaviour
{
public GameObject imagePrefab;
public Transform content;
public Sprite[] allSprites;
[Header("Audio")]
public AudioClip[] soundClips; // Must match order of sprites
public AudioSource audioSource;
[Header("Fullscreen UI")]
public GameObject fullscreenPanel;
public Image fullscreenImage;
// Call this with a list of unlocked card indices
public void LoadUnlockedImages(List<int> unlockedIndices)
{
foreach (Transform child in content)
Destroy(child.gameObject);
foreach (int index in unlockedIndices)
{
if (index >= 0 && index < allSprites.Length)
{
SpawnImage(index);
}
}
}
void SpawnImage(int index)
{
GameObject go = Instantiate(imagePrefab, content);
Image img = go.GetComponent<Image>();
if (img != null)
{
img.sprite = allSprites[index];
img.preserveAspect = true;
}
Button btn = go.GetComponent<Button>();
if (btn != null)
{
btn.onClick.AddListener(() => OpenFullscreen(index));
}
}
public void OpenFullscreen(int index)
{
if (fullscreenPanel != null && fullscreenImage != null)
{
fullscreenImage.sprite = allSprites[index];
fullscreenPanel.SetActive(true);
// Play the matching sound
if (audioSource != null && index < soundClips.Length && soundClips[index] != null)
{
audioSource.Stop();
audioSource.clip = soundClips[index];
audioSource.Play();
}
}
}
public void CloseFullscreen()
{
if (fullscreenPanel != null)
{
fullscreenPanel.SetActive(false);
}
}
}