A structure type (or struct type) is a value type that can encapsulate data and related functionality.
A struct is a simple container that binds several variables into a single entity.
Main difference between _struc_t and _clas_s is where they are stored:
struct (value type) — is stored in Stack. How does that work? Passed by value, when you assign a struct to a new variable, C# creates its standalone clone. In Unity, creation and deletion of struct occur instantly and do not load the Garbage Collector.
class (reference type) — is stored in Heap. How does that work? Passed by reference. Several variables can refer to the same object in memory. In Unity class instances require heap allocation, and when they are destroyed, the Garbage Collector is triggered, which can lead to micro-freezes during gameplay.
Yesterday I started making a small project to level up my skills, a simple slot machine that outputs 3 random fruits. You pull the lever and magic happens (do not gamble, kids).
I needed some structure that allows me to modify multipliers and payouts through the inspector, and I found an attribute [System.Serializable]. By default,Unity cannot see a custom struct in the inspector, so you can’t simply make a struct [SerializeField]. Here’s an example of how to do it.
And this is what it looks like in the inspector. I needed a key - value type of data:




Top comments (0)