路径和图库



我有一个问题与我的画廊代码,我的Android不识别正确的文件夹与。jpg图像在第20和30行。这是一个很大的不同,一个PC代码的原因磁盘的细节,如果有人可以帮助我,我将非常感激

public class Gallery : MonoBehaviour {
public List<Sprite> gallery = new List<Sprite>();
public Image displayImage;
public Button nextImg;
public Button prevImg;
public int i = 0;
void Start ()
{
// var Images = Directory.GetFiles("C:/Users/Bandeira/Downloads/Menu Start/Assets/Sprite/JPG/","*.jpg");
var Images = Directory.GetFiles("file:///" + "/unitypictures/","*.jpg");
Debug.Log(Images);
StartCoroutine(LoadImages(Images));
}
IEnumerator LoadImages(string[] Images)
{
foreach (var path in Images)
{
Debug.Log(path);
using (var uwr = UnityWebRequestTexture.GetTexture("file:///" + path))
// using (var uwr = UnityWebRequestTexture.GetTexture(path))
{
Debug.Log(path);
yield return uwr.SendWebRequest();

if (uwr.result != UnityWebRequest.Result.Success)
{
Debug.Log(uwr.error);
yield break;
}

var tex = DownloadHandlerTexture.GetContent(uwr);

var sprite = Sprite.Create(tex, new Rect(0f, 0f, tex.width, tex.height), new Vector2(0.5f, 0.5f), 50f, 0, SpriteMeshType.FullRect);
gallery.Add(sprite);
uwr.Dispose();
}
yield return null;
}
}
public void BtnNext (){
if(i + 1 < gallery.Count){
i++;
}
displayImage.sprite = gallery[i];
}

public void BtnPrev () {
if (i - 1 > 0){
i--;
}
displayImage.sprite = gallery[i];
}
}

老兄,我昨天已经告诉过你了。您使用的是绝对路径,它们不能工作,因为对于每个操作系统,它们是不同的。如果你把安卓手机连接到电脑上,或者安装了一个文件管理器,你会发现应该没有名为"unityptures"的文件夹,即使有,你也不能轻易到达那里,因为我相信它们是受设备保护的。答案总是一样的:您可以使用Persistent数据路径保存映像并在以后恢复它们。例如,您可以将它们保存在目录(应用程序)中。PersistentDataPath + "UnityPictures"),并从相同的路径获取它们。所以要明确一点:如果你想下载这些图片,你必须创建文件夹并插入这些图片。或者,您也可以从服务器下载它们。所以你可以编辑它们,添加更多,或者删除它们,而不需要更新,它将适用于所有设备。

最新更新