Skip to content

Instantly share code, notes, and snippets.

@arakaki-asdf
Last active July 4, 2022 02:05
Show Gist options
  • Save arakaki-asdf/840de3e20ef8c7ab20c580b6c4df9550 to your computer and use it in GitHub Desktop.
Save arakaki-asdf/840de3e20ef8c7ab20c580b6c4df9550 to your computer and use it in GitHub Desktop.
Unity GUID検索 (git grepを使用)
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
public class SearchGUID : EditorWindow
{
UnityEngine.Object targetObject = null;
List<AssetData> list = new List<AssetData>();
HashSet<string> hash = new HashSet<string>();
Vector2 scrollPosition = Vector2.zero;
void OnGUI()
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
targetObject = EditorGUILayout.ObjectField(targetObject, typeof(UnityEngine.Object), true);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("search"))
{
if (targetObject == null)
return;
Search(targetObject);
}
if (GUILayout.Button("clear"))
{
Clear();
}
EditorGUILayout.EndHorizontal();
foreach (var target in list)
{
var iconSize = EditorGUIUtility.GetIconSize();
EditorGUIUtility.SetIconSize(Vector2.one * 16);
var content = new GUIContent(target.Name, EditorGUIUtility.ObjectContent(target.Obj, target.Obj.GetType()).image);
if (GUILayout.Button(content, "Label"))
{
Selection.objects = new[] { target.Obj };
}
EditorGUIUtility.SetIconSize(iconSize);
}
GUILayout.EndScrollView();
}
[MenuItem("Tools/SearchGUID")]
public static void Create()
{
EditorWindow.GetWindow<SearchGUID>();
}
string DoCommand(string args)
{
try
{
using (var p = new Process())
{
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c " + args;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.EnableRaisingEvents = true;
if (p.Start())
{
return p.StandardOutput.ReadToEnd().Trim();
}
}
}
catch (System.Exception err)
{
UnityEngine.Debug.LogError(err);
}
return "unknown";
}
void Clear()
{
list.Clear();
hash.Clear();
}
void Search(UnityEngine.Object obj)
{
var targetPath = AssetDatabase.GetAssetPath(obj);
if (targetPath.EndsWith(".meta")) return;
Clear();
var target = new AssetData(targetPath);
var result = DoCommand($"git grep {target.Guid}");
foreach (var x in result.Split('\n'))
{
if (string.IsNullOrEmpty(x)) continue;
var index = x.IndexOf(':');
if (index == -1)
{
UnityEngine.Debug.Log($"<color=red>{x}: not found.</color>");
continue;
}
var path = x.Substring(0, index);
if (hash.Add(path))
{
var asset = new AssetData(path);
if (asset.Exist)
{
list.Add(asset);
}
}
}
}
public class AssetData
{
public string Name;
public string Path;
public string Guid;
public UnityEngine.Object Obj;
public bool Exist => Obj != null;
public AssetData(string path)
{
Path = path;
Guid = AssetDatabase.AssetPathToGUID(path);
Obj = AssetDatabase.LoadAssetAtPath(path, GetExtensionType(path));
Name = Obj == null ? string.Empty : Obj.name;
}
Type GetExtensionType(string path)
{
var pathExtension = System.IO.Path.GetExtension(Path);
switch (pathExtension)
{
case ".prefab":
return typeof(UnityEngine.Object);
case ".unity":
return typeof(SceneAsset);
case ".cs":
return typeof(MonoScript);
}
return null;
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment