Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Creating many gameobject slow

Discussion in 'Getting Started' started by unity_B180E0F3EF7502A517F1, Nov 12, 2021.

  1. unity_B180E0F3EF7502A517F1


    Nov 6, 2021
    I am creating sprites from code, this way:

    Code (CSharp):
    1. Sprite sprite = atlasHelper.GetSprite(2, 0); //my class, it gives back a sprite, based on a texture atlas position
    2. GameObject obj = new GameObject();
    3. SpriteRenderer rend = obj.AddComponent(typeof(SpriteRenderer)) as SpriteRenderer;
    4. rend.sprite = sprite;
    5. obj.transform.position = new Vector3Int(x, y, 0);
    I am new, is this a good solution, or there is a better way? I noticed if I create 500+ spirtes (many has the same texture bounds from the atlas, if it is matter) it needs same seconds to start. Which is not bad, but I want to go with the better solution if there is any.

    Maybe can I use the same sprite object and same spirteRender for the same looking (but other position) spirtes?