Unity에서 코드로 큐브 그리기

2586 단어
어떻게 코드로 큐브를 그립니까?말을 많이 하지 않고 직접.
    var pMeshFilter = gameObject.AddComponent(typeof(MeshFilter)) as MeshFilter;//     
    var pMeshRender = gameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;//    
    var pMesh = pMeshFilter.mesh as Mesh;    //          Mesh
    Vector3[] pVector = new Vector3[36];
    int[] pTriangles = new int[pVector.Length];
    //forword
    pVector[0] = new Vector3(-0.5f, -0.5f, -0.5f);
    pVector[1] = new Vector3(-0.5f, 0.5f, -0.5f);
    pVector[2] = new Vector3(0.5f, 0.5f, -0.5f);

    pVector[3] = new Vector3(-0.5f, -0.5f, -0.5f);
    pVector[4] = new Vector3(0.5f, 0.5f, -0.5f);
    pVector[5] = new Vector3(0.5f, -0.5f, -0.5f);
    back
    pVector[6] = new Vector3(-0.5f, 0.5f, 0.5f);
    pVector[7] = new Vector3(-0.5f, -0.5f, 0.5f);
    pVector[8] = new Vector3(0.5f, 0.5f, 0.5f);

    pVector[9] = new Vector3(0.5f, 0.5f, 0.5f);
    pVector[10] = new Vector3(-0.5f, -0.5f, 0.5f);
    pVector[11] = new Vector3(0.5f, -0.5f, 0.5f);
    //left
    pVector[12] = new Vector3(-0.5f, -0.5f, -0.5f);
    pVector[13] = new Vector3(-0.5f, 0.5f, 0.5f);
    pVector[14] = new Vector3(-0.5f, 0.5f, -0.5f);

    pVector[15] = new Vector3(-0.5f, -0.5f, -0.5f);
    pVector[16] = new Vector3(-0.5f, -0.5f, 0.5f);
    pVector[17] = new Vector3(-0.5f, 0.5f, 0.5f);
    //right
    pVector[18] = new Vector3(0.5f, -0.5f, -0.5f);
    pVector[19] = new Vector3(0.5f, 0.5f, -0.5f);
    pVector[20] = new Vector3(0.5f, 0.5f, 0.5f);

    pVector[21] = new Vector3(0.5f, -0.5f, -0.5f);
    pVector[22] = new Vector3(0.5f, 0.5f, 0.5f);
    pVector[23] = new Vector3(0.5f, -0.5f, 0.5f);
    //up
    pVector[24] = new Vector3(0.5f, -0.5f, 0.5f);
    pVector[25] = new Vector3(-0.5f, -0.5f, 0.5f);
    pVector[26] = new Vector3(-0.5f, -0.5f, -0.5f);

    pVector[27] = new Vector3(0.5f, -0.5f, 0.5f);
    pVector[28] = new Vector3(-0.5f, -0.5f, -0.5f);
    pVector[29] = new Vector3(0.5f, -0.5f, -0.5f);
    //down
    pVector[30] = new Vector3(-0.5f, 0.5f, -0.5f);
    pVector[31] = new Vector3(-0.5f, 0.5f, 0.5f);
    pVector[32] = new Vector3(0.5f, 0.5f, 0.5f);

    pVector[33] = new Vector3(-0.5f, 0.5f, -0.5f);
    pVector[34] = new Vector3(0.5f, 0.5f, 0.5f);
    pVector[35] = new Vector3(0.5f, 0.5f, -0.5f);

    for (int nIndex = 0; nIndex < pTriangles.Length; ++nIndex)
    {
        pTriangles[nIndex] = nIndex;
    }
    pMesh.Clear();
    pMesh.vertices = pVector;//    
    pMesh.triangles = pTriangles;//   
    pMesh.RecalculateBounds();//             

물론 더 잘하는 방법도 많고 더 적은 공용 정점을 잘 못 쓰니까 신이 많이 지도해 주셨으면 좋겠어요!!

좋은 웹페이지 즐겨찾기