Как иметь очередь методов - PullRequest
1 голос
/ 20 октября 2019

Предположим, я хочу вызвать некоторые методы автомобиля, например, drive(30, 5), rotate(45), stop().

. Как мне создать очередь методов, например, [drive(30,5), rotate(45), stop()], чтобыЯ могу выполнить первую функцию, дождаться ее завершения и вызвать следующую?

Все эти методы являются сопрограммами (IEnumerators).

Ответы [ 3 ]

4 голосов
/ 20 октября 2019
var ms = new List<Action>()
{
    () => drive(30, 5),
    () => rotate(45),
    () => stop()
};


for (int i = 0; i < ms.Count; i++)
{
    ms[i](); // Invoke
}
1 голос
/ 20 октября 2019

Да, используя C # Actions и Co-рутины, вы можете! Ниже приведен псевдокод, но его можно уточнить! Представь!

Using System;
public class SomeClass : MonoBehaviour
{
    myDefaultWaitTime = SomeFloat;
    myQueue = new List<Action>();



    void AddAction(Action myNewAction)
    {
        myQueue.Add(myNewAction);
    }
    Action myNextAction()
    {
        Action myAction = myQueue[0];
        myQueue.delete(0);
        return myAction;
    }

    IEnumerator WaitToLoad(Action myAction)
        {
            float currentWaitTime = 0;
            while (currentWaitTime < defaultWaitTime)
            {
                currentWaitTime += Time.deltaTime;
                yield return new WaitForEndOfFrame();
            }
            StartCoroutine(myNextAction());
        }
}
0 голосов
/ 20 октября 2019
public IEnumerator Drive(int x, int y)
{
    // driving routine here.
}

public IEnumerator Rotate(float angle)
{
    // rotation routine here.
}

public IEnumerator Stop()
{
   // Stop routine here
}

public IEnumerator ExecuteAll()
{
    // Drive
    yield return new StartCoroutine(Drive(30,5));

    // rotate
    yield return new StartCoroutine(Rotate(45));

    // Stop
    yield return new StartCoroutine(Stop());


    // All actions are done.
}


public void StartAll()
{
   StartCoroutine(ExecuteAll());
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...