using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class SavedGamesSlots : MonoBehaviour
{
public GameObject saveSlotPrefab;
public float gap;
private Transform slots;
private string[] imagesToLoad;
// Start is called before the first frame update
void Start()
{
imagesToLoad = Directory.GetFiles(Application.dataPath + "/screenshots", "*.png");
slots = GameObject.FindGameObjectWithTag("Slots Content").transform;
for (int i = 0; i < imagesToLoad.Length; i++)
{
var go = Instantiate(saveSlotPrefab);
go.transform.SetParent(slots);
Texture2D thisTexture = new Texture2D(100, 100); //NOW INSIDE THE FOR LOOP
string fileName = imagesToLoad[i];
byte[] bytes = File.ReadAllBytes(fileName);
thisTexture.LoadImage(bytes);
thisTexture.name = fileName;
GameObject ChildGameObject = go.transform.GetChild(1).gameObject;
ChildGameObject.GetComponent<RawImage>().texture = thisTexture;
var raw = go.GetComponent<RectTransform>();
raw.anchoredPosition = new Vector3(1 + 1 * gap, 6, 0);
}
}
// Update is called once per frame
void Update()
{
}
}
The way I'm doing it now is not working it's moving all the objects to the same position and not adding equal gap's between them.
In the loop I tried to do it this way in this line :
raw.anchoredPosition = new Vector3(1 + 1 * gap, 6, 0);