Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Draw Batching with Graphics.DrawMesh

Discussion in 'Editor & General Support' started by BerzerkLachhh, Aug 28, 2010.

  1. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    Hey guys,

    I have over 160 GameObject which each contain a meshFilter and a sharedMesh with 4 vertices. They all have the same material.

    So when I put then on the camera, the whole thing is batched automaticly (thanks to Unity3 !). But if I try

    Code (csharp):
    1.  
    2. var meshF:MeshFilter = GetComponent(MeshFilter);
    3. Graphics.DrawMesh(meshF.sharedMesh, pos, Quaternion.identity, meshF.renderer.sharedMaterial, 0);
    I can see the visual properly, but they are not batched anymore. Anybody have an idea ?

    What I'm trying to achieve is store and load about ~500 prefab once, and draw them on the screen myself, calculating their positions etc (for a 2d iphone game).

    Thanks !
     
    dot_entity likes this.
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    batching will not work with manual draw commands. they explicitely create own meshes.

    May I ask why you don't create these meshes programmatically through the mesh class, hook them up on GOs and use them like that in which case they would batch?

    Alternatively you can naturally also do the batching on your end and draw 1 mesh per material with all the corresponding vertices and triangles in.
     
  3. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    Thanks for the quick reply !

    Maybe because I still suck at Unity ! :D

    But seriously, I'll explain my problem in more detail.
    To put it simple, I'm working on the port of this game http://www.berzerkstudio.com/games.php?game=4 I have about 400+ textures of different non-unifrom format. I used Sprite Manager 2 and I think that's what the script does already.

    When I put my game on my iPod touch, according to the iphone profiler, the main thing that is dropping my FPS down is my scripts. So I wanted to optimize them and not create / cache GOs each frame, hence the solution I thought about above.

    But maybe I don't understand your solution perfectly ?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Just because you're creating game objects doesn't mean you have to attach scripts to them.

    --Eric
     
  5. BerzerkLachhh

    BerzerkLachhh

    Joined:
    Jun 24, 2010
    Posts:
    28
    I have only one main script that is updated and create all my GameObject (Prefab with PackedSprite attached), and according to Unity profiler, that's the main script who takes all the cpu power.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Ah, well, in that case Graphics.DrawMesh probably won't gain you anything in this case, except saving some memory, at the expense of not having dynamic batching. Sounds like you should try to optimize the script in other ways.

    --Eric