【问题标题】:Synchronize main loop (Spawner.cs) with subloop on Prefab将主循环 (Spawner.cs) 与预制件上的子循环同步
【发布时间】:2023-02-09 04:51:19
【问题描述】:

我正在处理地板必须逐渐展开的 Unity3d 项目。我创建了一个脚本 FloorModule.cs,其中使用协程地砖逐渐铺设。每个下一个模块必须在上一个模块完成后立即展开。在那里我创建了 Spawner.cs 以在前一个完成后立即循环一个新的 FloorModule.cs。

我似乎无法理解如何使用协程将主循环 (Spawner.cs) 与预制件 (FloorModule.cs) 上的子循环同步。

这是示例的链接 https://1drv.ms/u/s!AkVZpIE6f1GV4M5Ju7G5zPOrQcCe8w?e=QrghRT

附言 在给定的示例中,随着循环的进行,我正在使用“Reference.cs”类来更改一些变量值。

地板模块.cs

    public class FloorModule : MonoBehaviour
    {
        public float zSpacer = 0f;

        public int instPrefabCount;
        public Transform spawnPoint;
        public int lenght = 15;
        public int width = 5;

        public GameObject floorTiles;

        void Start()
        {
            spawnPoint = GetComponent<Transform>();

            StartCoroutine(FwFloorDelay(spawnPoint));
        }

        public IEnumerator FwFloorDelay(Transform origin)
        {
            for (int l = 0; l < lenght; l++)
            {
                float xAngle = 90;
                float yPos = 0;
                float zPos = 0 + l;

                for (int w = 0; w < width; w++)
                {
                    int xSelection = Random.Range(0, 6);
                    GameObject xFloor = Instantiate(floorTiles, origin);

                    TileStatusNames(xFloor, l, w);

                    // defining positiona and angles
                    float xPos = w + (zSpacer * w);
                    xFloor.transform.localEulerAngles = new Vector3(xAngle, 0, 0);
                    xFloor.transform.localPosition = new Vector3(xPos, yPos, zPos);

                    yield return new WaitForSeconds(.05f);

                }
            }

Spawner.cs

    public class Spawner : MonoBehaviour
    {
        public GameObject FloorModPrefab;
        public References[] referenceScript;

        void Start()
        {
            StartCoroutine(SpawnModules());
        }

        IEnumerator SpawnModules()
        {
            for (int i = 0; i < referenceScript.Length; i++)
            {
                referenceScript[i].instance =
                    Instantiate(FloorModPrefab, referenceScript[i].ref_spawnPoint.position, referenceScript[i].ref_spawnPoint.rotation);

                referenceScript[i].ref_instFloorModCount = i + 1;
                referenceScript[i].Setup();
                yield return new WaitForSeconds(5f);
            }
        }
    }

参考资料.cs

    [Serializable]
    public class References
    {
        FloorModule prefabObjScript;
        public GameObject instance; 

        public int ref_instFloorModCount;
        public Transform ref_spawnPoint;
        public int ref_Width = 5;
        public int ref_Lenght = 15;

        public void Setup()
        {
            // Get references to the components.
            prefabObjScript = instance.GetComponent<FloorModule>();

            // Set the player numbers to be consistent across the scripts.
            prefabObjScript.instPrefabCount = ref_instFloorModCount;
            prefabObjScript.spawnPoint = ref_spawnPoint;
            prefabObjScript.width = ref_Width;
            prefabObjScript.lenght = ref_Lenght;
        }
    }

不幸的是,我在给定的上下文中尝试使用协程,我意识到我不可能解决这个任务。

【问题讨论】:

  • 听起来你需要某种形式的信号来表示完成。

标签: c# unity3d coroutine


【解决方案1】:

您的目标不应该是同步单独的协程,而是让代码在一个协程中按顺序运行。

例如,您可以使 References.Setup() 异步,然后让它直接调用 FloorModel.FwFloorDelay 而不是 FloorModel 启动它自己的独立协程。

【讨论】:

    【解决方案2】:

    您可以从另一个协程中产生一个协程。

    此方法需要将 instance 更改为 FloorModule 类型,或使 FloorModule 可从引用中获得。

    去掉FloorModuleStart的代码,因为我们要控制协程什么时候启动。

    将 FwFloorDelay 更改为

    public IEnumerator FwFloorDelay(Transform origin = null)
    {
        if (origin == null)
        {
            origin = transform;  // No reason to use GetComponent<Transform>()
        }
        ...
    }
    

    在 SpawnModules 中,链接 floor 协程

    IEnumerator SpawnModules()
    {
        for (int i = 0; i < referenceScript.Length; i++)
        {
            ...
            yield return floorInstance.FwFloorDelay();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-06
      • 2015-09-13
      • 1970-01-01
      • 2018-10-17
      相关资源
      最近更新 更多