Search Unity

Pick a Rect from your scene and build a Sprite with it

Discussion in '2D' started by fonko, Aug 27, 2018.

  1. fonko

    fonko

    Joined:
    Mar 20, 2014
    Posts:
    23
    Im not sure if this is the place to ask this question, i started a game that needs like 10k platforms so my player can keep jumping up-left and up-right and new platforms will always be there for the player.

    I make a complex object pooling system but still... some android phones find this heavy to load when new platforms are being set active.

    So i thought on doing a big Sprite with randommly generated platforms and use it as a big sprite.

    my problem is that i can't find a why to kind of take a screenshot of this set of sprites and make a big sprite with those.

    this is the code i'm using but it uses a second camera and it gaves a blurry picture of all the platforms

    Code (CSharp):
    1.  private void GenerateBigSprite()
    2.     {
    3.         Camera activeCamera = cam2;
    4.  
    5.         Texture2D tex = new Texture2D(1000, 1000,TextureFormat.ARGB32, false);
    6.  
    7.         // ofc you probably don't have a class that is called CameraController :P
    8.  
    9.         // Initialize and render
    10.         RenderTexture rt = new RenderTexture(1000, 1000, 32);
    11.         activeCamera.targetTexture = rt;
    12.         activeCamera.Render();
    13.         RenderTexture.active = rt;
    14.  
    15.         // Read pixels
    16.         Rect rectReadPicture = new Rect(0, 0, 1000, 1000);
    17.         tex.ReadPixels(rectReadPicture, 0, 0);
    18.         tex.Apply();
    19.  
    20.         byte[] bytes = tex.EncodeToPNG();
    21.         NewSprite = Sprite.Create(tex, new Rect(0, 0, 1000, 1000), new Vector2(100, 100), 100f);
    22.         GameObject coso = new GameObject("coso");
    23.         coso.transform.parent = null;
    24.         coso.transform.position = new Vector3(0, 0, 0);
    25.         coso.AddComponent<SpriteRenderer>().sprite = NewSprite;
    26.         // Clean up
    27.         RenderTexture.active = null; // added to avoid errors
    28.                                      // DestroyImmediate(rt);
    29.  
    30.     }
    31.  
    Since this code depends on what the second camera renders, i had to increase camera size to 100 and this is what i get

    plataformasss.PNG

    If i use camera size 5 (normal camera size) i only get a small portion of the screen which doesn't work for me since i need to at least have 20k platforms in one sprite. but of course with size 5 platforms look beautiful

    plataformrmrmrm.PNG


    There must be another way not using an extra camera where i set all my platforms and then establish a rect with an area that covers all of them and then create a Sprite from that area... am i wrong?

    thanks in advance guys!
     
  2. fonko

    fonko

    Joined:
    Mar 20, 2014
    Posts:
    23
    no one :(