You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.7 KiB

  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System;
  5. using System.IO;
  6. using System.Collections.Generic;
  7. namespace ProGrids
  8. {
  9. [InitializeOnLoad]
  10. public static class pg_IconUtility
  11. {
  12. const string ICON_FOLDER_PATH = "ProGridsToggles";
  13. private static string iconFolderPath = "Assets/ProCore/ProGrids/GUI/ProGridsToggles/";
  14. static pg_IconUtility()
  15. {
  16. if(!Directory.Exists(iconFolderPath))
  17. {
  18. string folder = FindFolder(ICON_FOLDER_PATH);
  19. if(Directory.Exists(folder))
  20. iconFolderPath = folder;
  21. }
  22. }
  23. private static string FindFolder(string folder)
  24. {
  25. #if !UNITY_WEBPLAYER
  26. string single = folder.Replace("\\", "/").Substring(folder.LastIndexOf('/') + 1);
  27. string[] matches = Directory.GetDirectories("Assets/", single, SearchOption.AllDirectories);
  28. foreach(string str in matches)
  29. {
  30. string path = str.Replace("\\", "/");
  31. if(path.Contains(folder))
  32. {
  33. if(!path.EndsWith("/"))
  34. path += "/";
  35. return path;
  36. }
  37. }
  38. #endif
  39. Debug.LogError("Could not locate ProGrids/GUI/ProGridsToggles folder. The ProGrids folder may be moved, but the contents of ProGrids must remain unmodified.");
  40. return "";
  41. }
  42. public static Texture2D LoadIcon(string iconName)
  43. {
  44. string iconPath = string.Format("{0}{1}", iconFolderPath, iconName);
  45. if(!File.Exists(iconPath))
  46. {
  47. Debug.LogError("ProGrids failed to locate menu image: " + iconName + ".\nThis can happen if the GUI folder is moved or deleted. Deleting and re-importing ProGrids will fix this error.");
  48. return (Texture2D) null;
  49. }
  50. return LoadAssetAtPath<Texture2D>(iconPath);
  51. }
  52. static T LoadAssetAtPath<T>(string path) where T : UnityEngine.Object
  53. {
  54. return (T) AssetDatabase.LoadAssetAtPath(path, typeof(T));
  55. }
  56. }
  57. }
  58. #endif