Skip to content

Instantly share code, notes, and snippets.

@EliCDavis
Forked from ffyhlkain/ReferenceFinder.cs
Last active July 1, 2019 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EliCDavis/289a9b3e0d5a0245a4ae66730e6ec22b to your computer and use it in GitHub Desktop.
Save EliCDavis/289a9b3e0d5a0245a4ae66730e6ec22b to your computer and use it in GitHub Desktop.
A reference finder for assets in a #Unity3d project.
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;
using Object = UnityEngine.Object;
namespace XYFoundationEditor.Editor.ReferenceFinder
{
public class ReferenceFinder : EditorWindow
{
private string guidToFind = string.Empty;
private Object searchedObject;
private Dictionary<Object, int> referenceObjects = new Dictionary<Object, int>();
private Vector2 scrollPosition;
private Stopwatch searchTimer = new Stopwatch();
[MenuItem("Window/Reference Finder")]
static void Init()
{
GetWindow(typeof(ReferenceFinder), false, "Reference Finder");
}
void OnGUI()
{
if (EditorSettings.serializationMode == SerializationMode.ForceText)
{
DisplayMainMenu();
if ((searchedObject != null || guidToFind != "") && GUILayout.Button("Search"))
{
searchTimer.Reset();
searchTimer.Start();
referenceObjects.Clear();
var pathToAsset = AssetDatabase.GUIDToAssetPath(guidToFind);
if (!string.IsNullOrEmpty(pathToAsset))
{
searchedObject = AssetDatabase.LoadAssetAtPath<Object>(pathToAsset);
var allPathToAssetsList = new List<string>();
var allPrefabs = Directory.GetFiles(Application.dataPath, "*.prefab", SearchOption.AllDirectories);
allPathToAssetsList.AddRange(allPrefabs);
var allScenes = Directory.GetFiles(Application.dataPath, "*.unity", SearchOption.AllDirectories);
allPathToAssetsList.AddRange(allScenes);
var allMaterials = Directory.GetFiles(Application.dataPath, "*.mat", SearchOption.AllDirectories);
allPathToAssetsList.AddRange(allMaterials);
foreach (var assetPath in allPathToAssetsList)
{
var lines = File.ReadAllText(assetPath).Split('\n');
foreach (var line in lines)
{
if (line.Contains("guid:") && line.Contains(guidToFind))
{
var pathToReferenceAsset = assetPath.Replace(Application.dataPath, string.Empty);
pathToReferenceAsset = pathToReferenceAsset.Replace(".meta", string.Empty);
var path = "Assets" + pathToReferenceAsset;
path = path.Replace(@"\", "/"); // fix OSX/Windows path
var asset = AssetDatabase.LoadAssetAtPath<Object>(path);
if (asset != null)
{
if (!referenceObjects.ContainsKey(asset))
{
referenceObjects.Add(asset, 0);
}
referenceObjects[asset]++;
}
else
{
Debug.LogError(path + " could not be loaded");
}
}
}
}
searchTimer.Stop();
}
else
{
Debug.LogError("no asset found for GUID: " + guidToFind);
}
}
DisplayReferenceObjectList(referenceObjects);
}
else
{
DisplaySerializationWarning();
}
}
private void DisplaySerializationWarning()
{
GUILayout.Label("The Reference Finder relies on readable meta files (visible text serialization).\nPlease change your serialization mode in \"Edit/Project Settings/Editor/Version Control\"\n to \"Visisble Meta Files\" and \"Asset Serialization\" to \"Force Text\".");
}
private void DisplayReferenceObjectList(Dictionary<Object, int> referenceObjectsDictionary)
{
GUILayout.Label("Reference by: " + referenceObjectsDictionary.Count + " assets. (Last search duration: " + searchTimer.Elapsed + ")");
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
foreach (var referenceObject in referenceObjectsDictionary)
{
var referencingObject = referenceObject.Key;
var referenceCount = referenceObject.Value;
EditorGUILayout.ObjectField(referencingObject.name + " (" + referenceCount + ")", referencingObject, typeof(Object), false);
}
EditorGUILayout.EndScrollView();
}
private void DisplayMainMenu()
{
EditorGUILayout.BeginHorizontal();
searchedObject = EditorGUILayout.ObjectField(searchedObject != null ? searchedObject.name : "Drag & Drop Asset", searchedObject, typeof(Object), false);
if (GUILayout.Button("Get GUID") && searchedObject != null)
{
var pathToAsset = AssetDatabase.GetAssetPath(searchedObject);
guidToFind = AssetDatabase.AssetPathToGUID(pathToAsset);
}
EditorGUILayout.EndHorizontal();
var newGuidToFind = EditorGUILayout.TextField("GUID", guidToFind);
if (!guidToFind.Equals(newGuidToFind))
guidToFind = newGuidToFind;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment