How to import an image at runtime?

I have a button with an image component. I want the image of the image component to be selectable by the user of my application. The image selected by the user will not* be stored in any project folders to begin with. It will most likely come from the “Pictures” folder on their device. I imagine this working like uploading a profile picture in many modern applications. Once a user selects the option to import an image, a window pops up allowing them to navigate their storage and select the image. I would appreciate some direction as I have no idea where to begin with this.

1 Like

@tannaku Hey! I had a similar problem, I solved it using this script that I wrote, it won’t open any window for navigating to the file, but you probably can do something wilth File.ReadAllBytes.
This is function that will import image from given path:

        public static Texture2D LoadImage(string path)
        {
            if (File.Exists(path))
            {
                byte[] bytes = File.ReadAllBytes(path);
                Texture2D tex = new Texture2D(2, 2);
                tex.LoadImage(bytes);

                return tex;
            }
            else
            {
                return null;
            }
        }

will return texture2D.
Or:

        public static Texture2D LoadImage(string path)
        {
            if (File.Exists(path))
            {
                byte[] bytes = File.ReadAllBytes(path);
                Texture2D tex = new Texture2D(2, 2);
                tex.LoadImage(bytes);

                return tex;
            }
            else
            {
                return null;
            }
        }

        public static Sprite LoadImageAsSprite(string path)
        {
            Sprite sprite = Sprite.Create(LoadImage(path), new Rect(0.0f, 0.0f, LoadImage(path).width, 
            LoadImage(path).height), new Vector2(0.5f, 0.5f), 100.0f);

            return sprite;
        }

will return texture2D or Sprite

If you don’t anymore need this I understand because your comment is from 2017 :slight_smile: