在Unity3d中使用RESTful Web服务,无法从'System.Collections.IEnumerable'转换为'string'…如何转换或解决?

问题描述 投票:0回答:1

我正在尝试使用Unity Engine Networking API在Unity3d中使用RESTful Web服务。跟随此视频https://youtu.be/TrOLTrIX9Yk?t=289但是我得到“参数1:无法从'System.Collections.IEnumerable'转换为'string'”https://i.imgur.com/Q0ltU6X.png

[在我迷路的视频https://www.youtube.com/watch?v=TrOLTrIX9Yk中,在此视频https://youtu.be/TrOLTrIX9Yk?t=289之后,使用我自己的Rest Api尝试“在Unity3d中使用RESTful Web服务并学习如何使用Unity Engine Networking API”]

试图查看是否是我的Unity版本,所以我下载了2018.1.1f1(Dilmer在他的视频中使用了该错误)。>

//RestClient.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class RestClient : MonoBehaviour
{
    private static RestClient _instance;

    public static RestClient Instance 
    {
        get
        {
            if (_instance == null)
            {
                _instance = FindObjectOfType<RestClient>();
                if (_instance == null)
                {
                    GameObject go = new GameObject();
                    go.name = typeof(RestClient).Name;
                    _instance = go.AddComponent<RestClient>();
                    DontDestroyOnLoad(go);
                }
            }
            return _instance;
        }
    }

    public IEnumerable Get(string url)
    {
        using (UnityWebRequest www = UnityWebRequest.Get(url))
        {

            yield return www.SendWebRequest();

            if (www.isNetworkError)
            {
                Debug.Log(www.error);
            }
            else
            {
                if (www.isDone)
                {
                    string jsonResult = System.Text.Encoding.UTF8.GetString(www.downloadHandler.data);
                    Debug.Log(jsonResult);
                }
            }

        }
    }


}

//Game_URL.cs

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Game_URL : MonoBehaviour
{
    public string WEB_URL = "";

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(RestClient.Instance.Get(WEB_URL));
    }

}

将我的MeshData的控制台输出模拟到以下的预期实际结果:

https://i.imgur.com/5RcDt2H.pnghttps://i.imgur.com/FheOSWL.png

我正在尝试使用Unity Engine Networking API在Unity3d中使用RESTful Web服务。跟随此视频https://youtu.be/TrOLTrIX9Yk?t=289但我得到“参数1:无法从'...

c# unity3d asp.net-core asp.net-web-api
1个回答
1
投票

您得到的错误具有误导性,因为您的IDE假设您正在尝试调用另一个StartCoroutine重载,该重载将方法名(字符串)作为第一个参数。

您的实际问题是StartCoroutine需要IEnumerator,而不是IEnumerable。将Get方法的返回类型更改为IEnumerator(如视频中所示),它应该可以工作。

© www.soinside.com 2019 - 2024. All rights reserved.