Unity3D 移动端 本地 路径 Application.persistentDataPath

unity3D 访问本地文件以前都是访问 APK 包内的资源 ,比如说 声音,图片 等等。

        private IEnumerator DownLoadMovie()
        {
            PlatformCrossManager.Instance.OnMobileLog("==>读取userInfo.jpg路径:" + getImagePath());
            WWW www = new WWW(getImagePath());
 
            yield return www.isDone;
            if (string.IsNullOrEmpty(www.error))
            {
                headPortraitImage.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0.5f, 0.5f));
            }
            else
            {
                PlatformCrossManager.Instance.OnMobileLog("==>读取userInfo.jpg出错:" + www.error);
            }

        }

今天要访问的是,生成的文件 非 APK 内的资源。Unity3D是有区别的的:如下(重点是 Android 路径):

        private  string getImagePath() { 
        string src =
#if UNITY_EDITOR
            "file:///" + Application.persistentDataPath + "/userInfo.jpg";
#elif UNITY_ANDROID
            "file://" + Application.persistentDataPath + "/userInfo.jpg";
#else
            "file:///" + Application.persistentDataPath + "/userInfo.jpg";
#endif
            return src;
        }

要是访问 APK 内部的资源应该这样

        private  string getImagePath() { 
        string src =
#if UNITY_EDITOR
            "file:///" + Application.persistentDataPath + "/userInfo.jpg";
#elif UNITY_ANDROID
            "jar:file://" + Application.persistentDataPath + "/userInfo.jpg";
#else
            "file:///" + Application.persistentDataPath + "/userInfo.jpg";
#endif
            return src;
        }



 

猜你喜欢

转载自blog.csdn.net/nicepainkiller/article/details/79745566
今日推荐