【问题标题】:Unity3D - Let user load image for webglUnity3D - 让用户为 webgl 加载图像
【发布时间】:2019-09-24 11:42:25
【问题描述】:

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

【问题讨论】:

标签: c# unity3d unity-webgl


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    嘿,兄弟,你应该试试这个代码,它工作得很好 这是我的代码:

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

    【讨论】:

    • 嗨,欢迎来到堆栈溢出。清理代码(删除 cmets)并解释为什么这是问题的答案。
    猜你喜欢
    • 2012-10-20
    • 2022-11-06
    • 2018-08-03
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多