From Obese Parrot, 5 Days ago, written in C.
Embed
  1. // File: ScrollViewImageLoader.cs
  2.  
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Collections.Generic;
  6.  
  7. public class ScrollViewImageLoader : MonoBehaviour
  8. {
  9.     public GameObject imagePrefab;
  10.     public Transform content;
  11.     public Sprite[] allSprites;
  12.  
  13.     [Header("Audio")]
  14.     public AudioClip[] soundClips; // Must match order of sprites
  15.     public AudioSource audioSource;
  16.  
  17.     [Header("Fullscreen UI")]
  18.     public GameObject fullscreenPanel;
  19.     public Image fullscreenImage;
  20.  
  21.     // Call this with a list of unlocked card indices
  22.     public void LoadUnlockedImages(List<int> unlockedIndices)
  23.     {
  24.         foreach (Transform child in content)
  25.             Destroy(child.gameObject);
  26.  
  27.         foreach (int index in unlockedIndices)
  28.         {
  29.             if (index >= 0 && index < allSprites.Length)
  30.             {
  31.                 SpawnImage(index);
  32.             }
  33.         }
  34.     }
  35.  
  36.     void SpawnImage(int index)
  37.     {
  38.         GameObject go = Instantiate(imagePrefab, content);
  39.         Image img = go.GetComponent<Image>();
  40.         if (img != null)
  41.         {
  42.             img.sprite = allSprites[index];
  43.             img.preserveAspect = true;
  44.         }
  45.  
  46.         Button btn = go.GetComponent<Button>();
  47.         if (btn != null)
  48.         {
  49.             btn.onClick.AddListener(() => OpenFullscreen(index));
  50.         }
  51.     }
  52.  
  53.     public void OpenFullscreen(int index)
  54.     {
  55.         if (fullscreenPanel != null && fullscreenImage != null)
  56.         {
  57.             fullscreenImage.sprite = allSprites[index];
  58.             fullscreenPanel.SetActive(true);
  59.  
  60.             // Play the matching sound
  61.             if (audioSource != null && index < soundClips.Length && soundClips[index] != null)
  62.             {
  63.                 audioSource.Stop();
  64.                 audioSource.clip = soundClips[index];
  65.                 audioSource.Play();
  66.             }
  67.         }
  68.     }
  69.  
  70.     public void CloseFullscreen()
  71.     {
  72.         if (fullscreenPanel != null)
  73.         {
  74.             fullscreenPanel.SetActive(false);
  75.         }
  76.     }
  77. }

captcha