0

My code strangely isn't loading the user images from local hard drive to the gameObject called "planeLogo". The file with the function ImageUploaderCaptureClick() is called ImageUploader1.jslib and resides in the plugins/WebGl folder.

Here's the script

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;

public class LoadImage : MonoBehaviour
{


    [DllImport("__Internal")]
    private static extern void ImageUploaderCaptureClick();
    private Texture2D displayedTexture;

    IEnumerator LoadTexture(string url)
    {
        WWW image = new WWW(url);
        yield return image;
        image.LoadImageIntoTexture(displayedTexture);
    }

    void FileSelected(string url)
    {
        StartCoroutine(LoadTexture(url));
    }

    public void OnButtonPointerDown()
    {
#if UNITY_EDITOR
        string path = UnityEditor.EditorUtility.OpenFilePanel("Open image", "", "jpg,png,bmp");
        if (!System.String.IsNullOrEmpty(path))
            FileSelected("file:///" + path);
#else
        ImageUploaderCaptureClick ();
#endif
    }

    void Start()
    {
        displayedTexture = new Texture2D(1, 1);
        GameObject.Find("PlaneLogo").GetComponent<MeshRenderer>().material.mainTexture = displayedTexture;
        GameObject.Find("PlaneLogo").GetComponent<Renderer>().enabled = true;
    }
}

And here's how I deal with the events.

enter image description here

I've tried everything I know and the project keeps working inside Unity but does not when it's compiled as html(webgl).

gman
  • 100,619
  • 31
  • 269
  • 393
Muteking
  • 1,465
  • 5
  • 18
  • 31
  • Run a simple web server. It will take you just a few seconds to setup. [How about this one](https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb?hl=en)? or [these](http://stackoverflow.com/questions/12905426/what-is-a-faster-alternative-to-pythons-simplehttpserver/) – gman Aug 02 '16 at 15:37

2 Answers2

0

Unity WebGL does not have access to your file system

https://docs.unity3d.com/Manual/webgl-debugging.html

You'll have to fetch your image from a server. It's ghetto but the only way I could see it happening is if you set something up where the server fetches the image locally and relays it to your WebGL game.

Shredsauce
  • 63
  • 1
  • 9
0

hey bro you should try to this code it's work perfectly here my code:

string str_Parthname = Application.streamingAssetsPath + "/SceneDesigns/ComplViaduct" + "/ComplViaduct_Thumbnail" + ".png";
// string json_data = File.ReadAllText(str_Parthname);

    Debug.Log("name=="+str_Parthname);
    WWWForm wwform = new WWWForm();
    WWW wwwfile = new WWW(str_Parthname, wwform);
    yield return wwwfile;
    if (wwwfile.error != null)
    {
    }
    else
    {
        //img_display.sprite = LoadNewSprite(str_Parthname);
        Texture2D downloadedImage = new Texture2D(200, 200,TextureFormat.DXT1,false);

        wwwfile.LoadImageIntoTexture(downloadedImage);
        Rect rect = new Rect(0,0,downloadedImage.width,downloadedImage.height);
        Sprite sprite = Sprite.Create(downloadedImage,rect,new Vector2(0.5f,0.5f),100);
        img_display.sprite = sprite;

        //Debug.Log("debug=="+ wwwfile.text);
        //img_display.sprite=
        //txt_displayfoldername.text = wwwfile.text;
    }
Jeroen Heier
  • 3,520
  • 15
  • 31
  • 32
  • Hi, welcome to stack overflow. Cleanup the code (remove comments) and explain why this is an answer to the question. – Jeroen Heier Sep 24 '19 at 03:43