简体   繁体   中英

Dynamically Create Variables At Runtime

(Yes, I actually need to dynamically create variables, and not just use an array, list or dictionary.)

I am developing a video game using Unity. I have projectiles being fired from a cannon. At first I was having the projectiles dynamically created (good so I know it can be done) using Unity's "Object.Instantiate" method. This would cause my game to chunk out while the projectile was loaded into memory.

My solution was to create an Object Cache / Object Pool. The Object Cache uses Unity's "Object.Instantiate" method to place "cacheAmount" of game objects into a dictionary before we start playing.

This worked wonders for performance when "cacheAmount" was less than 64. If "cacheAmount" was increased above 64 the ObjectCache performed worse than using Unity's "Object.Instantiate" method.

I took a guess and figured that the dictionary's memory space was too large and that the time it took to access the whole block was what was slowing down the game.

I decided to split the dictionary up into 8 volumes with 8 objects each, volumes 1-8 with a total of 64 objects. Having these smaller volumes increased my performance again.

I fiddle around for a bit and hard coded 32 volumes with 8 objects. This allowed for 256 projectiles to be fired without a performance impact.

Optimally, I would not use a dictionary at all, simply create "cacheAmount" of variables, and loop through them all. This way I could change cacheAmount, and cache 256 or more individual objects without having to hard code them 256 times beforehand.

so for simplicity sake a I need something like this:

int cacheAmount = 256;
    for (int i = 0; i < cacheAmount; i++)
    {
        GameObject dynamicVariable>i< = (GameObject)Instantiate(projectile);
    }

Then I would have to access dynamicVariable1, dynamicVariable2, dynamicVariable3, dynamicVariable4 etc.

How do I go about this in C# or JavaScript/UnityScript?

(once again using an array, list, or dictionary, yields a memory space too large and impacts performance)

You really need to instantiate 256 gameObjects? Can't you reuse some of the gameObjects?

You don't need a dictionary and you don't need dynamic variables (whatever they are), just create an array and use RoundRobin to access the next free projectile in the array.

On your pseudocode you are not storing the reference anywhere, you declare it locally and then forget so there's no way to access them.

What you want to do is more along the lines of

int cacheAmount = 256;
GameObject[] cache=new GameObject[cacheAmount];
int nextObjectIndex=0;
void CreateCache()
{
    for (int i = 0; i < cacheAmount; i++)
    {
        cache[i] = (GameObject)Instantiate(projectile);
    }
}

GameObject GetFromCache()
{
   GameObject nextObject=cache[nextObjectIndex];
   nextObjectIndedx++;
   if (nextObjectIndex>=cacheAmount) nextObjectIndex=0;
   return nextObject;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM