unity Android 可后台替换图片

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

public class Main:MonoBehaviour {
    private FileInfo[] files;
    public GameObject explain;
    public GameObject homePage;
    public GameObject allBtn;
    public GameObject showPic;
    public List<string> list = new List<string>();
    private int currentStep = 0;
    private int totalStep;
    public GameObject nextBtn;
    public GameObject prevBtn;
    public GameObject backBtn;
    public string standName;
    public VideoPlayer video;
    private string AndroidDirectory = "/storage/emulated/0/Android/";//安卓路径
    private string WindowsDirectory = System.Environment.CurrentDirectory + "/资源/";//Windows路径
    private string filePath;
    void Start() {
        explain.SetActive(false);
        explain.GetComponent<Image>().raycastTarget = false;
     
        if(Directory.Exists(WindowsDirectory)) {
            filePath = WindowsDirectory;
        } else if(Directory.Exists(AndroidDirectory)) {
            filePath = AndroidDirectory + "资源";
        } else {
            return;
        }
        for(int i = 0;i < allBtn.transform.childCount;i++) {
            GameObject obj = allBtn.transform.GetChild(i).gameObject;
            EventTriggerListener.Get(obj).onClick = onClick;
        }

        string homePath = filePath + "/" + standName + ".png";
        LoaderPic.LoadByIo(homePath,homePage);
        video.url = filePath + "/" + standName + ".mp4";

        string explainPath = filePath + "/" + standName + "弹框.png";
        LoaderPic.LoadByIo(explainPath,explain);
        EventTriggerListener.Get(nextBtn).onClick = onNext;
        EventTriggerListener.Get(prevBtn).onClick = onPrev;
        EventTriggerListener.Get(backBtn).onClick = onBack;
    }

    public Text txt;
    void onClick(GameObject sender) {
        string getPaths = filePath + "/图片" + sender.name;
        if(Directory.Exists(getPaths)) {//判断这个路径是否存在
            DirectoryInfo direction = new DirectoryInfo(getPaths);
            files = direction.GetFiles("*",SearchOption.AllDirectories);
        }
        list.Clear();
        for(int i = 0;i < files.Length;i++) {
            if(files[i].Name.EndsWith(".meta")) {
                continue;
            }
            list.Add(files[i].ToString());
        }
        totalStep = list.Count;
        showPic.SetActive(true);
        explain.SetActive(true);
        txt.text = "====" + list[currentStep].ToString();
        LoaderPic.LoadByIo(list[currentStep],showPic);
    }

    void onNext(GameObject sender) {
        currentStep++;
        if(currentStep >= totalStep) {
            currentStep = 0;
        }
       LoaderPic.LoadByIo(list[currentStep],showPic);
    }
    void onPrev(GameObject sender) {
        if(currentStep <= 0) {
            currentStep = totalStep;
        }
        currentStep--;
        LoaderPic.LoadByIo(list[currentStep],showPic);
    }
    void onBack(GameObject sender) {
        showPic.SetActive(false);
        explain.SetActive(false);
        list.Clear();
    }

    IEnumerator GetTexture(Image showPic, string url) {
        UnityWebRequest uwr = UnityWebRequest.Get(url);
        DownloadHandlerTexture downloadTexture = new DownloadHandlerTexture(true);
        uwr.downloadHandler = downloadTexture;
        yield return uwr.SendWebRequest();
        if(uwr.isNetworkError || uwr.isHttpError) {
            print(uwr.error);
        } else {
            Texture2D t = downloadTexture.texture;
            showPic.sprite = Sprite.Create(t,new Rect(0,0,t.width,t.height),Vector2.one);
        }
    }
}

 

posted on 2023-02-14 13:27  zqiang0803  阅读(52)  评论(0编辑  收藏  举报

导航