首页 > 其他分享 >unity Android 可后台替换图片

unity Android 可后台替换图片

时间:2023-02-14 13:47:33浏览次数:42  
标签:string filePath GameObject currentStep unity 后台 using Android public

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);
        }
    }
}

 

标签:string,filePath,GameObject,currentStep,unity,后台,using,Android,public
From: https://www.cnblogs.com/zqiang0803/p/17119284.html

相关文章

  • Unity 转小游戏
    填写appid和游戏资源位置在导出的项目里可以修改游戏资源位置两个目录minigame是小程序打开的目录webgl是要下载的的资源下载一个http服务器就有了和JS交互大部......
  • Unity导出web浏览器运行的WebGL操作
    前提:1.Unity2020.3的编辑器+项目需要再Unity编辑器执行不报错2.系统win103.浏览器使用  或  导出:1.下载WebGL支持        ......
  • unity 保持物体大小不变
    ///<summary>///在透视投影时,改变相机的fov或物体的位置,让物体保持在屏幕上看起来的大小一样的缩放值///</summary>///<returns></returns>p......
  • Android 自定义变化的文本控件ColorChangeView
    实例图说明图/***有颜色过渡变化的textview**@ProjectApp_View*@Packagecom.android.view.colortextview*@authorchenlin*@version1.0*@NoteTODO*//*......
  • Android 实现简单的画画版一
    原理:1)首先创建一个创建一张空白图片2)把空白图片设置到imageview控件3)给imageview设置监听,并返回值true;代码如下:/***@描述画板*@项目名称App_Bas......
  • 2023-02-14 Apps targeting Android 12 and higher are required to specify an expli
    新建android项目,选择basicactive一项,在跑模拟器的时候,报错。解决方案:找到AndroidManifest.xml文件,在<activity里面添加一行属性 android:exported="true"详情Gpt给出......
  • Android Fragment的三种应用方式
    应用方式一:动态的使用Fragment首先是,MainActivity的布局文件activity_main.xml,该文件布局文件上面的顶部是一个TitleFragment,是一个静态声明的Fragment。中间也是一个Fr......
  • Unity Shader 后处理(1):景深效果
    景深的产生原因:摄像机成像时,通过棱镜聚焦,光线通过棱镜打在传感器平面上。不同物距的成像结果也会不一样,只有焦平面上的物体上的任意一点,通过过棱镜后会在传感器平面上聚焦......
  • Android 之 环境搭建
    1.Gradlegradle跟maven一样是一个包管理工具,Android项目默认的包管理工具,这两天使用下来,感觉比maven更加简洁,其他暂时没啥赶脚。1.1配置Javagradle需要java8+1.2......
  • Unity URP之如何实现半透明物体的阴影
    半透明物体的阴影现实中也应该是淡化过的,如何实现这种效果?渲染方案:使用内置的noise贴图来clip阴影。代码可参考:float2vpos=input.positionCS;h......