SCWindow.Project.Materials.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /// <summary>
  2. /// Shader Control - (C) Copyright 2016-2022 Ramiro Oliva (Kronnect)
  3. /// </summary>
  4. ///
  5. using UnityEngine;
  6. using UnityEditor;
  7. using System;
  8. using System.IO;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Text;
  12. namespace ShaderControl {
  13. public partial class SCWindow : EditorWindow {
  14. List<SCMaterial> projectMaterials;
  15. #region Materials handling
  16. void CleanMaterials(SCShader shader) {
  17. // Updates any material using this shader
  18. Shader shad = (Shader)AssetDatabase.LoadAssetAtPath<Shader>(shader.path);
  19. if (shad != null) {
  20. bool requiresSave = false;
  21. string[] matGUIDs = AssetDatabase.FindAssets("t:Material");
  22. foreach (string matGUID in matGUIDs) {
  23. string matPath = AssetDatabase.GUIDToAssetPath(matGUID);
  24. Material mat = (Material)AssetDatabase.LoadAssetAtPath<Material>(matPath);
  25. if (mat != null && mat.shader.name.Equals(shad.name)) {
  26. foreach (SCKeyword keyword in shader.keywords) {
  27. foreach (string matKeyword in mat.shaderKeywords) {
  28. if (matKeyword.Equals(keyword.name)) {
  29. if (!keyword.enabled && mat.IsKeywordEnabled(keyword.name)) {
  30. mat.DisableKeyword(keyword.name);
  31. EditorUtility.SetDirty(mat);
  32. requiresSave = true;
  33. }
  34. break;
  35. }
  36. }
  37. }
  38. }
  39. }
  40. if (requiresSave) {
  41. AssetDatabase.SaveAssets();
  42. AssetDatabase.Refresh();
  43. }
  44. }
  45. }
  46. void CleanAllMaterials() {
  47. if (!EditorUtility.DisplayDialog("Clean All Materials", "This option will scan all materials and will prune any disabled keywords. This option is provided to ensure no materials are referencing a disabled shader keyword.\n\nRemember: to disable keywords, first expand any shader from the list and uncheck the unwanted keywords (press 'Save' to modify the shader file and to clean any existing material that uses that specific shader).\n\nDo you want to continue?", "Yes", "Cancel")) {
  48. return;
  49. }
  50. try {
  51. for (int k = 0; k < shaders.Count; k++) {
  52. CleanMaterials(shaders[k]);
  53. }
  54. ScanProject();
  55. Debug.Log("Cleaning finished.");
  56. } catch (Exception ex) {
  57. Debug.LogError("Unexpected exception caught while cleaning materials: " + ex.Message);
  58. }
  59. }
  60. void PruneMaterials(string keywordName) {
  61. try {
  62. bool requiresSave = false;
  63. for (int s = 0; s < shaders.Count; s++) {
  64. SCShader shader = shaders[s];
  65. int materialCount = shader.materials.Count;
  66. for (int k = 0; k < materialCount; k++) {
  67. SCMaterial material = shader.materials[k];
  68. if (material.ContainsKeyword(keywordName)) {
  69. Material theMaterial = AssetDatabase.LoadAssetAtPath<Material>(shader.materials[k].path);
  70. if (theMaterial == null)
  71. continue;
  72. theMaterial.DisableKeyword(keywordName);
  73. EditorUtility.SetDirty(theMaterial);
  74. material.RemoveKeyword(keywordName);
  75. shader.RemoveKeyword(keywordName);
  76. requiresSave = true;
  77. }
  78. }
  79. }
  80. if (requiresSave) {
  81. AssetDatabase.SaveAssets();
  82. AssetDatabase.Refresh();
  83. }
  84. } catch (Exception ex) {
  85. Debug.Log("Unexpected exception caught while pruning materials: " + ex.Message);
  86. }
  87. }
  88. #endregion
  89. }
  90. }