Unity android Streaming Assets 加载图片

这篇博客介绍了在Unity中如何使用UnityWebRequest加载纹理资源,特别是在移动端如何处理只读文件系统的问题。通过Application.persistentDataPath获取可读写路径,并展示了如何使用IO读取 Texture2D。同时,讲解了不同平台下资源路径的差异及其处理方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Path.Combine("jar:file://" + Application.dataPath + "!/assets", relativePath);

不能用IO,只能用www,最新的就是UnityWeb

System.Collections.IEnumerator ChangeImageCo()
    {
        using (uwr =
       UnityWebRequestTexture.GetTexture(GetFileLocation(customTextureFilename)))
        {
            yield return uwr.SendWebRequest();
            if (uwr.isNetworkError || uwr.isHttpError)
            {
                Debug.Log(uwr.error);
            }
            else
            {
                rawImage.texture =
               DownloadHandlerTexture.GetContent(uwr);
            }
        }
    }

不像PC,手机上这个文件夹是只读的,如果你想读写就用Application.persistentDataPath

public static string GetFileLocation(string relativePath)
    {
        return "file:///" +Path.Combine(Application.persistentDataPath, relativePath);
    }

这个时候就可以用IO了,去掉上面地址里的file:///

    private Texture2D LoadImage(string path)
    {
        Texture2D texture = new Texture2D(1920, 1080, TextureFormat.RGB24, false);
        using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            fs.Seek(0, SeekOrigin.Begin);
            byte[] bytes = new byte[fs.Length];
            fs.Read(bytes, 0, (int)fs.Length);
            texture.LoadImage(bytes);
        }

        return texture;
    }

至于他在手机上的位置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值