Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How can I do basic sprite rendering (render_sprite(pos, rotation, scale)) within a script?

Discussion in '2D' started by Radnom, Apr 12, 2015.

  1. Radnom

    Radnom

    Joined:
    Apr 12, 2015
    Posts:
    2
    Hi,
    I'm fairly new to Unity, and I've been having a lot of issues performing simple rendering tasks. I've been wanting to render a sprite many times with increasing rotation to form a circle. Here's a crummy diagram to explain my goal:



    The basic trig to get the positions and rotations is not a problem at all - the problem is I don't know how to do the basic render calls to draw the sprites. Looking through the manual, it seems I am expected to create new Sprite objects for each and every sprite around the circle - but if I'm drawing a lot of circles like this with high precision, this could be many thousands of individual objects when it would be a lot easier to manage a bunch of simple render calls in a row.

    There's one possible option I can take - manually creating the vertices of a the full circle with texture coordinates and set up indices and pass them into a MeshFilter (I think). But it seems like a lot more effort than it should take to simply render a bunch of simple rotated and scaled sprites.

    Is there any simple 'render sprite' draw call that I can place in a script in a draw event, like
    render_sprite(texture, x, y, xscale, yscale, rotation)?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Sprites are specifically SpriteRenderer components attached to GameObjects; they can't be rendered using any other technique. For immediate mode rendering, Graphics.DrawMesh exists. For performance reasons, though, you should really create a single mesh with all the quads placed appropriately. Dynamic batching is an option that exists, but it still takes time, and creating a mesh with all vertices already computed up front bypasses all that.

    --Eric
     
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  4. Radnom

    Radnom

    Joined:
    Apr 12, 2015
    Posts:
    2
    Ok, thanks for the responses! I might go about making a script to attach to an object with a DrawMesh component and give helper functionality to modify the vertices in a friendlier manner. I need to be able to modify things like the circle's radius and precision on the fly, so unfortunately I can't pre-compute the mesh on init.

    Cheers.
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I would still recommend using a standard mesh. There aren't any issues with modifying the radius and precision on the fly, and it will render faster as mentioned.

    --Eric