public void Process() { if (this.goal == null) { Log.Warning("[MaterialMapper] : No goal shader selected."); return; } ProxyEditor.CreateAsset(new Material(this.goal), "Assets/Temporary.mat"); string goalHeader = File.Find("Temporary.mat").ReadText().Cut("%YAML", "m_SavedProperties"); string goalName = goalHeader.Parse("m_Name:", "\n"); ProxyEditor.DeleteAsset("Assets/Temporary.mat"); List <string> guids = new List <string>(); List <FileData> matching = new List <FileData>(); FileData[] allMaterials = File.FindAll("*.mat"); foreach (var shader in this.shaders) { if (shader == null) { continue; } var path = File.GetAssetPath(shader); guids.AddNew(ProxyEditor.GetGUID(path)); } ProxyEditor.StartAssetEditing(); foreach (FileData materialFile in allMaterials) { Material material = materialFile.GetAsset <Material>(); string text = materialFile.ReadText(); foreach (string guid in guids) { string idLine = "guid: " + guid; bool repair = material.shader.name.Contains("Hidden/InternalErrorShader"); if (repair || text.Contains(idLine)) { string header = text.Cut("%YAML", "m_SavedProperties"); string name = header.Parse("m_Name:", "\n"); foreach (ShaderInfo info in this.keywordMap) { if (!info.mapTo.ContainsAny("[Ignore]", "[No Matching]")) { text = text.Replace(" " + info.name, " " + info.mapTo); } } text = text.Replace(header, goalHeader); text = text.Replace(goalName, name); materialFile.Write(text); matching.AddNew(materialFile); break; } } } ProxyEditor.StopAssetEditing(); MaterialCleaner.Clean(matching.ToArray()); Log.Show("[MaterialMapper] : " + matching.Count + " materials modified."); }
void OnContextMenuEvent(Contents contents, Event ev) { if (ev.type == EventType.MouseUp && ev.button == 1) { if (view.GetViewRect().Contains(ev.mousePosition) != false) { int selectedCount = view.GetSelectedCount(); if (selectedCount > 0) { var contextMenu = new GenericMenu(); if (selectedCount == 1) { contextMenu.AddItem(new GUIContent("Open"), false, () => { Element element = view.FirstSelectedElements((x => x.CanOpenAsset())); if (element.directory != false) { view.SetExpanded(element.id, !view.IsExpanded(element.id)); } else { AssetDatabase.OpenAsset(AssetDatabase.LoadMainAssetAtPath(element.path)); } }); contextMenu.AddItem(new GUIContent("Show in Explorer"), false, () => { Element element = view.FirstSelectedElements((x => true)); EditorUtility.RevealInFinder(element.path); }); } if (selectedCount == 1 && view.ContainsSeelctedElements(true, x => x.directory) != false) { contextMenu.AddItem(new GUIContent("Filter Path"), false, () => { FilterPath(); }); } contextMenu.AddItem(new GUIContent("Copy Path"), false, () => { var elements = view.SelectSelectedElements(x => x.path); var builder = new System.Text.StringBuilder(); foreach (var element in elements) { builder.AppendLine(element); } EditorGUIUtility.systemCopyBuffer = builder.ToString(); }); contextMenu.AddItem(new GUIContent("Copy Guid"), false, () => { var elements = view.SelectSelectedElements(x => x.guid); var builder = new System.Text.StringBuilder(); foreach (var element in elements) { builder.AppendLine(element); } EditorGUIUtility.systemCopyBuffer = builder.ToString(); }); contextMenu.AddItem(new GUIContent("Export Package/Select Only"), false, () => { var assetPaths = view.SelectSelectedElements(x => x.IsFile(), x => x.path).ToArray(); if (assetPaths.Length > 0) { string directory = System.IO.Path.GetFullPath("Assets/../"); string fileName = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); string savePath = EditorUtility.SaveFilePanel("Export Package", directory, fileName, "unitypackage"); if (string.IsNullOrEmpty(savePath) == false) { AssetDatabase.ExportPackage(assetPaths, savePath, ExportPackageOptions.Default | ExportPackageOptions.Interactive); } } }); contextMenu.AddItem(new GUIContent("Export Package/Include Dependencies"), false, () => { var assetPaths = view.SelectSelectedElements(x => x.IsFile(), x => x.path).ToArray(); if (assetPaths.Length > 0) { string directory = System.IO.Path.GetFullPath("Assets/../"); string fileName = System.DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss"); string savePath = EditorUtility.SaveFilePanel("Export Package", directory, fileName, "unitypackage"); if (string.IsNullOrEmpty(savePath) == false) { AssetDatabase.ExportPackage(assetPaths, savePath, ExportPackageOptions.IncludeDependencies | ExportPackageOptions.Interactive); } } }); if (selectedCount == 1) { contextMenu.AddItem(new GUIContent("Ping"), false, () => { Element element = view.FirstSelectedElements(x => x.CanPingObject()); element?.PingObject(true); }); contextMenu.AddItem(new GUIContent("Active"), false, () => { Element element = view.FirstSelectedElements(x => x.CanActiveObject()); element?.ActiveObject(true); }); } contextMenu.AddItem(new GUIContent("Search/Check Missing"), false, () => { var elements = view.SelectSelectedElements(x => x.guid); contents?.SearchAssets(elements, SearchType.kCheckMissing); }); contextMenu.AddItem(new GUIContent("Search/Trace Precedents"), false, () => { var elements = view.SelectSelectedElements(x => x.guid); contents?.SearchAssets(elements, SearchType.kTracePrecedents); }); contextMenu.AddItem(new GUIContent("Search/Trace Dependents"), false, () => { var elements = view.SelectSelectedElements(x => x.guid); contents?.SearchAssets(elements, SearchType.kTraceDependents); }); contextMenu.AddItem(new GUIContent("Search/Trace Precedents new tab"), false, () => { var elements = view.SelectSelectedElements(x => x.guid); contents?.OpenSearchAssets(elements, SearchType.kTracePrecedents); }); contextMenu.AddItem(new GUIContent("Search/Trace Dependents new tab"), false, () => { var elements = view.SelectSelectedElements(x => x.guid); contents?.OpenSearchAssets(elements, SearchType.kTraceDependents); }); if (view.ContainsSeelctedElements(AssetType.kMaterial, x => x.type) != false) { contextMenu.AddItem(new GUIContent("Material Cleaner"), false, () => { var elements = view.SelectSelectedElements(x => x.guid); MaterialCleaner.Clean(elements); }); } contextMenu.ShowAsContext(); ev.Use(); } } } }
private void PopupTools(Rect position) { var menu = new GenericMenu(); int unboundTextures = MaterialCleaner.CountUnusedProperties(MaterialCleaner.CleanPropertyType.Texture, Materials); int unboundProperties = MaterialCleaner.CountAllUnusedProperties(Materials); List <string> unusedTextures = new List <string>(); MainGroup.FindUnusedTextures(unusedTextures, true); if (unboundTextures > 0 && !IsLockedMaterial) { menu.AddItem(new GUIContent($"Unbound Textures: {unboundTextures}/List in console"), false, delegate() { MaterialCleaner.ListUnusedProperties(MaterialCleaner.CleanPropertyType.Texture, Materials); }); menu.AddItem(new GUIContent($"Unbound Textures: {unboundTextures}/Remove"), false, delegate() { MaterialCleaner.RemoveUnusedProperties(MaterialCleaner.CleanPropertyType.Texture, Materials); }); } else { menu.AddDisabledItem(new GUIContent($"Unbound textures: 0")); } if (unusedTextures.Count > 0 && !IsLockedMaterial) { menu.AddItem(new GUIContent($"Unused Textures: {unusedTextures.Count}/List in console"), false, delegate() { Out("Unused textures", unusedTextures.Select(s => $"↳{s}")); }); menu.AddItem(new GUIContent($"Unused Textures: {unusedTextures.Count}/Remove"), false, delegate() { foreach (string t in unusedTextures) { if (PropertyDictionary.ContainsKey(t)) { PropertyDictionary[t].MaterialProperty.textureValue = null; } } }); } else { menu.AddDisabledItem(new GUIContent($"Unused textures: 0")); } if (unboundProperties > 0 && !IsLockedMaterial) { menu.AddItem(new GUIContent($"Unbound properties: {unboundProperties}/List in console"), false, delegate() { MaterialCleaner.ListUnusedProperties(MaterialCleaner.CleanPropertyType.Texture, Materials); MaterialCleaner.ListUnusedProperties(MaterialCleaner.CleanPropertyType.Float, Materials); MaterialCleaner.ListUnusedProperties(MaterialCleaner.CleanPropertyType.Color, Materials); }); menu.AddItem(new GUIContent($"Unbound properties: {unboundProperties}/Remove"), false, delegate() { MaterialCleaner.RemoveAllUnusedProperties(MaterialCleaner.CleanPropertyType.Texture, Materials); }); } else { menu.AddDisabledItem(new GUIContent($"Unbound properties: 0")); } menu.DropDown(position); }