User Image Upload

Ok so I have 2 issues. The first one is that when I attempt to have a user upload an image, it gives
7560832--935275--upload_2021-10-10_1-20-51.png

So I spent a couple hours troubleshooting (with no prevail) and decided to try and see if it would only work in a built version of the game. I tried to build it and got the following:

EditorUtility is giving me issues for unknown reasons. Originally, the problem was ‘File’ being unknown on line 25. However I switched from ‘using UnityEngine.Windows’ to ‘using System.IO’ which seems to be working in that regard.

I cant build the game, and I cant upload images to the game while it is running. Any input would be appreciated. Here’s the file causing issues:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using System.IO;

public class PlayerCard : MonoBehaviour
{
private Texture2D texture;
public Button yourButton;

void Start()
{
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(imageUpload);
}
void Update()
{

}
void imageUpload()
{
string path = EditorUtility.OpenFilePanel("", "", "");
var bytes = File.ReadAllBytes(path);
texture.LoadImage(bytes);
GetComponent<Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2());
}
}

If it’s not an Editor script, then you can’t use UnityEditor, and thus, you can’t use EditorUtility.
Thus why they have the word “Editor” in them. They are designed for Editor scripts and the code would have to be in an Editor folder. That code can’t be used in a build.

Thanks but I need an alternative then because i have looked everywhere and everything else is outdated like www or doesn’t exist

if you want File browser dialog (for desktop)
https://github.com/gkngkc/UnityStandaloneFileBrowser

2 Likes

You use the word “upload” but nothing in your code above has anything to do with the network.

If you want stuff sent by network from Unity, use UnityWebRequest().

Alternatively you may try to use something else and then spend weeks chasing around a ton of C# and .NET incompatibilities before finally coming back around to using UnityWebRequest in the end, your choice. :slight_smile:

As with ANYTHING networking-related, use this approach:

Networking, UnityWebRequest, WWW, Postman, curl, WebAPI, etc:

And setting up a proxy can be very helpful too, in order to compare traffic:

Thank you so much. Im gonna look through this in a minute.

Alright it looks like this should work, but after getting it all set up, and having to deal with more errors, it still is giving me 7562425--935668--upload_2021-10-10_22-23-26.png

The last one. Iv been dealing with the first two but it doesnt seem I can do anything for those so I just let it be and it runs well. I am adding these cause I am unsure if they are relevant to why the program cant take an image from a users computer and put it into the game.

My issue isn’t web related though. I am just trying to find a way to have a user upload an image the game. I do want to try and add a multiplayer thing later but that’s a future thing and I am not there yet lol. I only mentioned the WWW because I had seen it was a form of alternative to what I am trying to do.

Ok Im going through it and the path IS correct. It is getting the correct path for the file that i want to try and upload into the game, the issue is the File.ReadAllBytes(). I have tried File from UnityEngine.Windows and i have tried File from System.IO. I am not sure where the error here is but it is becoming a tad frustrating lol.

EDIT: The error might actually be Texture2D.LoadImage because I tested some of the output of the byte array from ReadAllBytes and its giving me numbers which I assume is correct. I even tried the exact example from Texture2D.LoadImage and it failed.

You’re using the wrong wording. Upload indicates sending something somewhere, usually remotely. Like uploading an image to Google drive or a database, etc. Thus why people are confused.

Sounds like all you want to do is load an image into your game.

Now, I’ve used https://github.com/yasirkula/UnitySimpleFileBrowser for a PC file browser. Now, if you’re getting stuck on something, show us your code WITH code tags. If you don’t use code tags, people are less likely to help you.
Explain what you want, explain what is happening, copy and paste the errors you are getting as they include full stack trace information.

Edit: Also, if you’re using code from somewhere else, you’ll need to make sure you are implementing it correctly.