在Unity中创建和渲染大量2d精灵的最快方法是什么?
我正在创建一个无限的地形生成器,我需要在玩家四处移动时不断更新地形.
I am creating an infinite terrain generator and I need to constantly update the terrain as the player moves around.
一切都很好,但是我很难找到有关快速创建和渲染Sprite的最快方法的信息.
Everything is fine, but I am having trouble with finding information about the fastest method of creating and rendering sprites on the fly.
有关精灵的信息:
我正在使用1张精灵表,其中包含我的地形所需的所有帧.草,沙,水等全部集中在一个单个的.png文件中.所有帧都存储在一个数组中,我可以从中轻松抓取它们.
I am using 1 sprite sheet which has all the frames I need for my terrain. Grass, sand, water etc. all in 1 single .png file. All frames are stored in an array from which I can easily grab them.
当前需要正确显示我的精灵对象的步骤:
- 创建新对象.
- 设置其在2d空间中的位置.
- 添加组件.
- 根据需要缩放它们.
生成的精灵存储在名为chunk
的GameObject
数组中.这是我目前生成子画面的方式.
Generated sprites get stored in a GameObject
array called chunk
. This is the way I am currently generating sprites.
chunk[i] = new GameObject();
chunk[i].gameObject.transform.position = new Vector2(spriteCoordX, spriteCoordY);
chunk[i].AddComponent<SpriteRenderer>();
SpriteRenderer renderer = chunk[i].GetComponent<SpriteRenderer>();
renderer.sprite = tiles[calculatedFrameId]; //Set correct sprite frame.
chunk[i].gameObject.transform.localScale = new Vector2(6.75f , 6.75f);
我不知道,每次我想在代码中创建一个新的精灵时,添加组件并进行缩放似乎是多余的和不必要的,而且我确信有更好的方法来做到这一点.
I don't know, adding component and scaling every single time I want to create a new sprite in code seems redundant and unnecessary and I am sure there is a better way to do that.
总结:
我需要最好的(最快的)方式来生成大量精灵,设置其帧,位置和适当的比例.
I need the best (fastest) possible way to generate large number of sprites, set their frame, position and proper scale.
在此不免张贴此图像,因为它确实包含数千个单词,感谢@AidenH的对象池"注释!
Cannot help posting this image here, as this is really of thousands of words, thanks @AidenH for the "Object pooling" comment!