⭐Unity 里调用弹出电脑系统文件选择窗 (选择图片/文件)

今天遇到的需求要从Uinty里调用选择程序外的图片,类似手机环境下拿图库的照片一样。

效果如下:

话不多说 直接上代码!

1.编辑器模式下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Runtime.InteropServices;
//using System.Windows.Input;
//using System.Windows.Forms;
using System;
using UnityEngine.UI;

public class TestImage : MonoBehaviour
{
    public RawImage rawImage;
    // 引用一个Unity的Texture2D来显示加载的图片
    public Texture2D texture;
    // 在Unity中点击按钮调用此方法
    public void LoadImage()
    {
        // 打开文件对话框并等待用户选择文件
        string filePath = UnityEditor.EditorUtility.OpenFilePanel("Select PNG", "", "png");

        // 检查用户是否选择了文件
        if (!string.IsNullOrEmpty(filePath))
        {
            try
            {
                ImageProcessor._instance.inputImagePath = filePath;

                // 读取PNG文件为字节数组
                byte[] fileData = File.ReadAllBytes(filePath);

                // 创建一个新的Texture2D
                texture = new Texture2D(2, 2);

                // 加载PNG文件数据到Texture2D
                texture.LoadImage(fileData);

                // 将加载的图片显示在RawImage对象上
                rawImage.texture = texture;
            }
            catch (System.Exception ex)
            {
                // 处理异常情况
                Debug.LogError("Error loading image: " + ex.Message);
            }
        }
    }
}

2.打包后运行模式下

由于 UnityEditor.EditorUtility.OpenFilePanel 只支持在编辑器模式下,打包会报错。

所以这边需要使用跨平台的第三方库,如StandaloneFileBrowser,这是一个可以在游戏运行时使用的开源库,支持Windows、Mac和Linux平台的文件选择对话框。

链接如下:

Unity 独立文件浏览器 Github地址

下载之后导入Unity就可以啦~~~

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
// 引入StandaloneFileBrowser
using SFB;

public class TestImage : MonoBehaviour
{
    public RawImage rawImage;
    // 引用一个Unity的Texture2D来显示加载的图片
    public Texture2D texture;

    // 在Unity中点击按钮调用此方法
    public void LoadImage()
    {
        // 使用StandaloneFileBrowser打开文件对话框
        string[] paths = StandaloneFileBrowser.OpenFilePanel("Select PNG", "", "png", false);

        if (paths.Length > 0)
        {
            string filePath = paths[0];

            if (!string.IsNullOrEmpty(filePath))
            {
                try
                {
                    // 读取PNG文件为字节数组
                    byte[] fileData = File.ReadAllBytes(filePath);

                    // 创建一个新的Texture2D
                    texture = new Texture2D(2, 2);

                    // 加载PNG文件数据到Texture2D
                    texture.LoadImage(fileData);

                    // 将加载的图片显示在RawImage对象上
                    rawImage.texture = texture;
                }
                catch (System.Exception ex)
                {
                    // 处理异常情况
                    Debug.LogError("Error loading image: " + ex.Message);
                }
            }
        }
    }
}
相关推荐
蓝裕安7 小时前
DesignMode__unity__抽象工厂模式在unity中的应用、用单例模式进行资源加载
unity·游戏引擎
tealcwu12 小时前
【Unity基础】Unity中跨平台使用SQLite
unity·游戏引擎
阿汪队14 小时前
【Unity单机游戏框架】K-Framework
unity·游戏引擎
UTwelve16 小时前
【UE5】将2D切片图渲染为体积纹理,最终实现使用RT实时绘制体积纹理【第二篇-着色器制作】
ue5·游戏引擎·虚幻·着色器·虚幻4
Artistation Game17 小时前
二、鼠标的解锁与锁定
unity·c#·游戏引擎
@Sunset...1 天前
Unity数据持久化4——2进制
unity·c#·游戏引擎
yukino_NZB1 天前
Unity网络开发记录(二):采用多线程处理服务端对客户端的连接和信息处理
网络·unity·游戏引擎
ue星空1 天前
将Mixamo的模型和动画导入UE5
游戏·ue5·游戏引擎·虚幻·虚幻引擎
AT巡礼2 天前
音频以及麦克风
unity·音视频
Moweiii2 天前
C# 游戏引擎中的协程
开发语言·unity·c#·游戏引擎