简体   繁体   English

如何同时生成单位?

[英]How can I generate the units at the same time?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateStairs : MonoBehaviour
{
    [Header("Stairs Prefb")]
    public GameObject stairsPrefab;
    [Space(5)]
    [Header("Platforms")]
    public bool addPlatforms = false;
    public GameObject platformsPrefab;
    [Space(5)]
    [Header("Settings")]
    [Range(1, 20)]
    public int numberOfUnits = 1;
    public float delay = 3;
    public int stairsNumber = 5;
    public Vector3 stairsStartPosition;
    public Vector3 stairSize;
    public Vector3 stairsSize;
    public float stepWidthFactor = 1f;

    private Vector3 stairsPosition;
    private GameObject stairsParent;
    private int oldNumberOfUnits = 0;

    // Use this for initialization
    void Start()
    {
        oldNumberOfUnits = numberOfUnits;

        for (int i = 0; i < numberOfUnits; i++)
        {
            stairsParent = new GameObject();
            stairsParent.name = "Stairs";
            StartCoroutine(BuildStairs());
        }
    }

    // Update is called once per frame
    void Update()
    {
        if(oldNumberOfUnits != numberOfUnits)
        {
            StartCoroutine(BuildStairs());
            oldNumberOfUnits = numberOfUnits;
        }
    }

    private IEnumerator BuildStairs()
    {
        for (int i = 1; i <= stairsNumber; i++)
        {

            stairsPosition = new Vector3(
                    stairsStartPosition.x,
                    stairsStartPosition.y + (i * stairsSize.y),
                    stairsStartPosition.z + (i * stairsSize.y) * stepWidthFactor);

            GameObject stair = Instantiate(
                    stairsPrefab,
                    stairsPosition,
                    Quaternion.identity);

            stair.tag = "Stair";
            stair.transform.parent = stairsParent.transform;
            stair.transform.localScale = stairSize;

            yield return new WaitForSeconds(delay);
        }

        stairsParent.AddComponent<MoveObjects>().Init();
    }
}

In the Start I'm doing a loop and start the Coroutine according to the numberOfunits. 在开始中,我正在执行一个循环,并根据numberOfunits启动协程。

It's working fine if numberOfUnits is 1. But is it's more then 1 for example 2 it's first creating the first set of stairs but then on the second "Stairs" parent it's creating only 1 stair. 如果numberOfUnits为1,则工作正常。但是,例如2大于1,它首先创建第一组楼梯,但是在第二个“ Stairs”父级上,它仅创建1个楼梯。 I don't want it to wait to finish the first Coroutine I want in the same time to create number of Coroutine's of stairs. 我不希望它等待完成我想要的第一个协程以创建多个协程的楼梯。

And I want also to add a gap between each stairs unit. 我还想在每个楼梯单元之间添加一个间隙。 And also to make that in the Update if I change the numberOfUnits it will add/destroy more stairs units. 并且如果要在Update中进行更改,如果我更改numberOfUnits,它将添加/销毁更多的楼梯单位。 All the stairs units should be Instantiate inside StartCoroutine. 所有楼梯单元都应在StartCoroutine中实例化。

You are mistaking how the coroutine works its not at thread. 您误会了协程的工作方式而不是线程。 What is happening is your continually invoking the coroutine so its starting over and over again not creating a separate instance. 发生的情况是您不断调用协程,因此它会一遍又一遍地创建而不创建单独的实例。

what you should do is create create a prefab and Instantiate that to do the work. 您应该做的是创建一个预制件,并实例化该预制件以完成工作。 My last remark was about threads but you wont be able to instantiate anything unless its on the main thread so the easiest way to get this done would be like so. 我的最后一句话是关于线程的,但是除非将其实例化在主线程上,否则您将无法实例化任何东西,因此完成此操作的最简单方法就是这样。

public GameObject yourGoWithAboveClassOnIt;


void Start()
{
    oldNumberOfUnits = numberOfUnits;

    for (int i = 0; i < numberOfUnits; i++)
    {
        Instantiate(yourGoWithAboveClassOnIt);
    }
}

your prior class will remove this 您之前的课程将删除此内容

void Start()
{
    //oldNumberOfUnits = numberOfUnits;

    //for (int i = 0; i < numberOfUnits; i++)
    //{
        stairsParent = new GameObject();
        stairsParent.name = "Stairs";
        StartCoroutine(BuildStairs());
    //}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM