ImageLoader.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using UnityEngine;
  2. using NativeFileBrowser;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.IO;
  6. using ToneTuneToolkit.Common;
  7. public class ImageLoader : SingletonMaster<ImageLoader>
  8. {
  9. /// <summary>
  10. /// 弹窗获取图片路径
  11. /// </summary>
  12. /// <returns>图片路径</returns>
  13. public static string GetImagePath()
  14. {
  15. string title = "Select Image";
  16. ExtensionFilter[] extensions = new ExtensionFilter[]
  17. {
  18. new ExtensionFilter("Image Files", "png", "jpg", "jpeg"),
  19. new ExtensionFilter("JPG ", "jpg", "jpeg"),
  20. new ExtensionFilter("PNG ", "png"),
  21. };
  22. // 标题、类型筛选器、是否允许选择多个文件
  23. List<string> paths = StandaloneFileBrowser.OpenFilePanel(title, extensions, false).ToList();
  24. if (paths.Count == 0)
  25. {
  26. return null;
  27. }
  28. // Debug.Log(paths[0]);
  29. return paths[0];
  30. }
  31. /// <summary>
  32. /// 获取图片
  33. /// </summary>
  34. /// <param name="path"></param>
  35. /// <returns></returns>
  36. public static Texture2D GetImageTexture(string path)
  37. {
  38. if (!File.Exists(path))
  39. {
  40. return null;
  41. }
  42. byte[] bytes = File.ReadAllBytes(path);
  43. Texture2D texture2D = new Texture2D(2, 2);
  44. texture2D.LoadImage(bytes);
  45. texture2D.Apply();
  46. return texture2D;
  47. }
  48. }