How can I get structs to appear in the inspector? (Making a shop screen)

I’m trying to figure out how to set up a shop screen. (And I welcome advice on other ways to approach this, this is just what I thought sounded to simplest.) I want my shop screen to have items displayed in a grid.

The way I figured I should do this is to create a struct for the slots in the shop screen to hold what kind of item it is, its cost, and maybe more stuff if I find it necessary. So next I want to create a multidimensional array of these slots so each one can have a location in x and y in the shop screen matrix.

However the first hurdle I have found is that I cannot get these structs to appear in the inspector. I was hoping I could make the matrix a public variable and then I could easily go into each shop in my game and just drop in values for what they sell at how much. But any variable with my struct in it doesn’t appear in the inspector.

Furthermore, multidimensional arrays don’t appear in the inspector. So how can I have a multidimensional array that I can access through the editor?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct ShopSlot
{
    public GameObject Item; // replace with a shopItem script once that exists

    public int Cost;
}

public class ShopScreen : MonoBehaviour
{
    public int MatrixSizeX;
    public int MatricSizeY;

    public ShopSlot[,] ShopMatrix;
}
[System.Seralizable]
public struct ShopSlot
{
    public GameObject Item; // replace with a shopItem script once that exists
    public int Cost;
}
1 Like

Simply add the [Serializable] attribute to any class or struct you want to show in the inspector:

[Serializable]
public struct ShopSlot
{
    public GameObject Item; // replace with a shopItem script once that exists
    public int Cost;
}

Unity doesn’t support serializing multi-dimensional arrays though, but funnily enough, you could use another serializable class or struct to work around that:

[Serializable]
public class ShopMatrix
{
  public ShopSlot x;
  public ShopSlot y;
}

Then just create a regular array of that instead:

public class ShopScreen : MonoBehaviour
{
    public int MatrixSize;
    public ShopMatrix[] ShopMatrix;
}
3 Likes

Wait,

You can use Horizontal layout component. And the like components, Layout Group, if the shop is user interface. These all function via parenting in the hierarchy.

I see what you are getting at, but really I would want to replace that X and Y with another array, I think? Oh wait, but that could potentially be inconsistent, because each array within the array could be a different size.
I haven’t decided on how big my shops are going to be, so I was trying to build a flexible system that would allow me to easily revise the size of a shop screen later.

@AnimalMan I didn’t know that was a thing; I have only taken a cursory glance at the UI system. (I’ve been using the legacy GUI for placeholder stuff because I’m already quite familiar with that.)
But I guess this means… I could just create the screen in the editor and assign values to individual slots?
I’m just so used to needing an explicitly-controlled backend that I didn’t think that I could craft it that way.
Huh. I guess I’ll study the UI system a bit more and see if I even need a single script to contain the whole shop screen.