如何在不同的时间仅调用一次不同的方法?

时间:2019-07-10 18:55:33

标签: c# unity3d events action coroutine

我正在尝试使用操作和事件,枚举器和协程,但是我变得如此困惑。

目标:使MoveCubes()运行一次3秒钟,然后使LerpSine()运行3秒钟。

CountdownTimer.cs正在跟踪秒。然后SkinnedMeshSpawn.cs与之交互。

倒数计时器:

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

public class CountdownTimer : MonoBehaviour
{
    float currentTime = 0f;
    float startingTime = 10f;
    public static event Action RaiseReady;
    public static event Action RaiseReady2;
    public SkinnedMeshSpawn SkinnedMeshSpawn;

    void Start()
    {
        currentTime = startingTime;     
        StartCoroutine(UpdateCoroutine());
    }

    IEnumerator UpdateCoroutine()
    {
        while (true)
        {
            SkinnedMeshSpawn.GetComponent<SkinnedMeshSpawn>().MoveCubes();
            currentTime -= 1 * Time.deltaTime; //does it each frame
            int n = Convert.ToInt32(currentTime);

            if (n == 7)
            {
                RaiseReady?.Invoke();
                RaiseReady = null; // clean the event
                yield break; // Kills the coroutine
            }
            yield return new WaitForFixedUpdate();
        }
    }

    IEnumerator UpdateCoroutine2()
    {
        while (true)
        {
            SkinnedMeshSpawn.GetComponent<SkinnedMeshSpawn>().LerpSine();
            currentTime -= 1 * Time.deltaTime; //does it each frame
            int n = Convert.ToInt32(currentTime);

            if (n == 4)
            {
                RaiseReady2?.Invoke();
                RaiseReady2 = null; // clean the event
                yield break; // Kills the coroutine

            }
            yield return new WaitForFixedUpdate();
        }
    }
}

SkinnedMeshSpawn:

using UnityEngine;

[RequireComponent(typeof(SkinnedMeshRenderer))]
public class SkinnedMeshSpawn : MonoBehaviour
{
    public GameObject CubePrefab;
    public GameObject[] objects;
    SkinnedMesh mesh;
    public Rigidbody cubePrefabRb;

    public Vector3[] verts;

    int runOnce = 1;

    void Awake()
    {
        mesh = GetComponent<SkinnedMesh>();
        verts = new Vector3[mesh.vertexCount];
    }
    void Start()
    {
        verts = mesh.vertices;

        CountdownTimer.RaiseReady += CountdownTimer_RaiseReady;
        CountdownTimer.RaiseReady2 += CountdownTimer_RaiseReady2;

        mesh.OnResultsReady += DrawVertices;
    }
    void CountdownTimer_RaiseReady()
    {
        Debug.Log("Done");
        CountdownTimer.RaiseReady -= CountdownTimer_RaiseReady; // Remove listener though the other class is already clearing it
    }

    private void CountdownTimer_RaiseReady2()
    {
        Debug.Log("Done2");
        CountdownTimer.RaiseReady2 -= CountdownTimer_RaiseReady; // Remove listener though the other class is already clearing it
    }

    void DrawVertices(SkinnedMesh mesh)
    {
        if (runOnce == 1)
        {
            for (int i = 0; i < mesh.vertexCount; i++)
            {
                Vector3 position = verts[i];
                var cubeClone = Instantiate(CubePrefab, position, transform.rotation);
                cubeClone.tag = "CubePFInst";
            }
            runOnce = 0;
        }
    }


    public void MoveCubes()
    {
        if (runOnce == 0)
        {
            objects = GameObject.FindGameObjectsWithTag("CubePFInst");

            for (int i = 0; i < mesh.vertexCount; i++)
            {
                Vector3 position = verts[i];
                cubePrefabRb = objects[i].GetComponent<Rigidbody>();
                cubePrefabRb.MovePosition(Vector3.Lerp(position, position, Time.deltaTime * 6));
            }
        }
    }

    public void LerpSine()
    {
        Debug.Log("LerpSine");
    }
}

到目前为止,我最能完成的工作就是调用UpdateCoroutine2并将Done2打印到Log,但是LerpSine重复打印,就像我在每帧更新时启动协程。

如果有人可以看一下代码并就如何实现上述目标提出建议,我将不胜感激。后来我想实现GameManager,但这已经变得非常复杂。

1 个答案:

答案 0 :(得分:1)

在这里使用事件实际上会使事情复杂化了。

我会用yield return new WaitForSeconds(seconds);来计时:

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

public class CountdownTimer : MonoBehaviour
{

    public SkinnedMeshSpawn SkinnedMeshSpawn;

    void Start()
    { 
        StartCoroutine(FunctionCaller(3f,3f));
    }

    IEnumerator FunctionCaller(float pause1, float pause2)
    {
        SkinnedMeshSpawn.GetComponent<SkinnedMeshSpawn>().MoveCubes();

        yield return new WaitForSeconds(pause1);

        SkinnedMeshSpawn.GetComponent<SkinnedMeshSpawn>().LerpSine();

        yield return new WaitForSeconds(pause2);

        // Do whatever after pause 2
    }
}