暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RemoveComponentUtils.cs 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using JetBrains.Annotations;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics.CodeAnalysis;
  5. using System.Linq;
  6. using UnityEngine;
  7. using UnityEngine.Rendering;
  8. namespace UnityEditor.Rendering
  9. {
  10. static class RemoveComponentUtils
  11. {
  12. public static IEnumerable<Component> ComponentDependencies([DisallowNull] Component component)
  13. {
  14. if (component == null)
  15. yield break;
  16. var componentType = component.GetType();
  17. foreach (var c in component.gameObject.GetComponents<Component>())
  18. {
  19. foreach (var rc in c.GetType().GetCustomAttributes(typeof(RequireComponent), true).Cast<RequireComponent>())
  20. {
  21. if (rc.m_Type0 == componentType || rc.m_Type1 == componentType || rc.m_Type2 == componentType)
  22. {
  23. yield return c;
  24. break;
  25. }
  26. }
  27. }
  28. }
  29. public static bool CanRemoveComponent([DisallowNull] Component component, IEnumerable<Component> dependencies)
  30. {
  31. if (dependencies.Count() == 0)
  32. return true;
  33. Component firstDependency = dependencies.First();
  34. string error = $"Can't remove {component.GetType().Name} because {firstDependency.GetType().Name} depends on it.";
  35. EditorUtility.DisplayDialog("Can't remove component", error, "Ok");
  36. return false;
  37. }
  38. public static bool RemoveComponent([DisallowNull] Component component, IEnumerable<Component> dependencies)
  39. {
  40. var additionalDatas = dependencies
  41. .Where(c => c != component && c is IAdditionalData)
  42. .ToList();
  43. if (!RemoveComponentUtils.CanRemoveComponent(component, dependencies.Where(c => !additionalDatas.Contains(c))))
  44. return false;
  45. bool removed = true;
  46. var isAssetEditing = EditorUtility.IsPersistent(component);
  47. try
  48. {
  49. if (isAssetEditing)
  50. {
  51. AssetDatabase.StartAssetEditing();
  52. }
  53. Undo.SetCurrentGroupName($"Remove {component.GetType()} and additional data components");
  54. // The components with RequireComponent(typeof(T)) also contain the AdditionalData attribute, proceed with the remove
  55. foreach (var additionalDataComponent in additionalDatas)
  56. {
  57. if (additionalDataComponent != null)
  58. {
  59. Undo.DestroyObjectImmediate(additionalDataComponent);
  60. }
  61. }
  62. Undo.DestroyObjectImmediate(component);
  63. }
  64. catch
  65. {
  66. removed = false;
  67. }
  68. finally
  69. {
  70. if (isAssetEditing)
  71. {
  72. AssetDatabase.StopAssetEditing();
  73. }
  74. }
  75. return removed;
  76. }
  77. public static void RemoveComponent([DisallowNull] Component comp)
  78. {
  79. var dependencies = RemoveComponentUtils.ComponentDependencies(comp);
  80. if (!RemoveComponent(comp, dependencies))
  81. {
  82. //preserve built-in behavior
  83. if (RemoveComponentUtils.CanRemoveComponent(comp, dependencies))
  84. Undo.DestroyObjectImmediate(comp);
  85. }
  86. }
  87. }
  88. }