简体   繁体   English

在运行时动态创建变量

[英]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. 我正在使用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. 最初,我是使用Unity的“ Object.Instantiate”方法动态创建弹丸的(很好,所以我知道可以做到)。 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. 在开始玩游戏之前,对象缓存使用Unity的“ Object.Instantiate”方法将游戏对象的“ cacheAmount”放入字典中。

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. 当“ cacheAmount”小于64时,这对性能产生了奇迹。如果将“ cacheAmount”增加到64以上,则ObjectCache的性能将比使用Unity的“ Object.Instantiate”方法差。

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. 我决定将字典分成8卷,每卷有8个对象,第1-8卷,共64个对象。 Having these smaller volumes increased my performance again. 这些较小的音量再次提高了我的性能。

I fiddle around for a bit and hard coded 32 volumes with 8 objects. 我花了一些力气,用8个对象对32卷进行了硬编码。 This allowed for 256 projectiles to be fired without a performance impact. 这样就可以发射256枚弹丸,而不会影响性能。

Optimally, I would not use a dictionary at all, simply create "cacheAmount" of variables, and loop through them all. 理想情况下,我根本不会使用字典,只需创建变量的“ cacheAmount”,然后遍历所有变量即可。 This way I could change cacheAmount, and cache 256 or more individual objects without having to hard code them 256 times beforehand. 这样,我可以更改cacheAmount并缓存256个或更多的单个对象,而不必事先对其进行256次硬编码。

so for simplicity sake a I need something like this: 为了简单起见,a我需要这样的东西:

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. 然后,我将不得不访问dynamicVariable1,dynamicVariable2,dynamicVariable3,dynamicVariable4等。

How do I go about this in C# or JavaScript/UnityScript? 如何在C#或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? 您真的需要实例化256个gameObjects吗? Can't you reuse some of the gameObjects? 您不能重用某些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. 您不需要字典,也不需要动态变量(无论它们是什么),只需创建一个数组并使用RoundRobin访问该数组中的下一个自由射弹即可。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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