多次克隆实例化对象Unity3D

时间:2019-05-13 01:56:25

标签: c# unity3d

我有一个预制件列表,下面的脚本在Y轴上随机实例化了其中的10个。

实例化的预制件是随机且不同的,但我希望它们是相同的(一个预制件的克隆),我该怎么做?

脚本:

    public GameObject[] Bricks;

void Start () {

        SpawnCubes();
    }

void SpawnBricks(int numCubes = 10, float startY = 3, float delta = 1)
    {
        for (int i = 0; i < numCubes; ++i)
        {
            int Rand = Random.Range(0, Bricks.Length);
            var Brick = Instantiate(Bricks[Rand], new Vector3(0, startY - (float)i * delta, 0), Quaternion.identity);
            Brick.transform.parent = gameObject.transform;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

void SpawnBricks(int numCubes = 10, float startY = 3, float delta = 1)
{
    int Rand = Random.Range(0, Bricks.Length);
    for (int i = 0; i < numCubes; ++i)
    {
        var Brick = Instantiate(Bricks[Rand], new Vector3(0, startY - (float)i * delta, 0), Quaternion.identity);
        Brick.transform.parent = gameObject.transform;
    }
}

喜欢这个?