-1

I have a button and a folder with many pictures. How do i make that when the button is clicked the pictures from the folder are shown and i can switch them like in a slide show?

public class OpenFaculty : MonoBehaviour
{
    public List<Button> buttons;
    public Dictionary<int, string> Path;

void Start()
    {
        Path = new();
        Path.Add(0, "file://C:\Unity\Myproject2\Assets\1ФМФ\");
        Path.Add(1, "file://C:\Unity\Myproject2\Assets\2ФИЯ");
        Path.Add(2, "file://C:\Unity\Myproject2\Assets\3ФРФиД"); 
        Path.Add(3, "file://C:\Unity\Myproject2\Assets\4ФИиП");
        Path.Add(4, "file://C:\Unity\Myproject2\Assets\5ФП");
        Path.Add(5, "file://C:\Unity\Myproject2\Assets\6ФТиБ");
        Path.Add(6, "file://C:\Unity\Myproject2\Assets\7ФИСиГН");
        Path.Add(7, "file://C:\Unity\Myproject2\Assets\8ФЕН");
        Path.Add(8, "file://C:\Unity\Myproject2\Assets\9ФФК");
        Path.Add(9, "file://C:\Unity\Myproject2\Assets\11ФИСиГН");
        Path.Add(10, "file://C:\Unity\Myproject2\Assets\12МФ");
        Path.Add(11, "file://C:\Unity\Myproject2\Assets\13ЦДиПО");
        Path.Add(12, "file://C:\Unity\Myproject2\Assets\14ПОДИГ");
        Path.Add(13, "file://C:\Unity\Myproject2\Assets\15ИПИТ");
        Path.Add(14, "file://C:\Unity\Myproject2\Assets\16ИИОП");
        Path.Add(15, "file://C:\Unity\Myproject2\Assets\17ИТиКИ");

        for (int i = 0; i < 16; i++) 
        {
            buttons[i].onClick.AddListener(()=>ShowPicture(i));
        }
    }
   //public void ShowPicture(int Number)
    public void ShowPicture(int Faculty)
    {
        Debug.Log(Path[Faculty]);
    }
}
mikhail
  • 11
  • 1
    Is it supposed to only work in the Unity Editor itself? ^^ Otherwise you will need to use e.g. [`StreamingAssets`](https://docs.unity3d.com/Manual/StreamingAssets.html) because after building the app you don't have direct FileIO access to assets ... why not rather simply reference your images directly in a `public Texture2D[] images;` via the Inspector? ... and finally be aware of [**closure and variable capture in your lambda expression**](https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp) ... currently all your buttons will always load the last element! – derHugo Mar 22 '23 at 08:48
  • Besides that the question as it stands is far too broad and unspecific ... where is your code for the slide show? .. how exactly are those to be displayed? ... etc – derHugo Mar 22 '23 at 08:50

1 Answers1

0

You shouldn't access IO directly in your project, that is prohibited outside of editor as @derHugo mentioned. Instead, you would implement something like this:

public class SpriteStorage : ScriptableObject
{
    public Sprite[] Sprites => _sprites;
    [SerializeField]
    private Sprite[] _sprites;
}

public class Slideshow : MonoBehaviour
{
    [SerializeField]
    private SpriteStorage _storage;
    
    public void NextSlide() => ... //access storage, get next or do nothing
    public void PreviousSlide() => ... //access storage, get previous or do nothing
}
E_net4
  • 27,810
  • 13
  • 101
  • 139
Xander
  • 378
  • 1
  • 6