【问题标题】:Locating file path of image in android devices在android设备中定位图像的文件路径
【发布时间】:2020-04-10 10:09:46
【问题描述】:

对不起,我对 unity 和 c# 很陌生。

我正在尝试读取图像并将其作为纹理应用。

当我在我的计算机上尝试使用图像时,该代码有效,但是,当我在我的 android 设备(手机和 AR 眼镜)上尝试时,我无法正确指定文件路径。如何正确指定 android 设备的文件路径,或者有什么方法可以获取这些文件路径?

非常感谢您提前提供的任何帮助! :)

void Start()
    {
        thisTexture = new Texture2D(100, 100);


        //string path = "C:/Users/kenny/Desktop/5th March/im.png";   //this works

        string path = "file:///storage/emulated/0/im2.png";         // this doesnt work

        bytes = File.ReadAllBytes(path);
        thisTexture.LoadImage(bytes);
        GetComponent<Renderer>().material.mainTexture = thisTexture;

    }

【问题讨论】:

    标签: c# unity3d textures vuforia


    【解决方案1】:

    对于移动设备,从普通路径获取文件并不那么简单。

    这样做的一种方法是使用Resources folder。在您的根文件夹 (Assets) 中,创建一个名为 Resources 的新文件夹。把你的图片放在那里。

    然后你可以这样做:

    // path without file extension!
    var texture = Resources.Load<Texture2D>("path/to/texture");
    

    您可以在此处获得有关此功能的更多参考资料:https://forum.unity.com/threads/how-to-load-a-image-from-the-resources-folder-to-a-texture2d.101542/

    或者,您可以使用StreamingAssets folder,但这是另一回事。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多