Unity网格编程(2017.3)课程(Andy老师)的模型切割案例,采用Unity自带的模型Cube\sphere等模型可以成功切割,但采用自编模型(同样的正方体)就无法成功切割,无任何反应,也不报错。自己思考了很久,都想不明白是何原因,切割功能的脚本是完全采用的Andy老师课程中的最终脚本,应该没有问题,能够兼容这种情况,而生成模型的代码应该也没有问题,想请问老师这种情况可能是什么原因造成的?
图一:Unity自带的Cube模型,能够正常切割
图二:采用脚本自己生成的正方体模型,无法切割,没有丝毫效果
生成模型的代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class model3 : MonoBehaviour
{
private Vector3[] _vertices;
private int[] _triangles;
void Start()
{
Mesh mesh = new Mesh();
MeshFilter filter = GetComponent<MeshFilter>();
filter.mesh = mesh;
mesh.name = "Mesh03";
GenerateVextex();
GenerateTriangle();
mesh.vertices = _vertices;
mesh.triangles = _triangles;
GameObject newGo = new GameObject();
newGo.transform.position = filter.transform.position;
newGo.transform.rotation = filter.transform.rotation;
//newGo.transform.rotation = _hitTrans.rotation;
newGo.AddComponent<MeshFilter>().mesh = mesh;
newGo.AddComponent<MeshRenderer>().material = filter.GetComponent<MeshRenderer>().material;
}
private void GenerateVextex()
{
_vertices = new Vector3[8];
_vertices[0] = new Vector3(-0.5f, -0.5f, -0.5f);
_vertices[1] = new Vector3(0.5f, -0.5f, -0.5f);
_vertices[2] = new Vector3(-0.5f, -0.5f, 0.5f);
_vertices[3] = new Vector3(0.5f, -0.5f, 0.5f);
_vertices[4] = new Vector3(-0.5f, 0.5f, -0.5f);
_vertices[5] = new Vector3(0.5f, 0.5f, -0.5f);
_vertices[6] = new Vector3(-0.5f, 0.5f, 0.5f);
_vertices[7] = new Vector3(0.5f, 0.5f, 0.5f);
}
private void GenerateTriangle()
{
_triangles = new int[36];
//底面
_triangles[0] = 0;
_triangles[1] = 1;
_triangles[2] = 2;
_triangles[3] = 1;
_triangles[4] = 3;
_triangles[5] = 2;
//前
_triangles[6] = 0;
_triangles[7] = 4;
_triangles[8] = 5;
_triangles[9] = 5;
_triangles[10] = 1;
_triangles[11] = 0;
//后
_triangles[12] = 3;
_triangles[13] = 7;
_triangles[14] = 6;
_triangles[15] = 3;
_triangles[16] = 6;
_triangles[17] = 2;
//左
_triangles[18] = 6;
_triangles[19] = 4;
_triangles[20] = 0;
_triangles[21] = 6;
_triangles[22] = 0;
_triangles[23] = 2;
//右
_triangles[24] = 1;
_triangles[25] = 5;
_triangles[26] = 7;
_triangles[27] = 7;
_triangles[28] = 3;
_triangles[29] = 1;
//上
_triangles[30] = 6;
_triangles[31] = 7;
_triangles[32] = 4;
_triangles[33] = 7;
_triangles[34] = 5;
_triangles[35] = 4;
}
}
联系方式,QQ 1157947543,请指教,谢谢!
同学你好,这个老师具体没有测试过,同学可以测试一下,换一下其他的模型,不用默认的和生成的模型试一下看看,可能切割方法的问题也可能是材质的问题