【问题标题】:How to add to scene own gameobject on unity如何在统一上添加到场景自己的游戏对象
【发布时间】:2015-06-03 09:13:52
【问题描述】:

我创建了自己的立方体。我使用下面的代码来做到这一点

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# 代码上添加许多对象。但我不能为自己的立方体做同样的工作。

【问题讨论】:

  • 看起来你做错了什么实例化你的预制件。您的预制件上是否附有带有实例化代码的脚本? - 所以在实例化预制件后,它会递归地实例化下一个?
  • 不,我只有一个 .cs 文件。它附加到游戏对象。我只实例化一次,但它递归实例化。
  • 如果我理解您的设置,我不放心。 - 你的预制件上应该有一个 CS 文件“BuildCubeMesh.cs”。使用上例中的代码。然后你有一个脚本“CubeBuilder.cs”,在 Start-method 中使用下面的代码和对你的 prefab 的引用。
  • 我撤消了我的代码和场景。我有一个test.cs 文件,它绘制了一个立方体上层代码。我的场景有一个主摄像机和游戏对象。这个cs文件附加到gameObject。我现在该怎么办? (还没有任何预制对象)
  • 好的。 - 1.将gameObject拉入“Project”-Window,它会自动转换为prefab。现在从场景中删除游戏对象。 2. 制作一个新的空游戏对象并添加一个新脚本“TestFactory.cs”并将您帖子的底部代码添加到 Start Method。 3. 在检查器中添加您之前创建的 Prefab => Start the Scene

标签: unity3d gameobject


【解决方案1】:

所以最后你有 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 之前。 Wich 包含一个带有“Cube.cs”脚本的游戏对象。

将预制件作为“PrefabToCreate”添加到检查器中的 Cubefactory。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多