-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Scene Material Analyzer [](#) [](#)  | ||
_Tool for analyzing materials and shaders used in scenes_ | ||
|
||
[](#) | ||
|
||
> 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). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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": {} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.