繁体   English   中英

Unity,协程无法启动

[英]Unity, Coroutine Won't Start

我有一台由我编写的简单有限 state 机器。 我有过渡和状态。 这是StateTransition从内部看的样子:

////////////////////////////////////////////////////////////////////////////

public delegate void ActionCaller(IEnumerator action); // Take note of this delegate

private readonly ActionCaller caller;
private readonly IEnumerator action; // This is my State action

////////////////////////////////////////////////////////////////////////////

public void Execute()
{
    caller.Invoke(action); // Here I call my `action` through `caller`
}

////////////////////////////////////////////////////////////////////////////

这是我的MonoBehaviour代码,它创建了这个状态:

////////////////////////////////////////////////////////////////////////////
private void Start()
{
    // Here I initialize my states and pass a method which will be called in `StateTransition.Execute` to execute passed method and `CallAction` to start Coroutine in my `MonoBehaviour` class
    States = new Dictionary<State, StateTransition>()
    {
        { State.Idling, new StateTransition(State.Idling, DoNothing(), CallAction) },
        { State.WanderingAround, new StateTransition(State.WanderingAround, WanderAround(), CallAction) }
    };

    StateMachine = new FiniteStateMachine(new List<Transition>()
    {
        new Transition(States[State.Idling], States[State.WanderingAround], Input.Wandering),
        new Transition(States[State.WanderingAround], States[State.Idling], Input.Nothing)
    });

    StateMachine.NextState(Input.Wandering);
}

////////////////////////////////////////////////////////////////////////////

private void CallAction(IEnumerator routine)
{
    if (currentActionCoroutine != null)
    {
        StopCoroutine(currentActionCoroutine);
    }

    currentActionCoroutine = StartCoroutine(routine); // This code right here starts my passed `routine`
}

////////////////////////////////////////////////////////////////////////////

这就是问题所在,当我调用我的第一个NextState时,它调用WanderAround然后调用DoNothing ,然后它应该再次调用WanderAround就好了,但问题是它根本不调用它。 没有发生任何错误。 协程只是不想启动。 但是,如果我打电话给DoNothing StartCoroutine(WanderAround()) - 它可以工作! 但不是通过我奇怪StateTransition/Action/Execute关系。

////////////////////////////////////////////////////////////////////////////

private IEnumerator DoNothing()
{
    Debug.Log("DoNothing:Enter");

    do
    {
        yield return new WaitForSeconds(2.5f);
    } while (CurrentState == State.Dead);

    Debug.Log("DoNothing:Exit");

    StateMachine.NextState(Input.Wandering); // Calls `StateTransition.Execute`
}

private IEnumerator WanderAround()
{
    Debug.Log("WanderAround:Enter");

    Vector2 randomPosition = new Vector2(Random.Range(-5f, 5f), Random.Range(-5f, 5f));

    movementController.RotateAndMoveTo(randomPosition, Genes[Gene.MovementSpeed], Genes[Gene.TurnSpeed]);

    while (movementController.IsMoving || movementController.IsRotating)
    {
        yield return new WaitForSeconds(Time.deltaTime);
    }

    Debug.Log("WanderAround:Exit");

    StateMachine.NextState(Input.Nothing); // Calls `StateTransition.Execute`
}

////////////////////////////////////////////////////////////////////////////

我应该怎么办? 谢谢!

PS真的很抱歉,如果这对你没有任何意义,我只是不知道如何更清楚地解释它。

好的,我想通了。

我将我的action类型从IEnumerator更改为System.Func<IEnumerator>并且......它起作用了!

现在我的代码如下所示:

////////////////////////////////////////////////////////////////////////////

public delegate void ActionCaller(IEnumerator action); // Take note of this delegate

private readonly ActionCaller caller;
private readonly System.Func<IEnumerator> action; // This is my State action

////////////////////////////////////////////////////////////////////////////

public void Execute()
{
    caller(action()); // Here I call my `action` through `caller`
}

////////////////////////////////////////////////////////////////////////////

StateTransition构造函数中,我通过SomeFunc而不是SomeFunc()

干杯!

暂无
暂无

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

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