在android设备中查找图像的文件路径

问题描述 投票:-1回答:1

对不起,我对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个回答
0
投票

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

一种方法是使用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,但这是另一个故事。

© www.soinside.com 2019 - 2024. All rights reserved.