This question already has answers here:
Unity - need to return value only after coroutine finishes
                                
                                    (2个答案)
                                
                        
                                2年前关闭。
            
                    
在我的Unity游戏中,您可以在地形表面放置各种对象,放置新对象时,我需要将时间戳保存在文件中。我的保存系统正在运行,并且在一些帮助下,我能够从服务器获取时间戳。看来您必须使用IEnumerator才能从服务器获取数据,但这给我带来了一个新问题。由于IEnumerator不是函数,因此不能简单地返回任何内容。而且由于它无法返回任何内容,因此我需要一种变通方法来将时间戳从IEnumerator传递回要求的位置。我如何计划就能获得时间戳记:

 int _timestamp = ServerTime.Get();


所以我可以像这样轻松存储它

gameObject.GetComponent<_DrillStats>().timestamp = _timestamp;
gameObject.GetComponent<_OtherStats>().timestamp = _timestamp;
gameObject.GetComponent<_EvenMoreStats>().timestamp = _timestamp;


这是接收时间戳的完整代码:

using UnityEngine;
using System;
using System.Collections;

public class ServerTime : MonoBehaviour
{
    private static ServerTime localInstance;
    public static ServerTime time { get { return localInstance; } }

    private void Awake()
    {
        if (localInstance != null && localInstance != this)
        {
            Destroy(this.gameObject);
        }
        else
        {
            localInstance = this;
        }
    }

    public static void Get()
    {
        if (time == null)
        {
            Debug.Log("Script not attached to anything");
            GameObject obj = new GameObject("TimeHolder");
            localInstance = obj.AddComponent<ServerTime>();
            Debug.Log("Automatically Attached Script to a GameObject");
        }

        time.StartCoroutine(time.ServerRequest());
    }

    IEnumerator ServerRequest()
    {
        WWW www = new WWW("http://www.businesssecret.com/something/servertime.php");

        yield return www;

        if (www.error == null)
        {
            int _timestamp = int.Parse (www.text);

            // somehow return _timestamp
            Debug.Log (_timestamp);
        }
        else
        {
            Debug.LogError ("Oops, something went wrong while trying to receive data from the server, exiting with the following ERROR: " + www.error);
        }

    }

}

最佳答案

这就是我在评论部分中的意思:

public static void Get(Action<int> callback)
{
    if (time == null)
    {
        Debug.Log("Script not attached to anything");
        GameObject obj = new GameObject("TimeHolder");
        localInstance = obj.AddComponent<ServerTime>();
        Debug.Log("Automatically Attached Script to a GameObject");
    }

    time.StartCoroutine(time.ServerRequest(callback));
}

IEnumerator ServerRequest(Action<int> callback)
{
    WWW www = new WWW("http://www.businesssecret.com/something/servertime.php");

    yield return www;

    if (www.error == null)
    {
        int _timestamp = int.Parse(www.text);

        // somehow return _timestamp
        callback(_timestamp);

        Debug.Log(_timestamp);
    }
    else
    {
        Debug.LogError("Oops, something went wrong while trying to receive data from the server, exiting with the following ERROR: " + www.error);
    }
}


我使两个函数都使用Action<int>作为参数。

美国高尔夫协会:

ServerTime.Get();替换为:

ServerTime.Get((myTimeStamp) =>
{
    Debug.Log("Time Stamp is: " + myTimeStamp);

    gameObject.GetComponent<_DrillStats>().timestamp = myTimeStamp;
    gameObject.GetComponent<_OtherStats>().timestamp = myTimeStamp;
    gameObject.GetComponent<_EvenMoreStats>().timestamp = myTimeStamp;
});

关于c# - 使IEnumerator以某种方式返回值的解决方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41518924/

10-11 13:17