Unity 安卓平台读写

发布时间:2024-02-23 13:00

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using  UnityEngine.Networking;
using UnityEngine.Video;

public class Test : MonoBehaviour
{
    public Image image;
    public AudioSource audioSource;
    public Text text;
    public VideoPlayer videoPlayer;

    private string abSavePath;
    private string abPath;
    private string imagePath;
    private string audioPath;
    private string textPath;
    private string videoPath;
    void Start()
    {
        imagePath = PathUtils.StreamingAssets + \"/Images/bg.png\";
        audioPath = PathUtils.StreamingAssets + \"/Audio/bg1.mp3\";
        textPath = PathUtils.StreamingAssets + \"/Json/json_config.txt\";
        videoPath = PathUtils.StreamingAssets + \"/Video/Test.mp4\";
        abSavePath = \"/AB/subjecttwo_zigong\";
        abPath = PathUtils.StreamingAssets + \"/AB/subjecttwo_zigong\";

        LoadSprite();
        LoadAudioClip();
        LoadTxt();
        LoadVideo();
        DownloadAB();
    }

    void LoadSprite()
    {
        StartCoroutine(Utils.LoadImageByUrl(imagePath, t =>
        {
            Sprite sp = Utils.LoadSpriteFromTexture2D(t);
            image.overrideSprite = sp;
        }));
    }
    
    void LoadAudioClip()
    {
        StartCoroutine(Utils.LoadAudioByUrl(audioPath, audio =>
        {
            audioSource.clip = audio;
        }));
    }

    void LoadTxt()
    {
        StartCoroutine(Utils.LoadRTextByUrl(textPath, txt =>
        {
            text.text = txt;
        }));
    }

    void LoadVideo()
    {
        videoPlayer.url = videoPath;
    }

    void DownloadAB()
    {
        StartCoroutine(Utils.DownloadABASsets(abPath, abSavePath, delegate
        {
            Debug.Log(\"下载完成\");            
        }));
    }
}

public class Utils
{
    public static IEnumerator LoadImageByUrl(string url,Action<Texture2D> action) 
    {
        UnityWebRequest request = new UnityWebRequest(url);
        DownloadHandlerTexture downloadHandler = new DownloadHandlerTexture(true);
        request.downloadHandler = downloadHandler;
        request.SendWebRequest();
        while (!request.isDone)
        {
            Debug.Log(request.downloadProgress);
            yield return 0;
        }
        Texture2D texture = null;
        if (!request.isNetworkError||request.isHttpError)
        {
            texture = downloadHandler.texture;
        }
        if (action!=null)
        {
            action(texture);
        }
    }

    public static Sprite LoadSpriteFromTexture2D(Texture2D texture)
    {
        Sprite sp = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
        return sp;
    }
    
    public static IEnumerator LoadRTextByUrl(string url,Action<string>callback)
    {
        UnityWebRequest request = UnityWebRequest.Get(url);
        request.SendWebRequest();
        while (!request.isDone)
        {
            Debug.Log(request.downloadProgress);
            yield return 0;
        }
        string str = request.downloadHandler.text;
        callback?.Invoke(str);
    }

    public static IEnumerator LoadAudioByUrl(string url,Action<AudioClip>callback)
    {
        UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(url,AudioType.UNKNOWN);
        request.SendWebRequest();
        while (!request.isDone)
        {
            Debug.Log(request.downloadProgress);
            yield return 0;
        }
        if (request.result== UnityWebRequest.Result.ConnectionError)
        {
            Debug.LogError(\"request.error\");
            callback?.Invoke(null);
        }
        else
        {
            AudioClip clip = DownloadHandlerAudioClip.GetContent(request);
            callback?.Invoke(clip);
        }
    }

    public static IEnumerator LoadABAssets(string url,Action<AssetBundle>callback)
    {
        UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(url);
        request.SendWebRequest();
        while (!request.isDone)
        {
            Debug.Log(request.downloadProgress);
            yield return 0;
        }
        AssetBundle ab = DownloadHandlerAssetBundle.GetContent(request);
        callback?.Invoke(ab);
    }

    public static IEnumerator DownloadABASsets(string url,string savePath,Action callback)
    {
        if (!Directory.Exists(PathUtils.PersistentDataPath+\"/AB\"))
        {
            Directory.CreateDirectory(PathUtils.PersistentDataPath+\"/AB\");
        }
        if (File.Exists(PathUtils.PersistentDataPath+savePath))
        {
            callback?.Invoke();
            yield break;
        }
        UnityWebRequest request = UnityWebRequest.Get(url);
        request.SendWebRequest();
        while (!request.isDone)
        {
            Debug.Log(request.downloadProgress);
            yield return 0;
        }

        if (request.isDone)
        {
            Debug.Log(\"download finished\");
        }

        byte[] data = request.downloadHandler.data;
        using (FileStream fs = new FileStream(PathUtils.PersistentDataPath+savePath,FileMode.Create))
        {
            fs.Write(data,0,data.Length);
        }
        callback?.Invoke();
    }
}

public class PathUtils
{
    public static string StreamingAssets
    {
        get
        {
            if (Application.platform== RuntimePlatform.Android)
            {
                return \"jar:file://\" + Application.dataPath + \"!/assets\";
            }else if (Application.platform== RuntimePlatform.IPhonePlayer)
            {
                return \"file://\"+Application.dataPath+\"/Raw\";
            }
            else
            {
                return Application.streamingAssetsPath;
            }
        }

    }

    public static string PersistentDataPath
    {
        get
        {
            return Application.persistentDataPath;
        }
    }
}

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号