c# - 表达式表示 `value' ,其中在 Unity 的线程内调用 StartCoroutine 时预期为 `type' 或 `method group'

标签 c# unity3d

我有这门课。

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)))); 处出错,因为 Expression 表示一个 value',其中一个类型' 或“方法组”是预期的。

如何解决这个问题?

最佳答案

忘记你想做什么,即使你摆脱了你的错误,你会在协程启动后立即抛出另一个错误,这是因为 Unity 是线程安全的,你不能在主线程之外使用 Unity 特定的 API(矢量除外如果没记错的话)。您有 2 个选择。

  1. 只使用一个没有线程的协程。
  2. 使用线程但避免使用 Unity API,例如使用 WebClient 类

关于c# - 表达式表示 `value' ,其中在 Unity 的线程内调用 StartCoroutine 时预期为 `type' 或 `method group',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50072951/

相关文章:

C# 用组合框替换 DataGridView 中的默认文本框

c# - 动态 Linq where 子句抛出 OutOfMemoryException

c# - Linq 到 SQL : How to get values added thru InsertOnSubmit but before SubmitChanges?

image - 保留纵横比,但裁剪中心

ios - Unity Google Play 游戏插件无法在 Xcode iOS 中编译

c# - 如何在 Unity 中显示类似于 android 中的 Toast 消息?

c# - .NET Core 中实现了什么内存模型?

c# - 将使用 Linq 的代码转换为不使用 Linq 的代码

android - 统一。安卓。更新应用程序后保存文件消失

c# - 统一编写txt文件时,它说在路径上共享冲突