Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vanifatovvlad committed Aug 19, 2023
0 parents commit 7099f0b
Show file tree
Hide file tree
Showing 12 changed files with 1,041 additions and 0 deletions.
708 changes: 708 additions & 0 deletions .editorconfig

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Editor.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions Editor/CodeWriter.SceneMaterialAnalyzer.Editor.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "CodeWriter.SceneMaterialAnalyzer.Editor",
"rootNamespace": "",
"references": [],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions Editor/CodeWriter.SceneMaterialAnalyzer.Editor.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

221 changes: 221 additions & 0 deletions Editor/SceneMaterialAnalyzerWindow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Linq;
using UnityEngine.SceneManagement;

public class SceneMaterialAnalyzerWindow : EditorWindow
{
[MenuItem("Window/Analysis/Scene Material Analyzer")]
public static void ShowSceneViewer()
{
var window = GetWindow<SceneMaterialAnalyzerWindow>();
window.titleContent = new GUIContent("Mat Analyzer");
window.Show();
}

private List<MaterialInfo> _materials;
private MaterialInfo _currentInfo;
private Vector2 _materialsScroll;
private Vector3 _rendersScroll;

private void OnEnable()
{
UnityEditor.SceneManagement.EditorSceneManager.sceneClosing += EditorSceneManager_sceneClosing;
UnityEditor.SceneManagement.EditorSceneManager.sceneOpened += EditorSceneManager_sceneOpened;
}

private void OnDisable()
{
UnityEditor.SceneManagement.EditorSceneManager.sceneClosing -= EditorSceneManager_sceneClosing;
UnityEditor.SceneManagement.EditorSceneManager.sceneOpened -= EditorSceneManager_sceneOpened;
}

private void EditorSceneManager_sceneClosing(Scene scene, bool removingScene)
{
_materials = null;
_currentInfo = null;
}

private void EditorSceneManager_sceneOpened(Scene scene, UnityEditor.SceneManagement.OpenSceneMode mode)
{
RefreshMaterials();
}

private void OnGUI()
{
using (new GUILayout.HorizontalScope(EditorStyles.toolbar))
{
if (GUILayout.Button("Refresh", EditorStyles.toolbarButton))
{
RefreshMaterials();
}

GUILayout.FlexibleSpace();
}

if (_materials == null)
{
using (new GUILayout.VerticalScope())
{
GUILayout.FlexibleSpace();
using (new GUILayout.HorizontalScope())
{
GUILayout.FlexibleSpace();

if (GUILayout.Button("Analyze", GUILayout.Width(160), GUILayout.Height(30)))
{
RefreshMaterials();
}

GUILayout.FlexibleSpace();
}

GUILayout.FlexibleSpace();
}

return;
}

using (new GUILayout.HorizontalScope())
{
using (new GUILayout.VerticalScope(GUI.skin.box, GUILayout.MaxWidth(position.width / 2f - 10)))
{
DrawMaterials();
}

using (new GUILayout.VerticalScope(GUI.skin.box, GUILayout.MaxWidth(position.width / 2f - 10)))
{
DrawSelectedMaterialInfo();
}
}
}

private void DrawMaterials()
{
GUILayout.Label("Materials in scene", EditorStyles.boldLabel);

var shader = default(Shader);

_materialsScroll = GUILayout.BeginScrollView(_materialsScroll);

foreach (var entry in _materials)
{
var prevColor = GUI.color;
GUI.color = entry == _currentInfo ? Color.cyan : prevColor;

if (shader != entry.Material.shader)
{
GUILayout.Space(5);
GUILayout.Label(entry.Material.shader.name, EditorStyles.miniLabel);
shader = entry.Material.shader;
}

if (GUILayout.Button(entry.Material.name, EditorStyles.objectField))
{
_currentInfo = entry;
}

GUI.color = prevColor;
}

GUILayout.EndScrollView();
}

private void DrawSelectedMaterialInfo()
{
GUILayout.Label("GameObjects with selected Material", EditorStyles.boldLabel);

if (_currentInfo == null)
{
if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.DownArrow &&
_materials.Count > 0)
{
_currentInfo = _materials[0];
Repaint();
Event.current.Use();
return;
}

EditorGUILayout.HelpBox("No material selected", MessageType.Info);
return;
}

if (Event.current.type == EventType.KeyDown)
{
if (Event.current.keyCode == KeyCode.DownArrow)
{
var currentInfoIndex = _materials.IndexOf(_currentInfo);
_currentInfo = _materials[(currentInfoIndex + 1) % _materials.Count];
Repaint();
Event.current.Use();
return;
}
else
{
var currentInfoIndex = _materials.IndexOf(_currentInfo);
currentInfoIndex = currentInfoIndex > 0 ? (currentInfoIndex - 1) : _materials.Count - 1;
_currentInfo = _materials[currentInfoIndex];
Repaint();
Event.current.Use();
return;
}
}

GUILayout.Label("Shader: " + _currentInfo.Material.shader.name, EditorStyles.largeLabel);

_rendersScroll = GUILayout.BeginScrollView(_rendersScroll);

foreach (var rend in _currentInfo.Renders)
{
var renderObject = rend.gameObject;

if (GUILayout.Button(renderObject.name, EditorStyles.objectField))
{
Selection.activeGameObject = renderObject;
EditorGUIUtility.PingObject(renderObject);
}
}

GUILayout.EndScrollView();
}

private void RefreshMaterials()
{
if (EditorApplication.isPlaying)
{
EditorApplication.isPaused = true;
}

_materials = FindObjectsOfType<Renderer>()
.SelectMany(o =>
{
return o.sharedMaterials.Select(m => new
{
renderer = o,
material = m,
});
})
.Where(o => o.material != null)
.GroupBy(o => o.material)
.Select(o =>
{
var renders = o.Select(p => p.renderer).ToList();
return new MaterialInfo(o.Key, renders);
})
.OrderBy(o => o.Material.shader.name)
.ToList();
}

private class MaterialInfo
{
public MaterialInfo(Material material, List<Renderer> renders)
{
Material = material;
Renders = renders;
}

public Material Material { get; }
public List<Renderer> Renders { get; }
}
}
12 changes: 12 additions & 0 deletions Editor/SceneMaterialAnalyzerWindow.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 CodeWriter Packages

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions LICENSE.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Scene Material Analyzer [![Github license](https://img.shields.io/github/license/codewriter-packages/SceneMaterialAnalyzer.svg?style=flat-square)](#) [![Unity 2020.1](https://img.shields.io/badge/Unity-2020.3+-2296F3.svg?style=flat-square)](#) ![GitHub package.json version](https://img.shields.io/github/package-json/v/codewriter-packages/SceneMaterialAnalyzer?style=flat-square)
_Tool for analyzing materials and shaders used in scenes_

[![Scene Material Analyzer Preview](https://user-images.githubusercontent.com/26966368/62204955-fc3f8b80-b396-11e9-8c05-4766d6b9b635.png)](#)

> Window/Analysis/Scene Material Analyzer
## How to Install
Minimal Unity Version is 2020.3.

Library distributed as git package ([How to install package from git URL](https://docs.unity3d.com/Manual/upm-ui-giturl.html))
<br>Git URL: `https://github.com/codewriter-packages/SceneMaterialAnalyzer.git`

## License

Scene Material Analyzer is [MIT licensed](./LICENSE.md).
7 changes: 7 additions & 0 deletions README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "com.codewriter.scene-material-analyzer",
"displayName": "Scene Material Analyer",
"description": "Tool for analyzing materials and shaders used in scenes ",
"version": "1.0.0",
"unity": "2020.3",
"license": "MIT",
"author": "CodeWriter (https://github.com/orgs/codewriter-packages)",
"homepage": "https://github.com/codewriter-packages/SceneMaterialAnalyzer#readme",
"dependencies": {}
}
7 changes: 7 additions & 0 deletions package.json.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 7099f0b

Please sign in to comment.