游戏运行时如何使用C#脚本修改网格 [英] How can I modify my mesh with a C# script while the game is running
问题描述
进入播放模式后,我正在尝试在Unity游戏运行时连续更改脚本.我制作了一个程序生成的网格,并为脚本分配了网格碰撞器,滤镜和渲染器组件.
I am trying to change my script continuously while the Unity game is running after I have entered play mode. I made a procedurally generated mesh and assigned a mesh collider, filter, and a renderer component to the script.
现在,我希望网格物体在游戏运行时改变高度.我认为可以在Update
函数中完成此操作,但不确定如何实现.
Now I want the mesh to change heights while the game is running. I figure this would be done in the Update
function but I am not sure how I would implement it.
这是我到目前为止所得到的:
Here's what I got so far:
void Start()
{
mesh = new Mesh();
MeshFilter filter = gameObject.AddComponent<MeshFilter>();
MeshRenderer render = gameObject.AddComponent<MeshRenderer>();
GetComponent<MeshFilter>().mesh = mesh;
GetComponent<Renderer>().material = grassMat;
CreateShape();
UpdateMesh();
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
MeshCollider collider = gameObject.AddComponent<MeshCollider>();
mesh.RecalculateNormals();
}
推荐答案
由于示例代码并未真正完成,因此我将指出一些常见的可能错误:
Since the example code is not really complete, i´ll point you to some common possible errors:
-
void根据帧速率调用每一帧的void Update(),以半固定帧速率调用void FixedUpdate()的每一帧.如果希望每个(固定)帧都发生一些事情,则应将代码放在其中.在您的示例中,您仅将方法命名为void UpdateMesh(),这并不意味着它在每个框架中都被调用:尚不清楚,如果在void UpdateMesh()中根本没有发生任何更改,则不清楚.例如,没有代码可以改变顶点的位置.
from you posted code it´s not clear, if there are changes happening in void UpdateMesh() at all. For example there is no code which changes positions of vertices.
这篇关于游戏运行时如何使用C#脚本修改网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!