C# 如何使协同程序有序运行?

C# 如何使协同程序有序运行?,c#,unity3d,C#,Unity3d,这是使用Unity3D。我有三个协同程序:GetJSONFromSelectedSubreddit(),loadmoremes(),以及一个单独脚本中的函数,该函数需要能够通过GetNewMemes()函数访问Meme数组(必须返回Meme[])LoadNewMemes()products。问题是,loadmoremes()需要json才能工作,所以它们必须按照前面提到的顺序运行。如果您需要这些功能,请参见: public void GetNewMemes(string subReddit, i

这是使用Unity3D。我有三个协同程序:
GetJSONFromSelectedSubreddit()
loadmoremes()
,以及一个单独脚本中的函数,该函数需要能够通过
GetNewMemes()
函数访问Meme数组(必须返回Meme[])
LoadNewMemes()
products。问题是,
loadmoremes()
需要json才能工作,所以它们必须按照前面提到的顺序运行。如果您需要这些功能,请参见:

public void GetNewMemes(string subReddit, int count)
{
    SetSelectedSubreddit(subReddit);
    memesAtATime = count;
    subJSON = null;
    StartCoroutine(GetJSONFromSelectedSubreddit());
    StartCoroutine(LoadMoreMemes());
}

IEnumerator GetJSONFromSelectedSubreddit()
{
    gettingJSON = true;
    WWW requester = new WWW("https://www.reddit.com/r/" + selectedSub + "/new.json?sort=new&count=25&after=" + postIndex);
    yield return requester;
    subJSON = requester.text;
    json = new JSONObject(subJSON);
    gettingJSON = false;
}

IEnumerator LoadMoreMemes()
{
    while (gettingJSON)
        yield return new WaitForSeconds(0.1f);
    for (int i = 0; i < memesAtATime; i++)
    {
        yield return StartCoroutine(GetUserPostKarma(json["data"]["children"][i]["data"]["author"].str));

        string sourceURL = json["data"]["children"][i]["data"]["preview"]["images"][0]["source"]["url"].str;
        sourceURL = sourceURL.Replace("&amp;", "&");

        yield return StartCoroutine(GrabImage(sourceURL));

        Meme currentMeme = new Meme(
                json["data"]["children"][i]["data"]["preview"]["images"][0]["source"]["url"].str,
                authorPostKarma,
                (int) json["data"]["children"][i]["data"]["score"].i,
                json["data"]["children"][i]["data"]["permalink"].str,
                json["data"]["children"][i]["data"]["title"].str,
                currentBitmap
        );
        Debug.Log(currentMeme.cost);
        memes[i] = currentMeme;
    }
}
public void GetNewMemes(字符串subReddit,int count)
{
设置选定的subReddit(subReddit);
Memestatime=计数;
subson=null;
启动例程(GetJSONFromSelectedSubreddit());
start例程(loadmoremes());
}
IEnumerator GetJSONFromSelectedSubreddit()
{
gettingJSON=true;
WWW请求者=新的WWW(“https://www.reddit.com/r/“+selectedSub+”/new.json?sort=new&count=25&after=“+postIndex”);
退回申请者;
subson=requester.text;
json=新的JSONObject(subjectson);
gettingJSON=false;
}
IEnumerator LoadMoreMemes()
{
while(gettingJSON)
收益率返回新WaitForSeconds(0.1f);
for(int i=0;i
下面是另一个脚本:

void Start ()
{
    RedditCommunicator redditCommunicator = GetComponent<RedditCommunicator>();
    redditCommunicator.GetNewMemes("me_irl", 1);
    Meme[] memes = redditCommunicator.GetCurrentMemes();
    Debug.Log(memes[0].currentScore);
    redditCommunicator.SpawnOneMeme(memes[0]);
}
void Start()
{
RedditCommunicator RedditCommunicator=GetComponent();
redditCommunicator.GetNewMemes(“me_irl”,1);
Meme[]memes=redditCommunicator.GetCurrentMemes();
Debug.Log(memes[0].currentScore);
redditCommunicator.spawnoneme(memes[0]);
}

每个函数本身都可以很好地工作,但它们需要等待彼此完成,并以正确的顺序运行才能工作。我希望函数保持独立,以便将来可以单独调用它们
memes
是一个私有变量,我想传递给调用这些函数的另一个脚本。如果你不认为我已经尝试了我的选择谷歌搜索和解决这个问题,相信我,我已经尽了最大的努力。提前谢谢你的帮助。如果你需要更多的信息,就问我。此代码的当前状态是,它在协同程序可以完成之前将memes返回到早期,从而导致空memes。

问题是
GetJSONFromSelectedSubreddit
LoadNewMemes
方法在
GetNewMemes
方法中被称为两个“并行”协同程序

如果不需要“异步”运行协同路由,则可以通过它枚举:

public void GetNewMemes(string subReddit, int count)
{
    SetSelectedSubreddit(subReddit);
    memesAtATime = count;
    subJSON = null;

    var enumerator = GetJSONFromSelectedSubreddit();
    while (enumerator.MoveNext());

    enumerator = LoadNewMemes();
    while (enumerator.MoveNext());
}

您可以在IEnumerator中生成一个协程,它将停止该协程的进程,直到该协程完成。像这样:

void Start()
{
    StartCoroutine(DoThings((text) => {
        Debug.Log("Dothings told me: " + text);
    }));
}

IEnumerator DoThings(Action<string>() callback)
{
    yield return StartCoroutine(DoThisFirst());
    callback("Returning a value mid-method!");
    yield return StartCoroutine(ThenThis());
    Debug.Log(3);
}

IEnumerator DoThisFirst()
{
    yield return new WaitForSeconds(1);
    Debug.Log(1);
}

IEnumerator ThenThis()
{
    yield return new WaitForSeconds(1);
    Debug.Log(2);
}
void Start()
{
开始例行程序(点符号((文本)=>{
Log(“Dothings告诉我:“+text”);
}));
}
IEnumerator DoThings(Action()回调)
{
收益返回开始例程(DoThisFirst());
回调(“返回值mid方法!”);
产生返回开始例程(然后是this());
调试日志(3);
}
IEnumerator DoThisFirst()
{
返回新的WaitForSeconds(1);
调试日志(1);
}
IEnumerator this()
{
返回新的WaitForSeconds(1);
调试日志(2);
}

Coroutines不是异步的。我调用的函数将在完成dothing之前返回一个值。我必须让它返回void,让枚举器设置一个公共变量,然后从另一个脚本获取它。我不想这样做,但如果这是我必须做的…我不完全确定我是否理解,但我会更新帖子,向您展示如何实现回调。