Button Displacement
Hey Everyone!
I'm having problems with a file manager I'm trying to create, when I start loading the buttons, every 6th button goes to the wrong place and then every 6th button starts stacking on top of one another. Here's the code I use:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class LoadButtons : MonoBehaviour {
public GameObject FileButton;
public GameObject ViewPortContent;
public int FileNo;
public GameObject DirButton;
public float PosX;
public float PosY;
public Transform ButtonSpawn;
public string path;
private void Start()
{
ButtonSpawn.position = new Vector3(-258 + 360, 0 + 300, 0);
//Sets the directory
DirectoryInfo dir = new DirectoryInfo(path);
//Puts list of files into an Array
FileInfo[] info = dir.GetFiles();
//Puts a list of Directories into an array
DirectoryInfo[] DirInfo = dir.GetDirectories();
FileNo = 1;
foreach (DirectoryInfo directory in DirInfo)
{
if (ButtonSpawn.position.x > 258 + 360)
{
RectTransform RectTrans = ViewPortContent.GetComponent<RectTransform>();
RectTrans.sizeDelta = new Vector2(0, RectTrans.sizeDelta.y + 50);
ButtonSpawn.position = new Vector3(-258 + 360, ButtonSpawn.position.y - 50, 0);
}
else
{
ButtonSpawn.position = new Vector3(ButtonSpawn.position.x + 105, ButtonSpawn.position.y, 0);
}
var Button = Instantiate(DirButton, ButtonSpawn.position, new Quaternion(0, 0, 0, 0));
Button.transform.SetParent(ViewPortContent.transform, false);
Button.transform.position = ButtonSpawn.position;
FileNo += 1;
}
FileNo = 1;
foreach (FileInfo file in info)
{
if (ButtonSpawn.position.x > 258 + 360)
{
RectTransform RectTrans = ViewPortContent.GetComponent<RectTransform>();
RectTrans.sizeDelta = new Vector2(0, RectTrans.sizeDelta.y + 50);
ButtonSpawn.position = new Vector3(-258 + 360, ButtonSpawn.position.y - 50, 0);
}
else
{
ButtonSpawn.position = new Vector3(ButtonSpawn.position.x + 105, ButtonSpawn.position.y, 0);
}
var Button = Instantiate(FileButton, ButtonSpawn.position, new Quaternion(0, 0, 0, 0));
Button.transform.SetParent(ViewPortContent.transform, false);
Button.transform.position = ButtonSpawn.position;
FileNo += 1;
}
}
}
And Here's a screenshot of what's happening:
capture.jpg
(65.2 kB)
Comment