GetAsset.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using static UnityEditor.ShaderGraph.Internal.KeywordDependentCollection;
  6. using UnityEngine.Windows;
  7. /// <summary>
  8. /// Resources Objects other than directories can be accessed. In fact, you can also access objects in the Resources directory.
  9. /// </summary>
  10. public static class GetAsset
  11. {
  12. //=================================================================================
  13. //Single load
  14. //=================================================================================
  15. /// <summary>
  16. /// Set the file path (including the extension from Assets) and type, and load the Object. Returns Null if not present
  17. /// </summary>
  18. public static T Load<T>(string path) where T : Object
  19. {
  20. return AssetDatabase.LoadAssetAtPath<T>(path);
  21. }
  22. /// <summary>
  23. /// Set the file path (from Assets, including the extension) and load the Object. Returns Null if not present
  24. /// </summary>
  25. public static Object Load(string path)
  26. {
  27. return Load<Object>(path);
  28. }
  29. //=================================================================================
  30. //multiple loads
  31. //=================================================================================
  32. /// <summary>
  33. /// Set the directory path(from Assets) and type, and load the Object.Returns an empty List if it does not exist
  34. /// </summary>
  35. public static List<T> LoadAll<T>(string directoryPath) where T : Object
  36. {
  37. List<T> assetList = new List<T>();
  38. //Get all files in the specified directory (including child directories)
  39. string[] filePathArray = System.IO.Directory.GetFiles(directoryPath, "*", SearchOption.AllDirectories);
  40. //Add only assets from the acquired files to the list
  41. foreach (string filePath in filePathArray)
  42. {
  43. T asset = Load<T>(filePath);
  44. if (asset != null)
  45. {
  46. assetList.Add(asset);
  47. }
  48. }
  49. return assetList;
  50. }
  51. /// <summary>
  52. /// Set the directory path (from Assets) and read the Object. Returns an empty List if it does not exist
  53. /// </summary>
  54. public static List<Object> LoadAll(string directoryPath)
  55. {
  56. return LoadAll<Object>(directoryPath);
  57. }
  58. }