Unity Android 加载StreamingAssets路径下资源

一、StreamingAssets在不同平台下的路径位置

Most platforms (Unity Editor, Windows, Linux players) use

Application.dataPath + "/StreamingAssets"

macOS player uses

Application.dataPath + "/Resources/Data/StreamingAssets"

iOS uses

Application.dataPath + "/Raw"

Android uses files inside a compressed APK/JAR file,

"jar:file://" + Application.dataPath + "!/assets"

官方文档传送门

二、Android下加载bundle资源

Android下加载bundle资源不需要通过WWW下载,可以直接加载资源。

加载时只需要修改一下路径既可以

"jar:file://" + Application.dataPath + "!/assets"

加载示例如下:

    private void LoadAssetBundleTexture()
    {
        if (m_rawImage == null)
        {
            return;
        }

        // 资源路径
        var bundlePath = "jar:file://" + Application.dataPath + "!/assets/" + m_bundleName;
        var bundle = AssetBundle.LoadFromFile(bundlePath);
        if (bundle == null)
        {
            return;
        }

        m_rawImage.texture = bundle.LoadAsset<Texture>(m_iconName);
        bundle.Unload(false);
    }

三、Android下加载Text或者bytes

    private void LoadFileBytes()
    {
        var bundlePath = Path.Combine(Application.streamingAssetsPath, m_bundleName);
        StartCoroutine(GetFileBytes(bundlePath));
    }

    IEnumerator GetFileBytes(string path)
    {
        var request = UnityWebRequest.Get(new System.Uri(path));
        yield return request.SendWebRequest();
        if (request.isNetworkError || request.isHttpError)
        {
            Debug.Log(request.error);
            yield break;
        }

        // 根据需求选择返回结果
        string text = request.downloadHandler.text;
        byte[] data = request.downloadHandler.data;
    }

四、UnityWebRequest的一些其他接口

 如果需要下载并加载bundle使用的话使用UnityWebRequestAssetBundle

 如果需要下载并加载Texture使用的话使用UnityWebRequestTexture

 因为UnityWebReques的一些接口已经废弃,建议根据unity版本选择接口使用。

 建议不要使WWW接口,因为该接口已经废弃了。

猜你喜欢

转载自blog.csdn.net/qq_33808037/article/details/125503928
今日推荐