浏览代码

Check : Better SceneObject Search for fields and game objects (#41)

* Tried to add Renderer fields into search, but atm not functional

* Another Try

* Extend Search to disabled objects

* Filter only visible

* Caching results

* Updated Changelog
/main
GitHub 3 年前
当前提交
fc3c7f3a
共有 5 个文件被更改,包括 85 次插入28 次删除
  1. 4
      CHANGELOG.md
  2. 97
      Editor/CheckWindow/CheckWindow.cs
  3. 6
      Editor/CheckWindow/Implementations/EmptyGameObjectCheck.cs
  4. 4
      Editor/CheckWindow/Implementations/InvalidTransformCheck.cs
  5. 2
      Editor/CheckWindow/Implementations/NullMaterialCheck.cs

4
CHANGELOG.md


## 2020.2.4
#### Added
#### Fixed
* CheckWindow: Better search for referenced objects in scene
#### Fixed

97
Editor/CheckWindow/CheckWindow.cs


EditorPrefs.SetBool(kShowIgnoredPreference, value);
}
}
int noticeCount;
int warningCount;
int errorCount;
enum SortMode
{

filterString = EditorGUILayout.DelayedTextField(filterString, EditorStyles.toolbarSearchField, GUILayout.Width(180));
showIgnored = GUILayout.Toggle(showIgnored, "Ignored", EditorStyles.toolbarButton);
showNotice = GUILayout.Toggle(showNotice, new GUIContent(m_Results.Where(o => o.result == CheckResult.Result.Notice).Count().ToString(), CheckResult.GetIcon(CheckResult.Result.Notice)), EditorStyles.toolbarButton);
showWarning = GUILayout.Toggle(showWarning, new GUIContent(m_Results.Where(o => o.result == CheckResult.Result.Warning).Count().ToString(), CheckResult.GetIcon(CheckResult.Result.Warning)), EditorStyles.toolbarButton);
showError = GUILayout.Toggle(showError, new GUIContent(m_Results.Where(o => o.result == CheckResult.Result.Failed).Count().ToString(), CheckResult.GetIcon(CheckResult.Result.Failed)), EditorStyles.toolbarButton);
showNotice = GUILayout.Toggle(showNotice, new GUIContent(noticeCount.ToString(), CheckResult.GetIcon(CheckResult.Result.Notice)), EditorStyles.toolbarButton);
showWarning = GUILayout.Toggle(showWarning, new GUIContent(warningCount.ToString(), CheckResult.GetIcon(CheckResult.Result.Warning)), EditorStyles.toolbarButton);
showError = GUILayout.Toggle(showError, new GUIContent(errorCount.ToString(), CheckResult.GetIcon(CheckResult.Result.Failed)), EditorStyles.toolbarButton);
}
using(new GUILayout.VerticalScope())

SortButton("Object", SortMode.ObjectName, GUILayout.Width(128));
SortButton("Message", SortMode.Message, GUILayout.ExpandWidth(true));
SortButton("Resolution", SortMode.Resolution, GUILayout.Width(128));
if(showIgnored)
SortButton("Ignored", SortMode.Ignored, GUILayout.Width(64));
SortButton("Ignored", SortMode.Ignored, GUILayout.Width(64));
GUILayout.Space(12);
}

bool odd = true;
foreach (var result in m_Results)
{
if (!showIgnored && IsIgnored(result) || result.mainObject == null)
continue;

EditorGUI.EndDisabledGroup();
if(showIgnored)
GUILayout.Space(18);
EditorGUI.BeginChangeCheck();
var ignored = GUILayout.Toggle(IsIgnored(result),"", EditorStyles.toggle ,GUILayout.Width(24));
if (EditorGUI.EndChangeCheck())
GUILayout.Space(18);
EditorGUI.BeginChangeCheck();
var ignored = GUILayout.Toggle(IsIgnored(result),"", EditorStyles.toggle ,GUILayout.Width(24));
if (EditorGUI.EndChangeCheck())
{
SetIgnored(result, ignored);
}
GUILayout.Space(18);
SetIgnored(result, ignored);
GUILayout.Space(18);
}
}

m_Results = results;
sortMode = SortMode.None;
noticeCount = m_Results.Where(o => o.result == CheckResult.Result.Notice).Count();
warningCount = m_Results.Where(o => o.result == CheckResult.Result.Warning).Count();
errorCount = m_Results.Where(o => o.result == CheckResult.Result.Failed).Count();
static class Styles
{

public class SceneObjects
{
public GameObject[] sceneObjects;
public GameObject[] allObjects;
public List<GameObject> referencedGameObjects;
public List<Component> referencedComponents;
public List<UnityEngine.Object> referencedObjects;

sceneObjects = GameObject.FindObjectsOfType<GameObject>();
allObjects = Resources.FindObjectsOfTypeAll<GameObject>();
allObjects = allObjects.Where(o => !PrefabUtility.IsPartOfPrefabAsset(o) && o.hideFlags == HideFlags.None).ToArray();
if (sceneObjects == null || sceneObjects.Length == 0)
if (allObjects == null || allObjects.Length == 0)
int count = sceneObjects.Length;
int count = allObjects.Length;
foreach (var sceneObject in sceneObjects)
foreach (var sceneObject in allObjects)
float progress = ++i / (float)count;
if (EditorUtility.DisplayCancelableProgressBar("Building Reference Table...", $"{sceneObject.name}", progress))
{

var components = sceneObject.GetComponents<Component>();
foreach (var component in components)
{
if (!(component is Transform))
if (component is Transform)
continue;
else if(component is Renderer)
{
var renderer = component as Renderer;
if (renderer.probeAnchor != null)
{
referencedComponents.Add(renderer.probeAnchor);
referencedGameObjects.Add(renderer.probeAnchor.gameObject);
}
foreach(var sharedMat in renderer.sharedMaterials)
{
referencedObjects.Add(sharedMat);
}
}
else
{
Type t = component.GetType();
FieldInfo[] fields = t.GetFields(BindingFlags.Public

referencedGameObjects.Add(go);
}
}
else if (f.FieldType == typeof(GameObject[]))
{
var golist = f.GetValue(component) as GameObject[];
foreach (var go in golist)
if (go != null && go != component.gameObject)
{
referencedGameObjects.Add(go);
}
}
else if (f.FieldType == typeof(Transform))
{
var tr = f.GetValue(component) as Transform;

referencedComponents.Add(tr);
else if (f.FieldType == typeof(Transform[]))
{
var trlist = f.GetValue(component) as Transform[];
foreach (var tr in trlist)
if (tr != null && tr.gameObject != component.gameObject)
{
referencedGameObjects.Add(tr.gameObject);
referencedComponents.Add(tr);
}
}
referencedGameObjects.Add(comp.gameObject);
}
else if (f.FieldType.IsSubclassOf(typeof(Component[])))
{
var complist = f.GetValue(component) as Component[];
foreach(var comp in complist)
if (comp != null && comp.gameObject != component.gameObject)
{
referencedGameObjects.Add(comp.gameObject);
referencedComponents.Add(comp);
}
}
else if (f.FieldType == typeof(UnityEvent))
{

6
Editor/CheckWindow/Implementations/EmptyGameObjectCheck.cs


{
try
{
int count = so.sceneObjects.Length;
int count = so.allObjects.Length;
foreach (var go in so.sceneObjects)
foreach (var go in so.allObjects)
{
float progress = ++i / count;
if (EditorUtility.DisplayCancelableProgressBar("Finding Empty Game Objects...", $"{go.name}", progress))

{
if (!so.referencedGameObjects.Contains(go) && !so.referencedComponents.Contains(go.transform))
{
var result = new CheckResult(this, CheckResult.Result.Notice, "Empty Static Game Object has no children and could be deleted if unused.", go);
var result = new CheckResult(this, CheckResult.Result.Notice, "Empty Static Game Object is not referenced, and has no children", go);
result.resolutionActionIndex = 1;
yield return result;
}

4
Editor/CheckWindow/Implementations/InvalidTransformCheck.cs


{
try
{
int count = so.sceneObjects.Length;
int count = so.allObjects.Length;
foreach (var go in so.sceneObjects)
foreach (var go in so.allObjects)
{
float progress = ++i / count;
if (EditorUtility.DisplayCancelableProgressBar("Finding Transforms...", $"{go.name}", progress))

2
Editor/CheckWindow/Implementations/NullMaterialCheck.cs


public override IEnumerable<CheckResult> GetResults(SceneObjects sceneObjects)
{
foreach(var obj in sceneObjects.sceneObjects)
foreach(var obj in sceneObjects.allObjects)
{
if(obj.TryGetComponent(out Renderer r))
{

正在加载...
取消
保存