游戏运行时如何使用C#脚本修改网格 [英] How can I modify my mesh with a C# script while the game is running

查看:137
本文介绍了游戏运行时如何使用C#脚本修改网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

进入播放模式后,我正在尝试在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:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆