SelectionManager.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. Copyright (c) 2021 Omar Duarte
  3. Unauthorized copying of this file, via any medium is strictly prohibited.
  4. Writen by Omar Duarte, 2021.
  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. [UnityEditor.InitializeOnLoad]
  17. public static class SelectionManager
  18. {
  19. private static GameObject[] _topLevelSelection = new GameObject[0];
  20. private static GameObject[] _topLevelSelectionWithPrefabs = new GameObject[0];
  21. private static GameObject[] _selection = new GameObject[0];
  22. public static System.Action selectionChanged;
  23. static SelectionManager() => UnityEditor.Selection.selectionChanged += UpdateSelection;
  24. private static void UpdateSelection(System.Collections.Generic.List<GameObject> list,
  25. bool filteredByTopLevel, bool excludePrefabs)
  26. {
  27. var newSet = new System.Collections.Generic.HashSet<GameObject>(
  28. UnityEditor.Selection.GetFiltered<GameObject>(UnityEditor.SelectionMode.Editable
  29. | (excludePrefabs ? UnityEditor.SelectionMode.ExcludePrefab : UnityEditor.SelectionMode.Unfiltered)
  30. | (filteredByTopLevel ? UnityEditor.SelectionMode.TopLevel : UnityEditor.SelectionMode.Unfiltered)));
  31. if (newSet.Count == 0)
  32. {
  33. list.Clear();
  34. return;
  35. }
  36. var unselectedSet = new System.Collections.Generic.HashSet<GameObject>(list);
  37. unselectedSet.ExceptWith(newSet);
  38. foreach (var obj in unselectedSet) list.Remove(obj);
  39. newSet.ExceptWith(list);
  40. foreach (var obj in newSet) list.Add(obj);
  41. }
  42. public static void UpdateSelection()
  43. {
  44. var selectionOrderedTopLevel = new System.Collections.Generic.List<GameObject>(_topLevelSelection);
  45. var selectionOrdered = new System.Collections.Generic.List<GameObject>(_selection);
  46. var selectionOrderedTopLevelWithPrefabs
  47. = new System.Collections.Generic.List<GameObject>(_topLevelSelectionWithPrefabs);
  48. UpdateSelection(selectionOrderedTopLevel, true, true);
  49. UpdateSelection(selectionOrdered, false, true);
  50. UpdateSelection(selectionOrderedTopLevelWithPrefabs, true, false);
  51. _selection = selectionOrdered.ToArray();
  52. _topLevelSelection = selectionOrderedTopLevel.ToArray();
  53. _topLevelSelectionWithPrefabs = selectionOrderedTopLevelWithPrefabs.ToArray();
  54. if (selectionChanged != null) selectionChanged();
  55. }
  56. public static GameObject[] GetSelection(bool filteredByTopLevel)
  57. => filteredByTopLevel ? _topLevelSelection : _selection;
  58. public static GameObject[] topLevelSelection => _topLevelSelection;
  59. public static GameObject[] topLevelSelectionWithPrefabs => _topLevelSelectionWithPrefabs;
  60. public static GameObject[] selection => _selection;
  61. public static GameObject[] GetSelectionPrefabs()
  62. {
  63. var result = new System.Collections.Generic.List<GameObject>();
  64. foreach (var obj in topLevelSelectionWithPrefabs)
  65. {
  66. if (obj == null) continue;
  67. var assetType = UnityEditor.PrefabUtility.GetPrefabAssetType(obj);
  68. if (assetType == UnityEditor.PrefabAssetType.NotAPrefab) continue;
  69. var prefab = obj;
  70. if (UnityEditor.PrefabUtility.IsAnyPrefabInstanceRoot(obj))
  71. {
  72. prefab = assetType == UnityEditor.PrefabAssetType.Variant ? obj
  73. : UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(obj);
  74. }
  75. if (result.Contains(prefab)) continue;
  76. result.Add(prefab);
  77. }
  78. return result.ToArray();
  79. }
  80. }
  81. }