0
\$\begingroup\$

I'm trying to load a class using JsonUtility to populate a few strings. I navigate to the file using Unity Simple File Browser and load it using the following coroutine:

IEnumerator ShowLoad()
{
    yield return FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.Files, false, null, null, "Upload", "Select");


    if (FileBrowser.Success)
    {
        Debug.Log("File Browsed: " + FileBrowser.Result[0]);

        UnityWebRequest url = new UnityWebRequest("file:///" + FileBrowser.Result[0]);

        yield return url;

        StreamReader reader = new StreamReader(url.ToString());
        string json = reader.ReadToEnd();

        Campaign c = JsonUtility.FromJson<Campaign>(json);
        CampaignName = c.CampaignName;
        saveFolder = c.saveFolder;
        savePath = c.savePath;
        persistentFolder = c.persistentFolder;
        persistentPath = c.persistentPath;
    }
}

But every time I try to run the code I get the following error:

FileNotFoundException: Could not find file "C:\Users\arily\Documents\GitKraken\DND-Astra\Astra\UnityEngine.Networking.UnityWebRequest"
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <7b935204f5ff4bcab44b3b0ebca330bf>:0)
System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options) (at <7b935204f5ff4bcab44b3b0ebca330bf>:0)
(wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions)
System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize) (at <7b935204f5ff4bcab44b3b0ebca330bf>:0)
System.IO.StreamReader..ctor (System.String path, System.Boolean detectEncodingFromByteOrderMarks) (at <7b935204f5ff4bcab44b3b0ebca330bf>:0)
System.IO.StreamReader..ctor (System.String path) (at <7b935204f5ff4bcab44b3b0ebca330bf>:0)
(wrapper remoting-invoke-with-check) System.IO.StreamReader..ctor(string)
Campaign+<ShowLoad>d__12.MoveNext () (at Assets/Campaign.cs:90)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <ab7de6937a4448b0a6ccd59e9820599a>:0)

I have tried the same method using WWW to load an image and it worked, but when loading a JSON using WWW I get the same error referring to UnityEngine.WWW instead. I haven't been able to find anything online about it, so I'm posting here. Any insight or tips would be much appreciated, thank you!

\$\endgroup\$
0

1 Answer 1

1
\$\begingroup\$

Your resultant path is wrong:

"C:\Users\arily\Documents\GitKraken\DND-Astra\Astra\UnityEngine.Networking.UnityWebRequest"

Surely that should be something more like,

"C:\Users\arily\Documents\GitKraken\DND-Astra\Astra\myfile.json"

I think you've got this part wrong:

FileBrowser.Result[0]

Should probably be something like FileBrowser.Result[0].fileName as its currently converting an object to a string representation.

\$\endgroup\$
6
  • \$\begingroup\$ Rather than that, Arilyn is also making a new UnityWebRequest of a local file. That seems kinda wrong too. According to the github, FileBrowserHelpers.ReadBytesFromFile( FileBrowser.Result[0] ) returns a byte[] \$\endgroup\$
    – Zibelas
    May 9 at 18:09
  • \$\begingroup\$ That's what I thought - was going to suggest running a webserver - but the solution should fit the problem, rather than vice-versa. \$\endgroup\$
    – Engineer
    May 9 at 18:10
  • \$\begingroup\$ Hi, thanks for your comments. The Debug.Log statement outputs the correct path. The result isn't the issue, I don't think, since using the .ToString method didn't change anything. Line 90 is referred to in the error which ends up being StreamReader reader = new StreamReader(url.ToString()); \$\endgroup\$
    – Arilyn
    May 9 at 22:21
  • \$\begingroup\$ You dont want to use UnityWebRequest if you want to load a local file \$\endgroup\$
    – Zibelas
    May 9 at 22:29
  • 1
    \$\begingroup\$ @Arilyn want to post your solution as an Answer below? \$\endgroup\$
    – DMGregory
    May 10 at 0:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .