Advertisement
Guest User

Untitled

a guest
Jun 8th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.09 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4.  
  5. public class ReferencesCleaner : EditorWindow
  6. {
  7.     private Material m_selectedMaterial;
  8.     private SerializedObject m_serializedObject;
  9.        
  10.     [MenuItem("Window/ReferencesCleaner")]
  11.     private static void Init()
  12.     {
  13.         GetWindow<ReferencesCleaner>("Ref. Cleaner");
  14.     }
  15.        
  16.     protected virtual void OnEnable()
  17.     {
  18.         GetSelectedMaterial();
  19.     }
  20.        
  21.     protected virtual void OnSelectionChange()
  22.     {
  23.         GetSelectedMaterial();
  24.     }
  25.        
  26.     protected virtual void OnProjectChange()
  27.     {
  28.         GetSelectedMaterial();
  29.     }
  30.        
  31.     protected virtual void OnGUI()
  32.     {
  33.         EditorGUIUtility.labelWidth = 200f;
  34.            
  35.         if (m_selectedMaterial == null)
  36.         {
  37.             EditorGUILayout.LabelField("No material selected");
  38.             if(GUILayout.Button("Remove all"))
  39.             {
  40.                 RemoveAllUnused();
  41.             }
  42.         }
  43.         else
  44.         {
  45.             EditorGUILayout.Space();
  46.             EditorGUILayout.LabelField("Selected material:",m_selectedMaterial.name);
  47.             EditorGUILayout.LabelField("Shader:", m_selectedMaterial.shader.name);
  48.             EditorGUILayout.Space();
  49.             EditorGUILayout.LabelField("Properties");
  50.                
  51.             m_serializedObject.Update();
  52.                
  53.             EditorGUI.indentLevel++;
  54.                
  55.             EditorGUILayout.LabelField("Textures");
  56.             EditorGUI.indentLevel++;
  57.             ProcessProperties("m_SavedProperties.m_TexEnvs");
  58.             EditorGUI.indentLevel--;
  59.                
  60.             EditorGUILayout.LabelField("Floats");
  61.             EditorGUI.indentLevel++;
  62.             ProcessProperties("m_SavedProperties.m_Floats");
  63.             EditorGUI.indentLevel--;
  64.                
  65.             EditorGUILayout.LabelField("Colors");
  66.             EditorGUI.indentLevel++;
  67.             ProcessProperties("m_SavedProperties.m_Colors");
  68.             EditorGUI.indentLevel--;
  69.                
  70.             EditorGUI.indentLevel--;
  71.         }
  72.            
  73.         EditorGUIUtility.labelWidth = 0;
  74.     }
  75.  
  76.     private static void RemoveAllUnused()
  77.     {
  78.         string[] materials = AssetDatabase.FindAssets("t:material");
  79.  
  80.         string path;
  81.         long i = 0;
  82.         long count = materials.LongLength;
  83.  
  84.         foreach (string mat in materials)
  85.         {
  86.             path = AssetDatabase.GUIDToAssetPath(mat);
  87.             EditorUtility.DisplayProgressBar("Remove all unused materials", path, (float)i / count);
  88.             RemoveAllUnused(AssetDatabase.LoadAssetAtPath<Material>(path));
  89.             i++;
  90.         }
  91.  
  92.         EditorUtility.ClearProgressBar();
  93.     }
  94.  
  95.     private static void RemoveAllUnused(Material m)
  96.     {
  97.         SerializedObject serializedObject = new SerializedObject(m);
  98.         RemoveAllUnusedProperties(m, serializedObject, "m_SavedProperties.m_TexEnvs");
  99.         RemoveAllUnusedProperties(m, serializedObject, "m_SavedProperties.m_Floats");
  100.         RemoveAllUnusedProperties(m, serializedObject, "m_SavedProperties.m_Colors");
  101.         serializedObject.ApplyModifiedProperties();
  102.     }
  103.  
  104.     private static void RemoveAllUnusedProperties(Material m, SerializedObject o, string path)
  105.     {
  106.         var properties = o.FindProperty(path);
  107.         if (properties != null && properties.isArray)
  108.         {
  109.             for (int i = 0; i < properties.arraySize; i++)
  110.             {
  111.                 string propName = properties.GetArrayElementAtIndex(i).displayName;
  112.                 bool exist = m.HasProperty(propName);
  113.  
  114.                 if(!exist)
  115.                 {
  116.                     properties.DeleteArrayElementAtIndex(i);
  117.                 }
  118.             }
  119.         }
  120.     }
  121.  
  122.     private void ProcessProperties(string path)
  123.     {
  124.         var properties = m_serializedObject.FindProperty(path);
  125.         if (properties != null && properties.isArray)
  126.         {
  127.             for (int i = 0; i < properties.arraySize; i++)
  128.             {
  129.                 string propName = properties.GetArrayElementAtIndex(i).displayName;
  130.                 bool exist = m_selectedMaterial.HasProperty(propName);
  131.                    
  132.                 if (exist)
  133.                 {
  134.                     EditorGUILayout.LabelField(propName, "Exist");
  135.                 }
  136.                 else
  137.                 {
  138.                     using (new EditorGUILayout.HorizontalScope())
  139.                     {
  140.                         EditorGUILayout.LabelField(propName, "Old reference", "CN StatusWarn");
  141.                         if (GUILayout.Button("Remove", GUILayout.Width(80f)))
  142.                         {
  143.                             properties.DeleteArrayElementAtIndex(i);
  144.                             m_serializedObject.ApplyModifiedProperties();
  145.                             GUIUtility.ExitGUI();
  146.                         }
  147.                     }
  148.                        
  149.                 }
  150.             }
  151.         }
  152.     }
  153.        
  154.     private void GetSelectedMaterial()
  155.     {
  156.         m_selectedMaterial = Selection.activeObject as Material;
  157.         if (m_selectedMaterial != null)
  158.         {
  159.             m_serializedObject = new SerializedObject(m_selectedMaterial);
  160.         }
  161.            
  162.         Repaint();
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement