Unity android Streaming Assets 加载图片

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

至于他在手机上的位置

猜你喜欢

转载自blog.csdn.net/lvcoc/article/details/115305094
今日推荐