Search Unity

How can i efficient draw many different mesh without GameObject?

Discussion in 'Scripting' started by qpuilie, Jul 15, 2021.

  1. qpuilie

    qpuilie

    Joined:
    Jan 14, 2020
    Posts:
    69
    Hi,
    I have many different mesh(they use same material).And I don't want to create many GameObject.Now i have to call Graphics.DrawMesh() in the loop.
    such as:
    Code (CSharp):
    1.  
    2. for (int i = 0; i < _currentPreviewConnections.Length; i++) {
    3.     Graphics.DrawMesh(
    4.         _currentPreviewConnections[i],
    5.         Vector3.zero,Quaternion.identity,
    6.         _connectionMaterial,
    7.         Consts.BuildingLayer);
    8.  }
    9.  
    Is there a more efficient way to do it?
    It confused me , and if you can help me, I would be very grateful.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Quick question, why? How many meshes are we talking about? Are you sure, this will be more convenient, than just creating those game objects and never having to worry about them again?
     
  3. qpuilie

    qpuilie

    Joined:
    Jan 14, 2020
    Posts:
    69
    For example:
    Each club has many roads.
    These road's meshes are procedural generated, i have to destroy them while change club if i use GameObject.
    So I think it will be more convenient and efficient to use mesh.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You don't have to destroy any more with the game object method than with the DrawMesh method. You just have to change their MeshFilter.sharedMesh and that's it. Ultimately you're sacrificing a lot of flexibility and debuggability for performance you don't even know if you're getting. A really important programming concept that most people miss is that you can't make nonexistent code faster. Make it work and then if you see that some part of it is underperforming, then you can optimize it by profiling what part of your code is actually the slowest
     
  5. qpuilie

    qpuilie

    Joined:
    Jan 14, 2020
    Posts:
    69
    Yes I know,But my initial consideration is that if it is the first time to enter a club, I may need to initialize dozens or hundreds of GameObjects. Then I can reuse them from the object pool. Thank you for your patient answer, maybe this is feasible.
     
  6. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201