It seams as it’s not possible to write:
Rect[] rects = new Rect[10];
I’m looking for a alternative to do the above line.I don’t want to use list.
Thanks a ton.
It seams as it’s not possible to write:
Rect[] rects = new Rect[10];
I’m looking for a alternative to do the above line.I don’t want to use list.
Thanks a ton.
I think the official answer is what I said in the comment: C# does support arrays of structs. The following code compiles and runs, and the approach you mention in your comment should work, as well.
private Rect[] m_Rects = new Rect[3];
void Start()
{
m_Rects[0] = new Rect(10, 10, 120, 40);
m_Rects[1] = new Rect(10, 60, 120, 40);
m_Rects[2] = new Rect(10, 110, 120, 40);
}
void OnGUI()
{
for (int i = 0; i < m_Rects.Length; i++)
GUI.Label(m_Rects*, "Testing Rect #" + i);*
}
As a sidenote, there doesn’t seem a good reason for avoiding List in your usecase. Yes, arrays are generally faster even in C# (where they are implemented as a class, rather than representing a raw memory pointer to the first element), but unless you’re really stress-testing the array by adding millions of elements and frequently accessing them, both are going to give you the same in real-time performance. More information and metrics are here: [.net - Performance of Arrays vs. Lists - Stack Overflow][1]
[1]: .net - Performance of Arrays vs. Lists - Stack Overflow