C# 程序网格减慢了场景的速度

C# 程序网格减慢了场景的速度,c#,unity3d,mesh,C#,Unity3d,Mesh,我在Unity3d中创建了一个非常简单的游戏,需要创建多个网格。我创建的代码非常简单,但是在同时拥有8个以上的网格之后,PeerPerformance会大大降低到仅几帧(约8帧)。我创建的网格只是一个简单的正方形,所以我真的不知道问题出在哪里,下面是我的代码: using UnityEngine; using System.Collections; public class TetraGenerator : MonoBehaviour { public int slices;

我在Unity3d中创建了一个非常简单的游戏,需要创建多个网格。我创建的代码非常简单,但是在同时拥有8个以上的网格之后,PeerPerformance会大大降低到仅几帧(约8帧)。我创建的网格只是一个简单的正方形,所以我真的不知道问题出在哪里,下面是我的代码:

using UnityEngine;
using System.Collections;

public class TetraGenerator : MonoBehaviour {
    public int slices;
    public GameObject forceSource;
    void OnMouseDown(){
        var arcLength = Mathf.PI / slices;
        var distance = 10;
        var height = 1;
        var origin = Random.Range(-slices,slices);

        Vector3[] vertices = new Vector3[4];
        vertices [0] = new Vector3 (Mathf.Cos(origin*arcLength),Mathf.Sin(origin*arcLength));
        vertices [1] = new Vector3 (Mathf.Cos(origin*arcLength),Mathf.Sin(origin*arcLength));
        vertices [2] = new Vector3 (Mathf.Cos((origin+1)*arcLength),Mathf.Sin((origin+1)*arcLength));
        vertices [3] = new Vector3 (Mathf.Cos((origin+1)*arcLength),Mathf.Sin((origin+1)*arcLength));

        vertices [0] *= distance;
        vertices [1] *= (distance+height);
        vertices [2] *= (distance+height);
        vertices [3] *= distance;

        Vector3 frameRef = new Vector3(Mathf.Cos(origin*arcLength+(arcLength/2)),Mathf.Sin(origin*arcLength+(arcLength/2)));
        frameRef *= distance;

        vertices [0] -= frameRef;
        vertices [1] -= frameRef;
        vertices [2] -= frameRef;
        vertices [3] -= frameRef;

        int[] triangles = new int[]{0,1,2,2,3,0};

        Mesh mesh = new Mesh ();
        mesh.vertices = vertices;
        mesh.triangles = triangles;

        GameObject tile = new GameObject("tile",typeof(MeshFilter),typeof(MeshRenderer));
        tile.transform.position = frameRef;

        MeshFilter meshFilter = tile.GetComponent<MeshFilter> ();
        meshFilter.mesh = mesh;

    }
}
使用UnityEngine;
使用系统集合;
公共类生成器:单行为{
公共int切片;
公共游戏对象资源;
void OnMouseDown(){
var arcLength=Mathf.PI/片;
var距离=10;
var高度=1;
变量原点=随机范围(-slices,slices);
Vector3[]顶点=新Vector3[4];
顶点[0]=新矢量3(Mathf.Cos(原点*弧长),Mathf.Sin(原点*弧长));
顶点[1]=新向量3(Mathf.Cos(原点*弧长),Mathf.Sin(原点*弧长));
顶点[2]=新向量3(数学Cos((原点+1)*弧长),数学Sin((原点+1)*弧长));
顶点[3]=新向量3(数学Cos((原点+1)*弧长),数学Sin((原点+1)*弧长));
顶点[0]*=距离;
顶点[1]*=(距离+高度);
顶点[2]*=(距离+高度);
顶点[3]*=距离;
Vector3 frameRef=新的Vector3(Mathf.Cos(原点*弧长+(弧长/2)),Mathf.Sin(原点*弧长+(弧长/2));
frameRef*=距离;
顶点[0]=frameRef;
顶点[1]=frameRef;
顶点[2]=frameRef;
顶点[3]=frameRef;
int[]三角形=新的int[]{0,1,2,2,3,0};
网格=新网格();
网格顶点=顶点;
mesh.triangles=三角形;
GameObject tile=新游戏对象(“tile”、typeof(MeshFilter)、typeof(MeshRenderer));
tile.transform.position=frameRef;
MeshFilter-MeshFilter=tile.GetComponent();
meshFilter.mesh=网格;
}
}

您的问题在于您没有设置材质,或者没有提供材质所需的一切,如uv坐标或顶点颜色。我不确定是Debug.Log中的错误消息造成的,还是着色器本身导致低帧率,但要测试它,可以使用:

// enter this at the top and set the material in the inspector
public Material mat;

[...]

// enter this at the bottom
MeshRenderer meshRenderer = tile.GetComponent<MeshRenderer>();
meshRenderer.material = mat;
Shader "SimpleShader"
{
    SubShader
    {
        Pass
        {
            CGPROGRAM

            #pragma vertex vert
            #pragma fragment frag

            struct vertexInput
            {
                float4 pos : POSITION;
            };

            struct vertexOutput
            {
                float4 pos : SV_POSITION;
                float4 col : COLOR0;
            };

            vertexOutput vert(vertexInput input)
            {
                vertexOutput output;

                output.pos = mul(UNITY_MATRIX_MVP, input.pos);
                output.col = float4(1, 0, 0, 1);

                return output;
            }

            float4 frag(vertexOutput input) : COLOR
            {
                return input.col;
            }

            ENDCG
        }
    }
}