2016-08-02 81 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

回答