Search code examples
c#unity-game-enginecoroutineinput-fieldunitywebrequest

How to update Url used by UnityWebRequest to InputField?


I want the user to be able to change the ID of the link, which is why I used InputField to get their input. Unfortunately, I can't seem to figure out, how to make it work smoothly with a Coroutine. When I try to update the link, it just ends in an endless loop like with the implementation below. When I don't update the link the Coroutine seems to start without getting the input and gets stuck. I would really appreciate any help.

public class CSVDownloader : MonoBehaviour
{

    public static string googleSheetDocID;
    public static string url;
    public InputField InputField;
    public static string csvDownloaded;
    
    public static CSVParametersData downloadedData;

    public void Start()
    {
        googleSheetDocID = InputField.text;
        Debug.Log(googleSheetDocID);
        StartCoroutine(DownloadData());
        
    }

    public void Update()
    {
        googleSheetDocID = InputField.text;
        Debug.Log(googleSheetDocID);
    }

    public static IEnumerator DownloadData()
    {
        Debug.Log(("In Coroutine"));
        //url = "https://docs.google.com/spreadsheets/d/1ufpl1MsxXU2bk0Kt5YT_BkM1uI2WMLxr/export? format=csv";
        url = "https://docs.google.com/spreadsheets/d/" + googleSheetDocID + "/export?format=csv";
        Debug.Log(url);

        UnityWebRequest webRequest = UnityWebRequest.Get(url);
        yield return new WaitForEndOfFrame();
        Debug.Log("Starting Download...");
        yield return webRequest.SendWebRequest();
        Debug.Log("Webrequestsend");
    }
}

Solution

  • How about doing it in a few more steps?

    In the code presented above, the user interaction ends with keyboard input. So trying to do everything in real time is a problem.

    If the only user interaction is the keyboard, it is complex and difficult to achieve the desired task.

    If it were me, I would improve the code to make it easier to achieve what you want by proceeding like this:

    Add that script to your Button GameObject

    using System.Collections;
    using System.IO;
    using UnityEngine;
    using UnityEngine.Networking;
    using UnityEngine.UI;
    
    public class CsvDownloader : MonoBehaviour
    {
      [SerializeField] private InputField inputField;
      
      private Button changeUrlButotn;
      private string googleSheetDocID;
      private string url;
    
      private void Awake()
      {
        changeUrlButotn = GetComponent<Button>();
        changeUrlButotn.onClick.AddListener(OnChangeUrl);
      }
    
      private void OnChangeUrl()
      {
        googleSheetDocID = inputField.text;
        StartCoroutine(DownloadData());
      }
    
      private IEnumerator DownloadData()
      {
        url = Path.Combine(
          "https://docs.google.com/spreadsheets/d",
          googleSheetDocID,
          "export?format=csv");
    
        Debug.Log(url);
    
        using var unityWebRequest = UnityWebRequest.Get(url);
        unityWebRequest.SendWebRequest();
    
        while (!unityWebRequest.isDone)
        {
          yield return null;
          
          // Things to process in units of frames
          // when unityWebRequest has not finished downloading ex) progress
          // ...
        }
    
        Debug.Log("Success DownloadData()");
      }
    }
    
    

    Separate the part that inputs the InputField with the keyboard and the part that updates the googleSheetDocID.


    FYI, as @Max Play said, infinite loop doesn't happen.

    Hope your problem is solved :)