0

奇怪的是,我的代码没有将用户图像从本地硬盘加载到名为“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) 时却没有。

4

2 回答 2

0

Unity WebGL 无权访问您的文件系统

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

您必须从服务器获取图像。它是贫民窟,但我能看到它发生的唯一方法是,如果您设置了一些东西,服务器会在本地获取图像并将其中继到您的 WebGL 游戏。

于 2016-08-02T11:53:29.973 回答
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;
    }
于 2019-09-24T03:11:07.597 回答