vmvr
May 21, 2024, 7:48pm
1
Im trying to download a picture from Windows Shared folder.
Here’s my code:
public async Task<Texture2D> ReadTexture(string path)
{
if (path.StartsWith(@"\\"))
path = @"file:\\" + path;
print(path); //output: file:\\\\DESKTOP-89HPKIQ\shared\cute.jpg
using (var request = UnityWebRequestTexture.GetTexture(path))
{
request.SendWebRequest();
while (!request.isDone)
{
await Task.Yield();
}
if (request.result == UnityWebRequest.Result.Success)
{
var texture = DownloadHandlerTexture.GetContent(request);
;
texture.name = Path.GetFileName(path);
ConsoleWindow.Instance.Print($"Image loaded: {texture.name}");
return texture;
}
else
{
ConsoleWindow.Instance.Print(request.error);
print(request.error);
print(request.url);
print(request.uri);
return null;
}
}
}
The folder (\DESKTOP-89HPKIQ\shared):
The output:
I tried 3 slashes. It gives me HTTP/1.1 404 Not Found
I tried 2: Curl error 3: URL rejected: Port number was not a decimal number between 0 and 65535
Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:
As with all network code, get it working with Postman or curl first. That makes it NOT a Unity problem. Solve the networking first. You must solve that or anything else is irrelevant.
Once you have that working, then try and port it to UnityWebRequest. If it still doesn't work, one handy debug solution is to hook up a proxy like Charles and compare the functioning output from Postman / curl to the output from Unity, and from that reason about where your issues are.
Always get network stuff working outside of Unity in either curl or PostMan, then connect a proxy such as Charles and start trying to replicate the traffic shape with UnityWebRequest.
Any other way risks wasting a lot of your time for trivial issues that only the above way will instantly reveal.
And setting up a proxy can be very helpful too, in order to compare traffic:
vmvr
May 21, 2024, 9:03pm
3
Checked outside unity.
windows forms:
private void Form1_Load(object sender, EventArgs e)
{
string path = @"\\DESKTOP-89HPKIQ\shared";
var files = Directory.GetFiles(path);
pictureBox1.Image = Image.FromFile(files[0]);
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
Works perfectly.
I use the same path in unity.
You most likely have the wrong number of slashes behind the file protocol . You usually have an additional slash when you’re on localhost. However you specify a different host, so you most likely need “less” slashes. Try with 3 or 2. There are many variants and Unity may not necessarily support network paths. Though those are the first things I would try.
ps: URLs use forward slashes /
not backslashes \
.
vmvr
May 21, 2024, 9:18pm
5
FIXED!
You need to inverse slashes:
if (path.StartsWith(@"\\"))
{
path = path.Replace(@"\", @"/");
path = @"file://" + path;
}
Full code:
public async Task<Texture2D> ReadTexture(string path)
{
if (path.StartsWith(@"\\"))
{
path = path.Replace(@"\", @"/");
path = @"file://" + path;
}
print(path); //output: file:////DESKTOP-89HPKIQ/shared/cute.jpg
using (var request = UnityWebRequestTexture.GetTexture(path))
{
request.SendWebRequest();
while (!request.isDone)
{
await Task.Delay(3000);
}
if (request.result == UnityWebRequest.Result.Success)
{
var texture = DownloadHandlerTexture.GetContent(request);
;
texture.name = Path.GetFileName(path);
ConsoleWindow.Instance.Print($"Image loaded: {texture.name}");
return texture;
}
else
{
ConsoleWindow.Instance.Print(request.error);
print(request.error);
print(request.url);
print(request.uri);
return null;
}
}
}
vmvr
May 21, 2024, 9:19pm
6
Bunny83:
You most likely have the wrong number of slashes behind the file protocol . You usually have an additional slash when you’re on localhost. However you specify a different host, so you most likely need “less” slashes. Try with 3 or 2. There are many variants and Unity may not necessarily support network paths. Though those are the first things I would try.
ps: URLs use forward slashes /
not backslashes \
.
Yeah, just fixed it! Thank you!
We need 2 slashes for file:// and +2 from local network path.
Correct me if im wrong
When accessing Windows shares, the correct number of slashes 5. Unity does work with 4 too as a side effect of supporting file://c/folder/… (which itself isn’t actually correct according spec).
The file:// is a protocol prefix (with two slashes), which is supposed to start with Unix style path, which, when absolute, starts with a slash. The network share prefix of \ has to be changed to forward slashes and appended to the slash indicating a root, so “file://” + “/” + “//shared_name”.
BTW, I think “smb://share_name” should work too (and not only on Windows).