简体   繁体   English

Unity,协程无法启动

[英]Unity, Coroutine Won't Start

I have a simple finite state machine written by me.我有一台由我编写的简单有限 state 机器。 I have transitions and states.我有过渡和状态。 Here's what StateTransition looks like from inside:这是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`
}

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

And here's my MonoBehaviour code which creates this states:这是我的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`
}

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

And here's the problem, when I call my first NextState it calls WanderAround which then calls DoNothing and after that it should call WanderAround again just fine, but the problem is that it simply doesn't call it.这就是问题所在,当我调用我的第一个NextState时,它调用WanderAround然后调用DoNothing ,然后它应该再次调用WanderAround就好了,但问题是它根本不调用它。 There's not a single error happening.没有发生任何错误。 The coroutine just doesn't wants to start.协程只是不想启动。 But if I call in, let's say, DoNothing StartCoroutine(WanderAround()) - it works!但是,如果我打电话给DoNothing StartCoroutine(WanderAround()) - 它可以工作! But not throught my weird StateTransition/Action/Execute relationship.但不是通过我奇怪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`
}

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

What should I do?我应该怎么办? Thanks!谢谢!

PS Really sorry if this doesn't make any sense to you, I just don't know how to explain it more clearer. PS真的很抱歉,如果这对你没有任何意义,我只是不知道如何更清楚地解释它。

Ok, I figured it out.好的,我想通了。

I changed my action type from IEnumerator to System.Func<IEnumerator> and... it worked!我将我的action类型从IEnumerator更改为System.Func<IEnumerator>并且......它起作用了!

Now my code looks like this:现在我的代码如下所示:

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

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`
}

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

And in StateTransition constructor I pass SomeFunc instead of SomeFunc() .StateTransition构造函数中,我通过SomeFunc而不是SomeFunc()

Cheers!干杯!

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

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