我创建自己的立方体。我使用下面的代码来做到这一点
void Start () {
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
Mesh mesh = new Mesh ();
meshFilter.mesh = mesh;
mesh.vertices = new Vector3[]{
// face 1 (xy plane, z=0)
new Vector3(0,0,0),
new Vector3(1,0,0),
new Vector3(1,1,0),
new Vector3(0,1,0),
// face 2 (zy plane, x=1)
new Vector3(1,0,0),
new Vector3(1,0,1),
new Vector3(1,1,1),
new Vector3(1,1,0),
// face 3 (xy plane, z=1)
new Vector3(1,0,1),
new Vector3(0,0,1),
new Vector3(0,1,1),
new Vector3(1,1,1),
// face 4 (zy plane, x=0)
new Vector3(0,0,1),
new Vector3(0,0,0),
new Vector3(0,1,0),
new Vector3(0,1,1),
// face 5 (zx plane, y=1)
new Vector3(0,1,0),
new Vector3(1,1,0),
new Vector3(1,1,1),
new Vector3(0,1,1),
// face 6 (zx plane, y=0)
new Vector3(0,0,0),
new Vector3(0,0,1),
new Vector3(1,0,1),
new Vector3(1,0,0),
};
int faces = 6; // here a face = 2 triangles
List<int> triangles = new List<int>();
List<Vector2> uvs = new List<Vector2>();
for (int i = 0; i < faces; i++) {
int triangleOffset = i*4;
triangles.Add(0+triangleOffset);
triangles.Add(2+triangleOffset);
triangles.Add(1+triangleOffset);
triangles.Add(0+triangleOffset);
triangles.Add(3+triangleOffset);
triangles.Add(2+triangleOffset);
// same uvs for all faces
uvs.Add(new Vector2(0,0));
uvs.Add(new Vector2(1,0));
uvs.Add(new Vector2(1,1));
uvs.Add(new Vector2(0,1));
}
mesh.triangles = triangles.ToArray();
mesh.uv = uvs.ToArray();
GetComponent<Renderer>().material = new Material(Shader.Find("Diffuse"));
mesh.RecalculateNormals();
mesh.RecalculateBounds ();
mesh.Optimize();
我想在运行时多次添加此对象。我在编辑器上单击右键创建预制件。我将gameobject拖到它上面。我创建了一个成员变量public Gameobject prefab
。我将预制件拖到检查器上的成员变量上。
我想在我的场景中添加2个立方体。我使用下面的代码,但它不适合我。它添加了许多对象。
我使用下面的代码克隆并添加对象
prefab = gameObject;
// for (int i = 0; i < 3; i++) {
gO = Instantiate (prefab, new Vector3 (0 * 1.8F - 8.2f, 0, 0), Quaternion.identity) as GameObject;
// }
如何从我自己的游戏对象创建预制件?如何在运行时将其正确添加到场景中? 编辑
如果我从编辑器创建统一形状,并将其拖动到预制件,我可以在C#代码上添加许多对象。但我不能为自己的立方体做同样的工作。
答案 0 :(得分:0)
所以最后你有2个脚本:
public class CubeFactory : MonoBehaviour
{
public GameObject PrefabToCreate;
// Use this for initialization
void Start () {
var cube = Instantiate(PrefabToCreate, new Vector3(0 * 1.8F - 8.2f, 0, 0), Quaternion.identity) as GameObject;
}
// Update is called once per frame
void Update () {
}
}
和
public class Cube : MonoBehaviour
{
private void Start()
{
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
Mesh mesh = new Mesh();
meshFilter.mesh = mesh;
mesh.vertices = new Vector3[]
{
// face 1 (xy plane, z=0)
new Vector3(0, 0, 0),
new Vector3(1, 0, 0),
new Vector3(1, 1, 0),
new Vector3(0, 1, 0),
// face 2 (zy plane, x=1)
new Vector3(1, 0, 0),
new Vector3(1, 0, 1),
new Vector3(1, 1, 1),
new Vector3(1, 1, 0),
// face 3 (xy plane, z=1)
new Vector3(1, 0, 1),
new Vector3(0, 0, 1),
new Vector3(0, 1, 1),
new Vector3(1, 1, 1),
// face 4 (zy plane, x=0)
new Vector3(0, 0, 1),
new Vector3(0, 0, 0),
new Vector3(0, 1, 0),
new Vector3(0, 1, 1),
// face 5 (zx plane, y=1)
new Vector3(0, 1, 0),
new Vector3(1, 1, 0),
new Vector3(1, 1, 1),
new Vector3(0, 1, 1),
// face 6 (zx plane, y=0)
new Vector3(0, 0, 0),
new Vector3(0, 0, 1),
new Vector3(1, 0, 1),
new Vector3(1, 0, 0),
};
int faces = 6; // here a face = 2 triangles
List<int> triangles = new List<int>();
List<Vector2> uvs = new List<Vector2>();
for (int i = 0; i < faces; i++)
{
int triangleOffset = i*4;
triangles.Add(0 + triangleOffset);
triangles.Add(2 + triangleOffset);
triangles.Add(1 + triangleOffset);
triangles.Add(0 + triangleOffset);
triangles.Add(3 + triangleOffset);
triangles.Add(2 + triangleOffset);
// same uvs for all faces
uvs.Add(new Vector2(0, 0));
uvs.Add(new Vector2(1, 0));
uvs.Add(new Vector2(1, 1));
uvs.Add(new Vector2(0, 1));
}
mesh.triangles = triangles.ToArray();
mesh.uv = uvs.ToArray();
GetComponent<Renderer>().material = new Material(Shader.Find("Diffuse"));
mesh.RecalculateNormals();
mesh.RecalculateBounds();
mesh.Optimize();
}
}
在你的场景中你有你的相机和一个带有CubeFactory的GameObject。
在你必须制作Prefab之前。它包含一个带有&#34; Cube.cs的游戏对象&#34;脚本就可以了。
将预制件添加为&#34; PrefabToCreate&#34;检查员到你的Cubefactory。