2016-08-02 73 views
0

我的代碼奇怪的是不會將用戶圖像從本地硬盤加載到名爲「planeLogo」的gameObject。帶有函數ImageUploaderCaptureClick()的文件稱爲ImageUploader1.jslib,位於插件/ WebGl文件夾中。Unity3D - 讓用戶爲webgl加載圖像

這裏的腳本

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; 
    } 
} 

這裏就是我如何處理事件。

enter image description here

我已經試過我什麼都知道,該項目一直工作到內部團結,但是當它編譯成HTML(WebGL的)沒有。

+0

運行一個簡單的Web服務器。它會花你幾秒鐘的時間來設置。 [這個怎麼樣](https://chrome.google.com/webstore/detail/web-server-for-chrome/ofhbbkphhbklhfoeikjpcbhemlocgigb?hl=en)?或[這些](http://stackoverflow.com/questions/12905426/what-is-a-faster-alternative-to-pythons-simplehttpserver/) – gman

回答