从Unity中的另一个脚本调用IEnumerator方法并获取其返回值 [英] Call a IEnumerator method from another script in Unity and get its return value

查看:1955
本文介绍了从Unity中的另一个脚本调用IEnumerator方法并获取其返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Unity中进行一个项目.我有这个文件:

I'm working on a project in Unity. I have this file:

API.cs (未附加到任何GameObject)

API.cs (no attached to any GameObject)

using UnityEngine;
using UnityEngine.Networking;
using System.Collections;
using LitJson;

public class API : MonoBehaviour
{

    public IEnumerator Login(string email, string psw)
    {
        string URL = "https://####.azurewebsites.net/api/login";
        WWWForm form = new WWWForm();
        form.AddField("email", email);
        form.AddField("password", psw);

        var download = UnityWebRequest.Post(URL, form);

        // Wait until the download is done
        yield return download.SendWebRequest();

        if (download.isNetworkError || download.isHttpError)
        {
            print("Error downloading: " + download.error);
        }
        else
        {
            JsonData data = JsonMapper.ToObject(download.downloadHandler.text);
            string token = (string)data["success"]["token"];
            Debug.Log(token);
        }
    }
}

caller.cs (附加到当前场景中的GameObject)

caller.cs (attached to a GameObject in the current scene)

    private void Start ()
    {
        // Something like this
        var token = API.Login("foo@boo.com", "####");
    }

我的问题是:如何在caller.cs中调用函数登录"并检索令牌的值?

我尝试过但没有起作用的内容:

  • 为脚本public API test;创建一个变量,然后为test.Login("foo@boo.com", "####")
  • 使API类静态化,在caller.cs中初始化该类,然后调用方法Login(...)
  • Create a variable for the script public API test; and then test.Login("foo@boo.com", "####")
  • Make the API class static, inizialize the class in caller.cs, and then call the method Login(...)

能够解决从另一个脚本调用方法的问题,问题是我必须创建一个对象并将其附加到API.cs.然后,我还必须将该对象拖放到检查器中的caller.cs的公共字段中.此外,我还必须在StartCoroutine()里面添加caller.cs中的Login方法.

Was able to solve the problem of calling the method from another script, the problem was that I had to create an object and attach to it API.cs. Then I also had to drag and drop that object in the public field of caller.cs in the inspector. Moreover, I had to add in caller.cs the Login method inside the StartCoroutine() one.

仍然,现在必须弄清楚如何从Login()方法中检索令牌的值.

Still, have to figure out now how to retrieve the value of the token from the Login() method.

推荐答案

能够解决从另一个调用方法的问题 脚本,问题是我必须创建一个对象并附加到 它是API.cs.然后我还不得不在公共场所拖放该对象 检查器中的caller.cs字段.

Was able to solve the problem of calling the method from another script, the problem was that I had to create an object and attach to it API.cs. Then I also had to drag and drop that object in the public field of caller.cs in the inspector.

在编辑器中手动拖放脚本听起来并不正确,因为您的目标是使用from脚本. API脚本是MonoBehaviour,因为它是从中衍生而来的.使用AddComponent添加API类,然后在登录功能上调用StartCoroutine.

That doesn't sound right dragging and dropping the script manually in the Editor since your goal is to do with from script. The API script is a MonoBehaviour because it derives from it. Use AddComponent to add the API class then call StartCoroutine on the Login function.

API api = gameObject.AddComponent<API>();
StartCoroutine(api.Login("foo@boo.com", "####"));

删除MonoBehaviour,这样您就不必将API脚本附加到GameObject上,而是使用new关键字创建new对象.

Remove the MonoBehaviour so that you don't have to attach the API script to a GameObject instead create new objects with the new keyword.

public class API 
{

    public IEnumerator Login(string email, string psw)
    {
        ....
    }
}

否,您可以简单地做到:

Noe, you can simply do:

API api = new API();
StartCoroutine(api.Login("foo@boo.com", "####"));

最后,要返回值,请添加Action作为第三个参数.

Finally, to return the value, add Action as third param.

public class API
{

    public IEnumerator Login(string email, string psw, Action<string> token)
    {
        string URL = "https://####.azurewebsites.net/api/login";
        WWWForm form = new WWWForm();
        form.AddField("email", email);
        form.AddField("password", psw);

        var download = UnityWebRequest.Post(URL, form);

        // Wait until the download is done
        yield return download.SendWebRequest();

        if (download.isNetworkError || download.isHttpError)
        {
            Debug.Log("Error downloading: " + download.error);
        }
        else
        {
            JsonData data = JsonMapper.ToObject(download.downloadHandler.text);
            string tokenResult = (string)data["success"]["token"];
            Debug.Log(tokenResult);
            if (token != null)
                token(tokenResult);
        }
    }
}

要从非协程函数调用它,请执行以下操作:

To call it from your non coroutine function:

API api = new API();
StartCoroutine(api.Login("foo@boo.com", "####", (token) =>
{
    Debug.Log("Token: " + token);
}
));

这篇关于从Unity中的另一个脚本调用IEnumerator方法并获取其返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆