DropUtils.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. Copyright (c) 2020 Omar Duarte
  3. Unauthorized copying of this file, via any medium is strictly prohibited.
  4. Writen by Omar Duarte, 2020.
  5. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  6. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  7. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  8. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  9. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  10. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  11. THE SOFTWARE.
  12. */
  13. using UnityEngine;
  14. namespace PluginMaster
  15. {
  16. public static class DropUtils
  17. {
  18. public struct DroppedItem
  19. {
  20. public GameObject obj;
  21. public DroppedItem(GameObject obj) => this.obj = obj;
  22. }
  23. public static DroppedItem[] GetDirPrefabs(string dirPath)
  24. {
  25. var filePaths = System.IO.Directory.GetFiles(dirPath, "*.prefab");
  26. var subItemList = new System.Collections.Generic.List<DroppedItem>();
  27. var dirName = dirPath.Substring(Mathf.Max(dirPath.LastIndexOf('/'), dirPath.LastIndexOf('\\')) + 1);
  28. foreach (var filePath in filePaths)
  29. {
  30. DroppedItem item;
  31. item.obj = UnityEditor.AssetDatabase.LoadAssetAtPath<GameObject>(filePath);
  32. var prefabType = UnityEditor.PrefabUtility.GetPrefabAssetType(item.obj);
  33. if (prefabType != UnityEditor.PrefabAssetType.Regular
  34. && prefabType != UnityEditor.PrefabAssetType.Variant) continue;
  35. subItemList.Add(item);
  36. }
  37. if (PaletteManager.selectedPalette.brushCreationSettings.includeSubfolders)
  38. {
  39. var subdirPaths = System.IO.Directory.GetDirectories(dirPath);
  40. foreach (var subdirPath in subdirPaths) subItemList.AddRange(GetDirPrefabs(subdirPath));
  41. }
  42. return subItemList.ToArray();
  43. }
  44. public static DroppedItem[] GetDroppedPrefabs()
  45. {
  46. var itemList = new System.Collections.Generic.List<DroppedItem>();
  47. for (int i = 0; i < UnityEditor.DragAndDrop.objectReferences.Length; ++i)
  48. {
  49. var objRef = UnityEditor.DragAndDrop.objectReferences[i];
  50. if (objRef is GameObject)
  51. {
  52. if (objRef == null) continue;
  53. if (UnityEditor.PrefabUtility.GetPrefabAssetType(objRef) == UnityEditor.PrefabAssetType.NotAPrefab)
  54. continue;
  55. var path = UnityEditor.PrefabUtility.GetPrefabAssetPathOfNearestInstanceRoot(objRef);
  56. if (path == string.Empty) continue;
  57. var prefab = objRef as GameObject;
  58. var prefabInstance = UnityEditor.PrefabUtility.GetNearestPrefabInstanceRoot(objRef) as GameObject;
  59. if (prefabInstance != null)
  60. {
  61. var assetType = UnityEditor.PrefabUtility.GetPrefabAssetType(prefabInstance);
  62. if (assetType == UnityEditor.PrefabAssetType.NotAPrefab
  63. || assetType == UnityEditor.PrefabAssetType.NotAPrefab) continue;
  64. if (assetType == UnityEditor.PrefabAssetType.Variant) prefab = prefabInstance;
  65. else prefab = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(prefabInstance);
  66. }
  67. itemList.Add(new DroppedItem(prefab));
  68. }
  69. else
  70. {
  71. var path = UnityEditor.DragAndDrop.paths[i];
  72. if (objRef is UnityEditor.DefaultAsset && UnityEditor.AssetDatabase.IsValidFolder(path))
  73. itemList.AddRange(GetDirPrefabs(path));
  74. }
  75. }
  76. return itemList.ToArray();
  77. }
  78. public static DroppedItem[] GetFolderItems()
  79. {
  80. DroppedItem[] items = null;
  81. var folder = UnityEditor.EditorUtility.OpenFolderPanel("Add Prefabs in folder:", Application.dataPath, "Assets");
  82. if (folder.Contains(Application.dataPath))
  83. {
  84. folder = folder.Replace(Application.dataPath, "Assets");
  85. items = GetDirPrefabs(folder);
  86. if (items.Length == 0)
  87. UnityEditor.EditorUtility.DisplayDialog("No Prefabs found", "No prefabs found in folder", "Ok");
  88. }
  89. else if (folder != string.Empty)
  90. UnityEditor.EditorUtility.DisplayDialog("Folder Error", "Folder must be under Assets folder", "Ok");
  91. return items;
  92. }
  93. }
  94. }