- Home /
Grid layout lags after thousand inserts.
Hello everyone,
I'm trying to make a table using a grid layout. There are many tutorials on youtube showcasing how to do it and I basically insert a prefab white image with altered text whenever I need to populate a table. I also added a vertical scroll to be able to scroll each tab. Furthermore I added a toggle and a button script on each tab using RectTransform.SetAsLastSibling() to bring the respective tab in front.
The problem is that after a few hundred insertions on a tab the performance drops dramatically. At about 1000 insertions the table because sluggishly slow and is not good for use.
This is the code for the insertions (It's based on a postgres npgsql dll plugin)
using System;
using System.Collections.Generic;
using UnityEngine;
using Npgsql;
using UnityEngine.UI;
public class DBConnection : MonoBehaviour
{
private NpgsqlConnection conn;
public bool Connected=false;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//should check here if the connection is active or down
}
public void Connect_Database()
{
// connection config
string server = "****";
string port = ""****";
string database = ""****";
string user = ""****";
string password = ""****";
// connection string format
string connstring = String.Format($"Server={server};" +
$"Port={port};" +
$"Database={database};" +
$"User Id={user};" +
$"Password={password};");
// attempt to connect to database
Debug.Log("Connect String = " + connstring);
try
{
// Making connection with Npgsql provider
conn = new NpgsqlConnection(connstring);
Debug.Log("Connected" + conn);
conn.Open();
Debug.Log("connection established");
Connected = true;
}
catch (Exception e)
{
Debug.LogError($"Could not establish a connection, reason:{e}");
Connected = false;
throw;
}
}
public List<string> Select_Pelatologio()
{
string sql = "SELECT * FROM Πελατολόγιο";
return Select_Query(sql);
}
public List<string> Select_Peristatika()
{
string sql = "SELECT * FROM Περιστατικά limit 800";
return Select_Query(sql);
}
public List<string> Select_Query(string query)
{
List<string> result_query = new List<string>();
NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
NpgsqlDataReader dr = cmd.ExecuteReader();
Debug.LogFormat($"Attempting to run query:{query}");
// insert all items into the grid
while (dr.Read())
{
string tmp = "";
for (int i = 0; i < dr.FieldCount; i++)
{
//tmp = ;
tmp += ($"{dr[i].ToString()}\t");
result_query.Add(dr[i].ToString());
}
Debug.Log($"{tmp}\n");
}
Debug.Log("Query finished");
dr.Close();
return result_query;
}
}
The grid code is this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Grid_Pelatologio : MonoBehaviour
{
public GameObject Prefab;
//public int NumberToCreate;
public GridLayoutGroup GridLayoutGroup;
public GameObject Content;
// Start is called before the first frame update
void Start()
{
Clear_All();
}
void Update()
{
}
public void Clear_All()
{
foreach (Transform child in this.transform)
{
Destroy(child.gameObject);
}
}
public void Add_Random_Color(GameObject obj)
{
obj.GetComponent<Image>().color = Random.ColorHSV();
}
public void Insert_Items(List<string> items)
{
for (int i = 0; i < items.Count; i++)
{
GameObject obj = Instantiate(Prefab, Content.transform);
obj.GetComponentInChildren<Text>().text = items[i];
}
}
public void Insert_Item(string item)
{
GameObject obj = Instantiate(Prefab, Content.transform);
obj.GetComponentInChildren<Text>().text = item.ToString();
}
IEnumerator ChangeGridSize(int x, int y)
{
yield return new WaitForSeconds(2);
// Now do your thing here
}
}
What am I doing wrong? Thanks for your time and I'll gladly answer any specifications you ask.
Your answer
Follow this Question
Related Questions
How to assign textures to specific grid locations? 2 Answers
Gui Layout Interface 0 Answers
Grid Layout spacing not working when in Android Build 0 Answers
Snap guides for laying out objects 2 Answers