在 Unity 中创建和渲染大量 2d 精灵的最快方法是什么? [英] What is the fastest method to create and render large number of 2d sprites in Unity?

查看:42
本文介绍了在 Unity 中创建和渲染大量 2d 精灵的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个无限地形生成器,我需要在玩家四处移动时不断更新地形.

I am creating an infinite terrain generator and I need to constantly update the terrain as the player moves around.

一切都很好,但我无法找到有关即时创建和渲染精灵的最快方法的信息.

Everything is fine, but I am having trouble with finding information about the fastest method of creating and rendering sprites on the fly.

关于精灵的信息:

我正在使用 1 个精灵表,其中包含我的地形所需的所有帧.草、沙子、水等都在 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.

当前正确显示我的精灵对象需要执行的步骤:

  1. 创建新对象.
  2. 在二维空间中设置它们的位置.
  3. 添加组件.
  4. 根据需要缩放它们.

生成的精灵存储在一个名为 chunkGameObject 数组中.这是我目前生成精灵的方式.

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!

这篇关于在 Unity 中创建和渲染大量 2d 精灵的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆