Jump to content
Unity Insider Forum

Probleme mit yield return.


StefanD

Recommended Posts

Hallo zusammen,

ich bin gerade dabei meinen code für WebGL zu konvertieren. Leider funktioniert hier das Laden von Daten aus dem StreamingAsset Ordner anders als im normalen Windows Player.
Hier ist ein Beispiel aus der Unity Bibliothek: https://docs.unity3d.com/560/Documentation/ScriptReference/Application-streamingAssetsPath.html

Mein original Code sieht wie folgt aus:
 

protected void LoadTextFile(string directoryName, string fileName, Action<string> readText)
{
  // Construct path to file.
  var filePath = Path.Combine(_basePath, directoryName);
  filePath = Path.Combine(filePath, fileName);

  // Check if file exists
  if (File.Exists(filePath))
  {
    // Involke the read text action.
    var text = File.ReadAllText(filePath);
    readText(text);
  }
  else
  {
    log.ErrorFormat("File with path {0} does not exist.", filePath);
  }
}

 

Dieses habe ich dann versucht zu konvertieren. Um das laden auch in WebGL möglich zu machen.
Der Code hierfür sieht dann wie folgt aus:

protected IEnumerator LoadTextFile(string directoryName, string fileName, Action<string> readText)
{
  // Construct path to file.
  var filePath = Path.Combine(_basePath, directoryName);
  filePath = Path.Combine(filePath, fileName);

  // Check if file is read from indexdb
  if (filePath.Contains("://") || filePath.Contains(":///"))
  {
    // file is loaded from index db. For examlple for webgl or android
    WWW www = new WWW(filePath);
    yield return www;
  
  	// TODO: Check for errors
  
    var text = www.text;
    readText(text);
  }
  else
  {
    // file is loaded from filesystem.
    if (File.Exists(filePath))
    {
      // file exists on filesystem.
      var text = File.ReadAllText(filePath);
      readText(text);
    }
    else
    {
      // file does not exist on filesystem.
      log.ErrorFormat("File with path {0} does not exist.", filePath);
    }
  }
}

Leider funktioniert das ganze so aber nicht. In der Version funktioniert das laden in dem normalen Player und auch im WebGL nicht. Leider komme ich auch mit dem "yield return www" nicht ganz zu recht wie ich das richtig verwenden soll.

Aufgerufen wird das ganze dann wie folgt.

public void LoadPrototypes(string fileName, Action<string> prototypesLoader)
{
  if (fileName == null)
    throw new ArgumentNullException(nameof(fileName));

  if (prototypesLoader == null)
    throw new ArgumentNullException(nameof(prototypesLoader));

  // Load the text file.
  LoadTextFile(
    "Data",
    fileName,
    (text) =>
    {
      prototypesLoader(text);
    });
}

Kann mir hier jemand erklären was ich falsch gemacht habe bzw. wie man yield kongret anwendet
um die Ausführung bis zum fertigen laden der Daten zu stopen.

Vielen Dank

Gruß Stefan

Link zu diesem Kommentar
Auf anderen Seiten teilen

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Auf dieses Thema antworten...

×   Du hast formatierten Text eingefügt.   Formatierung jetzt entfernen

  Only 75 emoji are allowed.

×   Dein Link wurde automatisch eingebettet.   Einbetten rückgängig machen und als Link darstellen

×   Dein vorheriger Inhalt wurde wiederhergestellt.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Lädt...
×
×
  • Neu erstellen...