1
\$\begingroup\$

Hello folks I'm making a little game for fun in unity, Its a boxing management sim. I've got the code done to create boxers and give them many properties such as names, weights, stats, etc. I've made another screen , lets call it 'office screen' for now. In that I want to open a panel which will show all the boxers in the game (ideally I'd like to be able to make the player be able to filter these results by which company he's signed to etc - but I think that might be out of my skill range so maybe i will just make a separate screen for Owned Boxers)

But I cannot seem to find a way to display all these infos in a table with columns such as Name ; Age ; Company ; etc;

I have had a go just using the UI Text components and panels. But thing is, there will be approx 100 boxers or more and this number will always change also.

Any ideas for this in Unity specifically?

EDIT: I am storing all the data just as variables (i think fields) in a class called Boxer which is assigned as a component of a new GameObject i instantiate at the time each boxer is created. So each boxer has its own gameobject.

EDIT 2 :D i've found this: https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/models-data/displaying-a-table-of-database-data-cs
would this be the sort of thing I can add to Unity (assuming I can follow along with the guide)?

enter image description here

Thanks for any help

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Regarding edit2: No, the article is for ASP.NET which is entirely different (kind of) technology, Unity does not have built-in web browser for rendering, yet alone web server for hosting such as IIS or Apache (Mono implementation). \$\endgroup\$
    – wondra
    May 3 '18 at 15:45
  • \$\begingroup\$ ah, fair enough. I did think that looked nothing like what im used to. I've actually found this now and feel i might just manage to get something working forum.unity.com/threads/highscore-table-with-guilayout.7918 \$\endgroup\$ May 3 '18 at 15:52
  • \$\begingroup\$ but if you see the image i added to my question. The information for the Unity GUILayout class says "Make an auto-layout label" , what does that mean if you have to the arrange its position and size manually? \$\endgroup\$ May 3 '18 at 15:57
1
\$\begingroup\$

Somehow I have managed to cobble something together that works. I've purely done the layout here and not pulled any data into it yet, but in case any one else has the same confusion as me, I will post the method here I used. It is actually very short method and seems to work for me after I made the Row a prefab, and make it a parent of the panel which i want the table to stretch within.

   public void BuildTable()
{
    // TODO: Deal with more than 10 boxers by making it sort them into pages somehow.

    // discover which boxers user owns:
    List<Boxer> ownedBoxers = new List<Boxer>();
    foreach (Boxer b in Game.instance.boxers)
    {
        if (b.companySignedTo == Game.instance.playerCompanyName)
            ownedBoxers.Add(b);
    }

    for (float i = 0; i < ownedBoxers.Count; i += rowHeight)
    {
        tableRowPrefab = Resources.Load("BoxerTableRowPrefab") as GameObject;
        tableRow = Instantiate(tableRowPrefab);
        tableRow.GetComponent<RectTransform>().SetParent(panelRectTransform);
        tableRow.GetComponent<RectTransform>().offsetMin = Vector2.zero;
        tableRow.GetComponent<RectTransform>().offsetMax = Vector2.zero;
                    tableRow.GetComponent<RectTransform>().anchorMin = new Vector2(0f, 1 - (i * 0.9f) - rowHeight);
        tableRow.GetComponent<RectTransform>().anchorMax = new Vector2(1f, 1 - (i * 0.9f));
    }
}
\$\endgroup\$
6
  • 1
    \$\begingroup\$ How will you handle column alignment, when there will be cells of different length? \$\endgroup\$
    – wondra
    May 3 '18 at 18:31
  • 1
    \$\begingroup\$ The cells are fixed in width in the prefab, and then I used the anchormin/max of RectTransform to adjust the height to be panelheight / rowHeight). I've made a fair bit of progress with it now and I must say it is working ok. (I do wish I could not have made a prefab and instead coded the whole thing from scratch, but it made it easier having that visual starting block of the initial row being made myself in the Editor GUI. Next time I use this I will create each part of the prefab in code instead (ie. A panel, containing n panels, each containing text components) \$\endgroup\$ May 4 '18 at 15:15
  • 1
    \$\begingroup\$ The confusing part for me was having to set OffsetMin/max to Zero. Anything but zero is wrong because that seems to actually mean position (the way I use Unity's UI is the 'stretch method' so my Anchors are never/rarely 0.5 and then the positional fields (above the anchors in Inspector are always Zero). I'm probably doing it very wrong tbh but its working for me, but is fiddly to say the least \$\endgroup\$ May 4 '18 at 15:17
  • 2
    \$\begingroup\$ Good to hear you figured it out. Back when I implemented something similar I used 4 prefabs: header+row Panel prefabs (background, lines between rows) and header+row Text cell prefabs (font, foreground, etc.) and script hierarchically instantiating those with two pass measure-arrange algorithm. However my cell sizes were dynamic and determined by their content. Worked ok-ish but still far from what you can get out of dedicated GUI framework, still not sure if it was the correct solution given the effort/feel ratio. \$\endgroup\$
    – wondra
    May 4 '18 at 18:06
  • 1
    \$\begingroup\$ That's cool and very similar to my 'technique' here lol. but as you rightly say it doesnt feel like Unity paid any attention to adding GUI functions to the games. (not saying they put no effort in, theres obviously many features here such as stretch and other layout options which im thankful of). Seems wierd though, 100% of games have a UI, if I owned Unity I would want the UI's for games made in Unity to be of the highest quality so I would probably get a team to work on making this better. But in short, I don't own nothing hahah \$\endgroup\$ May 6 '18 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.