Синтаксис:
Используется csharp
using System;
using System.Linq;
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using Object = UnityEngine.Object;
namespace StarFightClient.Editor
{
public class UsagesFinder : EditorWindow
{
static bool _inProcess;
[MenuItem("GameObject/Find Usages #F12")]
[MenuItem("Custom/Find Game Object Usages")]
private static void OnSearchForReferences()
{
if (_inProcess) return;
_inProcess = true;
try
{
var what = new HashSet<Object>(
Selection.gameObjects
.SelectMany(x => x.GetComponents<Component>())
.Select(x => (Object)x)
.Concat(Selection.gameObjects.Select(x => (Object)x))
);
bool anyPersistant = what.Any(EditorUtility.IsPersistent);
Component[] components = Resources.FindObjectsOfTypeAll<MonoBehaviour>()
.Where(x => anyPersistant || !EditorUtility.IsPersistent(x))
.Except(what.OfType<MonoBehaviour>())
.ToArray();
var instance = CreateInstance<UsagesFinder>();
instance._process = SearchDependencies(components, what).GetEnumerator();
instance.Show();
instance.minSize = new Vector2();
instance.maxSize = new Vector2();
instance.position = new Rect(Screen.width / 2f, Screen.height / 2f, 0f, 0f);
}
catch
{
_inProcess = false;
throw;
}
}
private static IEnumerable SearchDependencies(Component[] components, HashSet<Object> what)
{
var objs = new HashSet<Object>();
for (int i = 0; i < components.Length; i++)
{
var x = components[i];
EditorUtility.DisplayProgressBar(
"Please wait",
"Searching dependencies, " + (i + 1) + " of " + components.Length + ": " + x.name,
(float)(i + 1) / (float)components.Length);
if (IterateProperties(new SerializedObject(x))
.Any(y => y.propertyType == SerializedPropertyType.ObjectReference
&& what.Contains(y.objectReferenceValue)))
{
if (objs.Add(x.gameObject) && objs.Count % 10 == 0)
{
Selection.objects = objs.ToArray();
}
}
yield return null;
}
Selection.objects = objs.ToArray();
EditorUtility.ClearProgressBar();
}
static IEnumerable<SerializedProperty> IterateProperties(SerializedObject obj)
{
var iterator = obj.GetIterator();
while (iterator.Next(true))
{
yield return iterator;
}
iterator.Dispose();
obj.Dispose();
}
IEnumerator _process;
protected void Update()
{
if (_process == null || !_process.MoveNext())
{
Close();
_inProcess = false;
}
}
}
}
using System.Linq;
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
using Object = UnityEngine.Object;
namespace StarFightClient.Editor
{
public class UsagesFinder : EditorWindow
{
static bool _inProcess;
[MenuItem("GameObject/Find Usages #F12")]
[MenuItem("Custom/Find Game Object Usages")]
private static void OnSearchForReferences()
{
if (_inProcess) return;
_inProcess = true;
try
{
var what = new HashSet<Object>(
Selection.gameObjects
.SelectMany(x => x.GetComponents<Component>())
.Select(x => (Object)x)
.Concat(Selection.gameObjects.Select(x => (Object)x))
);
bool anyPersistant = what.Any(EditorUtility.IsPersistent);
Component[] components = Resources.FindObjectsOfTypeAll<MonoBehaviour>()
.Where(x => anyPersistant || !EditorUtility.IsPersistent(x))
.Except(what.OfType<MonoBehaviour>())
.ToArray();
var instance = CreateInstance<UsagesFinder>();
instance._process = SearchDependencies(components, what).GetEnumerator();
instance.Show();
instance.minSize = new Vector2();
instance.maxSize = new Vector2();
instance.position = new Rect(Screen.width / 2f, Screen.height / 2f, 0f, 0f);
}
catch
{
_inProcess = false;
throw;
}
}
private static IEnumerable SearchDependencies(Component[] components, HashSet<Object> what)
{
var objs = new HashSet<Object>();
for (int i = 0; i < components.Length; i++)
{
var x = components[i];
EditorUtility.DisplayProgressBar(
"Please wait",
"Searching dependencies, " + (i + 1) + " of " + components.Length + ": " + x.name,
(float)(i + 1) / (float)components.Length);
if (IterateProperties(new SerializedObject(x))
.Any(y => y.propertyType == SerializedPropertyType.ObjectReference
&& what.Contains(y.objectReferenceValue)))
{
if (objs.Add(x.gameObject) && objs.Count % 10 == 0)
{
Selection.objects = objs.ToArray();
}
}
yield return null;
}
Selection.objects = objs.ToArray();
EditorUtility.ClearProgressBar();
}
static IEnumerable<SerializedProperty> IterateProperties(SerializedObject obj)
{
var iterator = obj.GetIterator();
while (iterator.Next(true))
{
yield return iterator;
}
iterator.Dispose();
obj.Dispose();
}
IEnumerator _process;
protected void Update()
{
if (_process == null || !_process.MoveNext())
{
Close();
_inProcess = false;
}
}
}
}