unity从本地上传图片

 

 dd633536a4034a038a10bb5ad8d16767.png

d655e14abd0a4effbce816d8283f0fef.png 

 


using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class ChangeImage : MonoBehaviour
{
    private static ChangeImage _instance;
    public Image currentImage;//当前图片
    public Button selectBtn;//选择图片的按钮
    public Image imageprefab;
    public bool isaddimg=false;
    private void Start()
    {
        selectBtn.onClick.AddListener(OnSelectBtnOnclick);//监听按是否被按下,按下则执行括号中的方法
    }
    //当按钮被按下时执行该脚本,打开本地文件夹
    private void Update()
    {
        
    }
    public static ChangeImage GetInstance()
    {
        return _instance;
    }
    private void OnSelectBtnOnclick()
    {
        isaddimg = true;
        currentImage = GameObject.Instantiate(imageprefab, transform);
        currentImage.gameObject.SetActive(true);
        OpenFileimage ofn = new OpenFileimage();

        ofn.structSize = Marshal.SizeOf(ofn);
        //可进行修改选择的文件类型
        ofn.filter = "图片文件(*.jpg*.png)\0*.jpg;*.png";
        ofn.file = new string(new char[256]);

        ofn.maxFile = ofn.file.Length;

        ofn.fileTitle = new string(new char[64]);

        ofn.maxFileTitle = ofn.fileTitle.Length;
        string path = Application.streamingAssetsPath;
        path = path.Replace('/', '\\');
        //默认路径
        ofn.initialDir = path;

        ofn.title = "选择需要替换的图片";

        ofn.defExt = "JPG";//显示文件的类型
                           //注意 一下项目不一定要全选 但是0x00000008项不要缺少
        ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR

        if (WindowDll.GetOpenFileName(ofn))
        {
            StartCoroutine(LoadTextrue(ofn.file));
        }
    }
    public List<Image> wuzhengimage = new List<Image>();
    //加载选择的图片并进行替换    
    public string[] sArray;
    IEnumerator LoadTextrue(string path)
    {
        
        UnityWebRequest unityWebRequest = new UnityWebRequest("file:///" + path);
    DownloadHandlerTexture handlerTexture = new DownloadHandlerTexture(true);
    unityWebRequest.downloadHandler = handlerTexture;
        yield return unityWebRequest.SendWebRequest();
        if (unityWebRequest.isNetworkError || unityWebRequest.isHttpError)
        {
            print(unityWebRequest.error);
        }
        else
        {
            Texture2D t = handlerTexture.texture;
            Debug.Log("1111" + t.isReadable);
            //将选择的图片替换上去
            currentImage.sprite = Sprite.Create(t, new Rect(0, 0, t.width, t.height), Vector2.one);
        }
       
       
        wuzhengimage.Add(currentImage);
        MainUIController.GetInstance().saveimage(currentImage.sprite);
       
    }
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值