当前位置:网站首页>Unity grid programming 08

Unity grid programming 08

2022-06-23 08:44:00 LittleU

Using the previous grid programming method , Just know how to generate the mesh , Now the advanced API, Programming , So advanced api And previous api What are the differences ?

First, follow the previous method to understand , First of all, there is data like grid , And then all of a sudden open up space in the memory , Then assign these data to mesh On this component .

And if the model is complex enough , This is bound to cause huge computation in a certain frame .

If advanced api, Operate directly in the requested memory

First, the same as before , Add script dependencies , And assignment

[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class AdvancedMultiStreamProceduralMesh : MonoBehaviour

...
  var mesh = new Mesh
        {
            name = "Procedural Mesh"
        }; 
   
  GetComponent<MeshFilter>().mesh = mesh;
  ....

This is the same as the previous code , There is no need to explain .

So first of all , This advanced API Usage of , First, you need to apply for a memory space , This memory space can be directly manipulated by code :

 //1. Allocate writable grid data list , It can be more than one 
        MeshDataArray meshDataArray = Mesh.AllocateWritableMeshData(1);

then , Save it again

 
        var mesh = new Mesh
        {
            name = "Procedural Mesh"
        };

      
        Mesh.ApplyAndDisposeWritableMeshData(meshDataArray, mesh);
        
        GetComponent<MeshFilter>().mesh = mesh;

that , Same thing , The rest of the code , You need to add it here .

原网站

版权声明
本文为[LittleU]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/01/202201101559036645.html