Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEditor;
- using System;
- public class ReferencesCleaner : EditorWindow
- {
- private Material m_selectedMaterial;
- private SerializedObject m_serializedObject;
- [MenuItem("Window/ReferencesCleaner")]
- private static void Init()
- {
- GetWindow<ReferencesCleaner>("Ref. Cleaner");
- }
- protected virtual void OnEnable()
- {
- GetSelectedMaterial();
- }
- protected virtual void OnSelectionChange()
- {
- GetSelectedMaterial();
- }
- protected virtual void OnProjectChange()
- {
- GetSelectedMaterial();
- }
- protected virtual void OnGUI()
- {
- EditorGUIUtility.labelWidth = 200f;
- if (m_selectedMaterial == null)
- {
- EditorGUILayout.LabelField("No material selected");
- if(GUILayout.Button("Remove all"))
- {
- RemoveAllUnused();
- }
- }
- else
- {
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Selected material:",m_selectedMaterial.name);
- EditorGUILayout.LabelField("Shader:", m_selectedMaterial.shader.name);
- EditorGUILayout.Space();
- EditorGUILayout.LabelField("Properties");
- m_serializedObject.Update();
- EditorGUI.indentLevel++;
- EditorGUILayout.LabelField("Textures");
- EditorGUI.indentLevel++;
- ProcessProperties("m_SavedProperties.m_TexEnvs");
- EditorGUI.indentLevel--;
- EditorGUILayout.LabelField("Floats");
- EditorGUI.indentLevel++;
- ProcessProperties("m_SavedProperties.m_Floats");
- EditorGUI.indentLevel--;
- EditorGUILayout.LabelField("Colors");
- EditorGUI.indentLevel++;
- ProcessProperties("m_SavedProperties.m_Colors");
- EditorGUI.indentLevel--;
- EditorGUI.indentLevel--;
- }
- EditorGUIUtility.labelWidth = 0;
- }
- private static void RemoveAllUnused()
- {
- string[] materials = AssetDatabase.FindAssets("t:material");
- string path;
- long i = 0;
- long count = materials.LongLength;
- foreach (string mat in materials)
- {
- path = AssetDatabase.GUIDToAssetPath(mat);
- EditorUtility.DisplayProgressBar("Remove all unused materials", path, (float)i / count);
- RemoveAllUnused(AssetDatabase.LoadAssetAtPath<Material>(path));
- i++;
- }
- EditorUtility.ClearProgressBar();
- }
- private static void RemoveAllUnused(Material m)
- {
- SerializedObject serializedObject = new SerializedObject(m);
- RemoveAllUnusedProperties(m, serializedObject, "m_SavedProperties.m_TexEnvs");
- RemoveAllUnusedProperties(m, serializedObject, "m_SavedProperties.m_Floats");
- RemoveAllUnusedProperties(m, serializedObject, "m_SavedProperties.m_Colors");
- serializedObject.ApplyModifiedProperties();
- }
- private static void RemoveAllUnusedProperties(Material m, SerializedObject o, string path)
- {
- var properties = o.FindProperty(path);
- if (properties != null && properties.isArray)
- {
- for (int i = 0; i < properties.arraySize; i++)
- {
- string propName = properties.GetArrayElementAtIndex(i).displayName;
- bool exist = m.HasProperty(propName);
- if(!exist)
- {
- properties.DeleteArrayElementAtIndex(i);
- }
- }
- }
- }
- private void ProcessProperties(string path)
- {
- var properties = m_serializedObject.FindProperty(path);
- if (properties != null && properties.isArray)
- {
- for (int i = 0; i < properties.arraySize; i++)
- {
- string propName = properties.GetArrayElementAtIndex(i).displayName;
- bool exist = m_selectedMaterial.HasProperty(propName);
- if (exist)
- {
- EditorGUILayout.LabelField(propName, "Exist");
- }
- else
- {
- using (new EditorGUILayout.HorizontalScope())
- {
- EditorGUILayout.LabelField(propName, "Old reference", "CN StatusWarn");
- if (GUILayout.Button("Remove", GUILayout.Width(80f)))
- {
- properties.DeleteArrayElementAtIndex(i);
- m_serializedObject.ApplyModifiedProperties();
- GUIUtility.ExitGUI();
- }
- }
- }
- }
- }
- }
- private void GetSelectedMaterial()
- {
- m_selectedMaterial = Selection.activeObject as Material;
- if (m_selectedMaterial != null)
- {
- m_serializedObject = new SerializedObject(m_selectedMaterial);
- }
- Repaint();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement