InTerra_DeleteMaterials.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEditor;
  4. namespace InTerra
  5. {
  6. public class InTerra_DeleteMaterials : EditorWindow
  7. {
  8. static string baseName;
  9. static Vector2 ScrollPosMats;
  10. static Material selectedMaterial = null;
  11. static List<Material> deleteMaterials;
  12. static List<Renderer> materialsRenderes;
  13. public static void OpenWindow(string materialsBaseName, List<Material> relatedMaterials, List<Renderer> renderes)
  14. {
  15. InTerra_DeleteMaterials window = GetWindow<InTerra_DeleteMaterials>(true, "Delete Materials and Replace them...", true);
  16. Vector2 size = new Vector2(500, 220);
  17. window.minSize = size;
  18. window.maxSize = size;
  19. InTerra_Data.CenterOnMainWin(window);
  20. deleteMaterials = relatedMaterials;
  21. baseName = materialsBaseName;
  22. materialsRenderes = renderes;
  23. }
  24. void OnGUI()
  25. {
  26. GUIStyle warningLabelStyle;
  27. warningLabelStyle = new GUIStyle(GUI.skin.label);
  28. warningLabelStyle.wordWrap = true;
  29. warningLabelStyle.alignment = TextAnchor.MiddleLeft;
  30. warningLabelStyle.richText = true;
  31. using (new GUILayout.HorizontalScope())
  32. {
  33. using (new GUILayout.VerticalScope())
  34. {
  35. EditorGUILayout.LabelField("Following Materials will be deleted:");
  36. using (new GUILayout.HorizontalScope(EditorStyles.helpBox))
  37. {
  38. ScrollPosMats = EditorGUILayout.BeginScrollView(ScrollPosMats, GUILayout.Height(100));
  39. if (deleteMaterials != null)
  40. {
  41. foreach (var mat in deleteMaterials)
  42. {
  43. if (mat != selectedMaterial)
  44. {
  45. GUILayout.Label(new GUIContent() { text = mat.name, tooltip = AssetDatabase.GetAssetPath(mat) }, GUILayout.MinWidth(30));
  46. }
  47. }
  48. }
  49. else
  50. {
  51. Close();
  52. }
  53. EditorGUILayout.EndScrollView();
  54. }
  55. }
  56. }
  57. EditorGUILayout.Space();
  58. using (new GUILayout.VerticalScope(EditorStyles.helpBox))
  59. {
  60. EditorGUILayout.LabelField("Select Material for Replacing:");
  61. selectedMaterial = (Material)EditorGUILayout.ObjectField(selectedMaterial, typeof(Material), false, GUILayout.MinWidth(100), GUILayout.Height(22));
  62. string replaceMatBaseName = selectedMaterial == null ? "" : selectedMaterial.GetTag("BaseName", false);
  63. if (replaceMatBaseName.Length > 0 && replaceMatBaseName != baseName)
  64. {
  65. EditorUtility.DisplayDialog("Selected Material", "To avoid inconsistency you cannot choose Material that belongs to another set of Materials with different Base name, please choose a different one.", "Ok");
  66. selectedMaterial = null;
  67. }
  68. }
  69. EditorGUILayout.Space();
  70. using (new GUILayout.HorizontalScope())
  71. {
  72. if(selectedMaterial == null)
  73. {
  74. GUI.enabled = false;
  75. }
  76. if (GUILayout.Button(" OK "))
  77. {
  78. if (EditorUtility.DisplayDialog("Delete Materials", "All Materials with the base name " + baseName + " will be deleted and to all Renderers using these Materials will be asigned the Material " + selectedMaterial.name + ". This step cannot be undo.", "Ok", "Cancel"))
  79. {
  80. foreach (var mat in deleteMaterials)
  81. {
  82. if (mat != selectedMaterial)
  83. {
  84. AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(mat));
  85. AssetDatabase.Refresh();
  86. }
  87. }
  88. deleteMaterials.Clear();
  89. InTerra_MaterialManager.CreateRelatedMaterialsList(baseName, deleteMaterials);
  90. foreach (Renderer rend in materialsRenderes)
  91. {
  92. rend.sharedMaterial = selectedMaterial;
  93. }
  94. }
  95. selectedMaterial = null;
  96. Close();
  97. }
  98. GUI.enabled = true;
  99. if (GUILayout.Button(" Cancel "))
  100. {
  101. selectedMaterial = null;
  102. Close();
  103. }
  104. }
  105. }
  106. }
  107. }