I have two tasks:

  1. I want to load a text file from the web, but I don’t understand how to write a class that has a static method that accepts a url and eventually returns the downloaded content. Namely, it is not clear to me how to turn this with an IEnumerator, considering that he does not return anything when executed.

  2. I would like to load during game initialization. The test file will contain configs for everything (pass the player’s movement, etc.). And as far as I understand, running IEnumerator is not a blocking operation. How to implement?

  • What is with enumerator, what is without it - the tasks are completely identical. Do not think about enumerators, imagine that this is a regular getter, or a function that returns a value. Do you have any developments? - test123
  • There are no @ test123 developments, just an example from the docs.unity3d.com/ScriptReference/WWW.html documentation, which actually uses IEnumerator. My knowledge of both the Mono platform and the C # language is poor. I understand as if I wrote in JavaScript (with promises or callbacks). Would fulfill the request, wait for an answer, and only then began the initialization of the game. - dakiesse 4:02 pm

1 answer 1

How so? I did not check it myself, but it seems to work. Where action- function in which the text is transmitted, and then at will.

public stati class Load { public static void LoadFile(MonoBehevior m, string url, Action<string> action) { m.StartCoroutine(LoadCoroutine(url, action)); } private static IEnumerator LoadCoroutine(string u, Action<string> a) { using(WWW url=new WWW(u)) { yield return url; if(url.error!=null&&url.error!="") { a(url.text); } } } }