奇怪的是,我的代码并没有将用户图像从本地硬盘加载到名为" planeLogo"的gameObject。具有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)时却没有。
答案 0 :(得分:0)
Unity WebGL无权访问您的文件系统
https://docs.unity3d.com/Manual/webgl-debugging.html
您必须从服务器获取图像。它是贫民窟,但我能看到它发生的唯一方法就是你设置一些东西,服务器在本地获取图像并将其传递给你的WebGL游戏。
答案 1 :(得分:0)
嘿,兄弟,您应该尝试使用此代码,它的工作原理非常完美 这是我的代码:
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;
}