表达式表示一个"值",其中在 Unity 中的线程内调用 StartCoroutine 时需要"类型"或"方法组"



我有这个类。

public class UploadData: MonoBehaviour{
    public void uploadindividualshot(pushdatawrapper pw){       
        StartCoroutine (PushData (pw));
        Thread t = new Thread(new ThreadStart(StartCoroutine(PushData(pw))));
        t.Start();
        t.IsBackground = true;
    }
    private IEnumerator PushData(pushdatawrapper pdata){
        WWW www;
        Hashtable postHeader = new Hashtable();
        postHeader.Add("Content-Type", "application/json");
        string dataToJason = JsonUtility.ToJson(pdata);
        Debug.Log ("dataToJason " + dataToJason);
        // convert json string to byte
        var formData = System.Text.Encoding.UTF8.GetBytes(dataToJason);
        www = new WWW("http://rmotion.rapsodo.com/api/push/new", formData, postHeader);
        return www;
    }
    IEnumerator WaitForRequest(WWW data)
    {
        yield return data; // Wait until the download is done
        if (data.error != null)
        {
            Debug.Log("There was an error sending request: " + data.text);
        }
        else
        {
            Debug.Log("WWW Request: " + data.text);
        }
    }
}

我在 Thread t = new Thread(new ThreadStart(StartCoroutine(PushData(pw))));遇到错误,因为表达式表示value', where a类型'或``方法group''。

如何解决该问题?

即使摆脱了错误,忘记了要做什么,即使您的错误另一个错误,您就会一旦您的coroutine开始就会丢弃,这是因为Unity是线程安全,您无法使用Unity特定的API在主线程之外(如果没有误认为,则向量除外(。您有2个选择。

  1. 仅使用无线程的Coroutine。
  2. 使用线程,但通过使用WebClient类避免统一API

我想你打算写

Thread t = new Thread(obj => { PushData(obj); });

PushData是方法组。PushData(pw)是实际的方法调用。您可以[应该]将数据传递给线程PROC,例如t.Start(pw);

旁注:应在t.Start()调用之前设置t.IsBackground = true;

最新更新