奇怪的是,我的代码没有将用户图像从本地硬盘加载到名为“planeLogo”的游戏对象。具有 ImageUploaderCaptureClick() 函数的文件名为 ImageUploader1.jslib,位于 plugins/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;
}
}
这就是我处理这些事件的方式。
我已经尝试了我所知道的一切,并且该项目在 Unity 中继续工作,但当它被编译为 html(webgl) 时却没有。