如何在统一中添加到场景自己的游戏对象
How to add to scene own gameobject on unity
我创建自己的立方体。我使用下面的代码来做到这一点
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();
我想在运行时多次添加这个对象。我通过在编辑器上单击右键来创建一个预制件。我将游戏对象拖到它上面。我创建了一个成员变量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# 代码上添加许多对象。但是我不能为我自己的多维数据集做同样的工作。
所以最后你有 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 的游戏对象。
之前你得做一个Prefab。其中包含一个带有 "Cube.cs" 脚本的游戏对象。
将预制件作为 "PrefabToCreate" 添加到检查器中的 Cubefactory。
我创建自己的立方体。我使用下面的代码来做到这一点
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();
我想在运行时多次添加这个对象。我通过在编辑器上单击右键来创建一个预制件。我将游戏对象拖到它上面。我创建了一个成员变量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# 代码上添加许多对象。但是我不能为我自己的多维数据集做同样的工作。
所以最后你有 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 的游戏对象。
之前你得做一个Prefab。其中包含一个带有 "Cube.cs" 脚本的游戏对象。
将预制件作为 "PrefabToCreate" 添加到检查器中的 Cubefactory。