Unity(十六) Unity加载本地/网络图片

更新日期:2021.1.13

工具:Unity2019.4.10

读取手机内存里的图片

"/storage/emulated/0" 是Android内存主目录

注意:file://+路径,总共是3个斜杠

 
    
    //方式1:使用www加载
    IEnumerator wwwLoadTexture(string filePath)
    {
        //例如  "file:///storage/emulated/0/Download/123.jpg"
        WWW www = new WWW(filePath);
        yield return www;
        if (www.isDone && www.error == null)
        {
            Texture2D texture = www.texture;
            //rawImage.texture = texture;
        }
    }


    //方式2:使用UnityWebRequest加载
    IEnumerator UnityWebRequestLoadPic(string dirpath)
    {
        //例如   "file:///storage/emulated/0/Download/123.jpg";
        using (UnityWebRequest request = UnityWebRequestTexture.GetTexture(dirpath))
        {
            yield return request.SendWebRequest();
            if (request.isHttpError || request.isNetworkError)
            {
                Debug.LogError("request error");
            }
            else
            {
                var texture = DownloadHandlerTexture.GetContent(request);
                //rawImage.texture = texture;
            }
        }
    }

读取StreamingAssets目录

 public static IEnumerator load()
{
        string path = string.Empty;
        string line1 = string.Empty;

        

        //Window路径
        path = Application.streamingAssetsPath + "/aaa.xml"; //方法1 读取streamingAssetsPath目录
        //path = "jar:file://" + Application.dataPath + "!/assets/" + "aaa.xml";//方法2和方法1一样




        //PC路径
        path = "file://" + Application.streamingAssetsPath + "/aaa.xml";
        //或  path = Application.dataPath + "/StreamingAssets" + "/aaa.xml";


        
        WWW wWA = new WWW(path);///WWW读取在各个平台上都可使用
        yield return wWA;
        line1 = wWA.text;
            
        Debug.Log(line1);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值