- Home /
 
How do I navigate to a directory in game and upload an image to a pre-determined area like a canvas? ( IN GAME )
Hi there,
I am trying to develop a simple 2D App/Game whereby once the project is built in an EXE..
There will be a button to press which will open up a Local file Browser / Directory whereby the user can navigate to say , ' the desktop' and select a Jped or PNG Image.
Once they have selected the image, it will upload into the canvas in game where they can then draw ( ui touch input ) on the canvas and then save what they see as a screenshot ( to a folder on their PC ) Which they can Then Email onward to another person.
I am just trying to find out if this is possible and how it would be done.
All I am worried about at this moment is the simple navigating to a specific directory and uploading an image such as a JPEG in game.
Thanks a lot for any help !
Answer by dmitriy-untilov · Feb 19, 2018 at 02:22 PM
Try to look into these articles related to open file browser: https://answers.unity.com/questions/648882/how-to-call-the-open-file-dialog-box-during-runtim.html https://answers.unity.com/questions/770063/equivalent-of-openfilepanel-without-using-unityedi.html?childToView=1134105#answer-1134105
And this custom solution https://github.com/gkngkc/UnityStandaloneFileBrowser from forum thread https://forum.unity.com/threads/openfiledialog-in-runtime.459474/
Load image at run-time you can via System.IO.ReadAllBytes(filepath); and load it to Texture by using Texture2D.LoadImage(bytes); https://docs.unity3d.com/455/Documentation/ScriptReference/Texture2D.LoadImage.html
Answer by BattleCharge · Apr 27, 2018 at 08:52 AM
@wolfman 1)Make a raw Image 2) Add Button to canvas 3) Add script below to canvas 4)on button click, add canvas>Explorer script> Open Explorer 5) Add Raw Image to Explorer Script in Canvas
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  UnityEngine.UI;
  using UnityEditor;
  public class Explorer : MonoBehaviour 
  {
 string path;
 public RawImage image;
 public void OpenExplorer()
 {
         path = EditorUtility.OpenFilePanel("Overwrite with png", "", "png");
         GetImage();
 }
 void GetImage()
 {
     if (path != null)
     {
             UpdateImage();
     }
 }
     void UpdateImage()
 {
     WWW www = new WWW("file:///" + path);
     image.texture = www.texture;
 }
 void Display(GameObject plane, byte[] data){
     var texture = new Texture2D(Screen.width, Screen.height);
     texture.LoadRawTextureData(data);
     texture.Apply();
     var the_renderer = plane.GetComponent<Renderer>();
     the_renderer.material.mainTexture = texture;
      }
  }
 
              Your answer
 
             Follow this Question
Related Questions
Saving File in new directory - Unauthorized Access Exception 1 Answer
How do I load a config (.cfg) or ini file? 2 Answers
How to get the next file in a directory? 1 Answer
Cannot find 3D Prefab after doing a build. 0 Answers
Getting Directory not Found error with Binary Formatting attempt 2 Answers