tanis1
tanis1

Reputation: 42

Unity Dictionary Use

I am currently working on an educational game in Unity in which the player will measure certain information about an object, such as its height, and weight. I decided to implement this using an enumerated type containing the objects to be measured, and several Dictionaries using the enumerated type as the key, an example of this which is seen below.

static Dictionary <MeasurableObjects,int> heights = new 
Dictionary<MeasurableObjects, int> ()
{
    {MeasurableObjects.Avatar,42},
    {MeasurableObjects.Dog,24},
    {MeasurableObjects.Cat,6},
    {MeasurableObjects.Pig,30},
    {MeasurableObjects.Hen, 18},
    {MeasurableObjects.Cow, 72},
    {MeasurableObjects.Fox, 18},
    {MeasurableObjects.Fish,8},
    {MeasurableObjects.Duck, 12},
    {MeasurableObjects.Horse,84},
    {MeasurableObjects.Sheep, 36},
    {MeasurableObjects.Pony, 60},
    {MeasurableObjects.Bear, 54},
    {MeasurableObjects.Camel,96},
    {MeasurableObjects.Tiger,48}
};

I have many of these, sometimes even using Sprites as the data type. I was wondering, since the dictionaries are all static does this mean that I would have a bunch of data structures sitting around in memory at all times, or would the required information be looked up and pulled as it is needed?

Upvotes: 0

Views: 975

Answers (3)

Xarbrough
Xarbrough

Reputation: 1461

Yes, as others have noted, static objects will remain in memory from when they are first used until the program terminates.

You might be happier to work with ScriptableObject in Unity. It works like a MonoBehaviour but is instantiated on the project level, not the scene. This means, it works like any basic asset. You can't directly serialize dictionaries in Unity, but you can create small simple data types to represent your information and then use Resources.Load to pull in your asset when you need it.

Here's a simplification (you would probably add functionality and share common operations between different types etc.:

[CreateAssetMenu("MyHeightsAsset")]
public class Heights : ScriptableObject
{
    public static Heights Load()
    {
        return Resources.Load("MyHeightsAsset");
    }

    public KeyValuePair[] valueMap;

    [Serializable]
    public class KeyValuePair
    {
        public MeasurableObjects obj;
        public int value;
    }

    public enum MeasurableObjects
    {
        Avatar,
        Dog
    }
}

Upvotes: 0

Blubberguy22
Blubberguy22

Reputation: 1332

When you created the dictionary, you made it static. This means that they stay in memory for the running lifetime of the application. In your situation, the static object is heights.

From Wikipedia:

In computer programming, a static variable is a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program.

Upvotes: 0

Ren&#233; Vogt
Ren&#233; Vogt

Reputation: 43906

Since they are static, they are created the first time the declaring type (the class in which you declared heights) is used.

After this creation, they stay in memory for the rest of your process's life time.

Upvotes: 1

Related Questions