| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- using UnityEngine;
- using NativeFileBrowser;
- using System.Collections.Generic;
- using System.Linq;
- using System.IO;
- using ToneTuneToolkit.Common;
- public class ImageLoader : SingletonMaster<ImageLoader>
- {
- /// <summary>
- /// 弹窗获取图片路径
- /// </summary>
- /// <returns>图片路径</returns>
- public static string GetImagePath()
- {
- string title = "Select Image";
- ExtensionFilter[] extensions = new ExtensionFilter[]
- {
- new ExtensionFilter("Image Files", "png", "jpg", "jpeg"),
- new ExtensionFilter("JPG ", "jpg", "jpeg"),
- new ExtensionFilter("PNG ", "png"),
- };
- // 标题、类型筛选器、是否允许选择多个文件
- List<string> paths = StandaloneFileBrowser.OpenFilePanel(title, extensions, false).ToList();
- if (paths.Count == 0)
- {
- return null;
- }
- // Debug.Log(paths[0]);
- return paths[0];
- }
- /// <summary>
- /// 获取图片
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public static Texture2D GetImageTexture(string path)
- {
- if (!File.Exists(path))
- {
- return null;
- }
- byte[] bytes = File.ReadAllBytes(path);
- Texture2D texture2D = new Texture2D(2, 2);
- texture2D.LoadImage(bytes);
- texture2D.Apply();
- return texture2D;
- }
- }
|