Unity Standalone File Browser,Unity打开文件选择器
下载地址:GitHub链接:
https://github.com/gkngkc/UnityStandaloneFileBrowser
简单的示例代码
using SFB;
using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class NewBehaviourScript : MonoBehaviour
{
   /// <summary>
   /// 用于显示图片的image
   /// </summary>
   public Image myimage;
   /// <summary>
   /// 图片选择按钮
   /// </summary>
   public Button btnSelect;
   /// <summary>
   /// 图片读取按钮
   /// </summary>
   public Button btnRead;
   private void Start()
   {
       btnSelect.onClick.AddListener(OnSelectImageButtonClicked);
       btnRead.onClick.AddListener(LoadAndDisplayBase64Image);
   }
   public void OnSelectImageButtonClicked()
   {
       //        // Open file with filter
       //        var extensions = new[] {
       //    new ExtensionFilter("Image Files", "png", "jpg", "jpeg" ),
       //    new ExtensionFilter("Sound Files", "mp3", "wav" ),
       //    new ExtensionFilter("All Files", "*" ),
       //};
       // Open file with filter
       var extensions = new[] {
   new ExtensionFilter("Image Files", "png", "jpg", "jpeg" )};
       string[] paths = StandaloneFileBrowser.OpenFilePanel("Select Image", "", extensions, false);
       if (paths.Length > 0)
       {
           string imagePath = paths[0];
           byte[] imageData = File.ReadAllBytes(imagePath);
           // 将图像数据转换为Base64字符串
           string base64ImageString = Convert.ToBase64String(imageData);
           // 将Base64字符串写入文本文件
           File.WriteAllText(Application.streamingAssetsPath + "/image_data.txt", base64ImageString);
       }
   }
   public void LoadAndDisplayBase64Image()
   {
       if (File.Exists(Application.streamingAssetsPath + "/image_data.txt"))
       {
           // 从文件读取Base64字符串
           string base64ImageString = File.ReadAllText(Application.streamingAssetsPath + "/image_data.txt");
           // 将Base64字符串解码回字节数组
           byte[] imageData = Convert.FromBase64String(base64ImageString);
           // 创建Texture2D并加载图像数据
           Texture2D texture = new Texture2D(100, 100);
           texture.LoadImage(imageData);
           // 将Texture2D转换为Sprite并设置到Image组件上
           myimage.sprite = CreateSpriteFromTexture(texture);
       }
   }
   private Sprite CreateSpriteFromTexture(Texture2D texture)
   {
       Rect rect = new Rect(0, 0, texture.width, texture.height);
       Vector2 pivot = new Vector2(0.5f, 0.5f);
       return Sprite.Create(texture, rect, pivot);
   }
}
场景布局
 
代码解析:
 引用命名空间: using SFB;
 两个按钮实现功能,一个image用于显示图片
        // Open file with filter
        var extensions = new[] {
    new ExtensionFilter("Image Files", "png", "jpg", "jpeg" )};
添加过滤条件,效果如下图,在右下角
 
多条件过滤,
        // Open file with filter
        var extensions = new[] {
            new ExtensionFilter("Image Files", "png", "jpg", "jpeg" ),
            new ExtensionFilter("Sound Files", "mp3", "wav" ),
            new ExtensionFilter("All Files", "*" ),
        };
效果在右下角
 
选择完毕之后,点击读取按钮会读取上面写如的base64文件,将图片显示到场景中















![[大模型]# Yi-6B-Chat Lora 微调](https://img-blog.csdnimg.cn/direct/13a4758156e6487fb0183110c0613100.png#pic_center)



