リソースがクエリーを使用するいくつかの方法
14823 ワード
雨松の方法、コードの量は少なくて、工事の中で原文を置くことができます
後でbaidudarenのもう一つのスクリプトを見て、5.0以上の原文をサポートします.
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
public class FindReferences
{
[MenuItem("Assets/Find References", false, 10)]
static private void Find()
{
EditorSettings.serializationMode = SerializationMode.ForceText;
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (!string.IsNullOrEmpty(path))
{
string guid = AssetDatabase.AssetPathToGUID(path);
string withoutExtensions = "*.prefab*.unity*.mat*.asset";
string[] files = Directory.GetFiles(Application.dataPath, "*.*", SearchOption.AllDirectories)
.Where(s => withoutExtensions.Contains(Path.GetExtension(s).ToLower())).ToArray();
int startIndex = 0;
EditorApplication.update = delegate()
{
string file = files[startIndex];
bool isCancel = EditorUtility.DisplayCancelableProgressBar(" ", file, (float)startIndex / (float)files.Length);
if (Regex.IsMatch(File.ReadAllText(file), guid))
{
Debug.Log(file, AssetDatabase.LoadAssetAtPath
後でbaidudarenのもう一つのスクリプトを見て、5.0以上の原文をサポートします.
#define UseAutoFind
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System.Reflection;
public class ArtFindSet{
public bool _onlyShowNoUsed;
}
public class ArtAssetsEx : EditorWindow{
#region Init
static ArtAssetsEx Instance;
static List _filePrefabPath = new List();
static List _fileAssetsPath = new List();
static List _prefabs = new List();
static List _readyToFind;
static bool _path;
static public AssetUsedData _AssetUsedData;
static public AllAssetsData _AllAssetsData;
static public string[] _AssetType;
static public int _SelectAssetType = -1;
static public ArtFindSet _artFindSet = new ArtFindSet();
static bool _repaint;
[MenuItem("Window/ ")]
public static void Init(){
Instance = EditorWindow.GetWindow ();
//#if UseAutoFind
_filePrefabPath.Add (Application.dataPath);
// Find ();
//#endif
}
// [MenuItem("Assets/ Find Art Prefab Path")]
// public static void AddFilePrefabPath(){
// string path = AssetDatabase.GetAssetPath(Selection.activeObject);
// path = path.Replace ("Assets", "");
// _filePrefabPath.Add(Application.dataPath + path);
// }
// [MenuItem("Assets/ Find Art Assets Path")]
// public static void AddFileAssetsPath(){
// string path = AssetDatabase.GetAssetPath(Selection.activeObject);
// path = path.Replace ("Assets", "");
// _fileAssetsPath.Add(Application.dataPath + path);
// }
[MenuItem("Assets/ ")]
public static void FindUsePrefab(){
string path = AssetDatabase.GetAssetPath(Selection.activeObject);
if (Instance == null) {
Init();
Find();
}
_AssetUsedData = new AssetUsedData (path);
_AssetUsedData.FindUsed (_prefabs);
_repaint = true;
}
// [MenuItem("Assets/ Find Use Prefab")]
// public static void FindUsePrefab(){
// string path = AssetDatabase.GetAssetPath(Selection.activeObject);
//// AssetDatabase.SetLabels(
// }
static public string[] _toolStr = {" "," "};
static public int _selectTool;
void OnGUI(){
_selectTool = GUILayout.Toolbar (_selectTool, _toolStr);
switch (_selectTool) {
case 1:
GUILayout.Label (" ");
for (int i = 0; i < _filePrefabPath.Count; i++) {
EditorGUILayout.BeginHorizontal ();
if (GUILayout.Button ("-", GUILayout.Width (30))) {
_filePrefabPath.RemoveAt (i);
i++;
} else {
GUILayout.Label (_filePrefabPath [i]);
}
EditorGUILayout.EndHorizontal ();
}
GUILayout.Label (" ");
for (int i = 0; i < _fileAssetsPath.Count; i++) {
EditorGUILayout.BeginHorizontal ();
if (GUILayout.Button ("-", GUILayout.Width (30))) {
_fileAssetsPath.RemoveAt (i);
i++;
} else {
GUILayout.Label (_fileAssetsPath [i]);
}
EditorGUILayout.EndHorizontal ();
}
break;
case 0:
// if (GUILayout.Button ("Find",GUILayout.Height(100),GUILayout.MaxWidth(300)))
// Find ();
if(_prefabs != null && _AllAssetsData!=null){
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField(" :"+_prefabs.Count);
EditorGUILayout.LabelField(" :"+_AllAssetsData._data.Count);
EditorGUILayout.EndVertical();
}
ShowTest ();
break;
}
if (_repaint) {
Repaint ();
}
}
Vector2 pos;
void ShowTest(){
bool change = false;
EditorGUILayout.BeginHorizontal ();
if (_AssetType != null) {
_SelectAssetType = EditorGUILayout.Popup(" :",_SelectAssetType,_AssetType,GUILayout.MaxWidth(300));
if(GUI.changed){
change = true;
EditorGUILayout.EndHorizontal ();
_artFindSet._onlyShowNoUsed = false;
_AllAssetsData.GetDepend(_SelectAssetType,_prefabs);
}
}
if(!change)
EditorGUILayout.EndHorizontal ();
if (_AssetUsedData != null) {
if(GUILayout.Button(" "))
_AssetUsedData = null;
if(_AssetUsedData != null)
_AssetUsedData.OnGUI ();
}
if(_AssetUsedData == null && _SelectAssetType != -1){
_artFindSet._onlyShowNoUsed = EditorGUILayout.Toggle(!_artFindSet._onlyShowNoUsed?" ":" " ,_artFindSet._onlyShowNoUsed);
EditorGUILayout.LabelField(_AssetType[_SelectAssetType]+" :"+_AllAssetsData._data[_SelectAssetType]._allAssets);
EditorGUILayout.LabelField(_AssetType[_SelectAssetType]+" :"+_AllAssetsData._data[_SelectAssetType]._allUsedAssets);
EditorGUILayout.LabelField(_AssetType[_SelectAssetType]+" :"+_AllAssetsData._data[_SelectAssetType]._allUnUsedAssets);
pos = EditorGUILayout.BeginScrollView (pos);
_AllAssetsData.OnGUI(_SelectAssetType,_artFindSet);
EditorGUILayout.EndScrollView ();
}
}
#endregion
#region Find
static void Find(){
_SelectAssetType = -1;
if (_filePrefabPath == null || _filePrefabPath.Count == 0)
_filePrefabPath.Add (Application.dataPath);
_readyToFind = new List ();
_prefabs = new List ();
_AllAssetsData = new AllAssetsData ();
for (int i = 0; i < _filePrefabPath.Count; i++) {
FindAllPath(_filePrefabPath[i]);
}
CreatePrefabData ();
RestPrefabDependencie ();
_AssetType = new string[_AllAssetsData._data.Count];
int count = 0;
for (int i = 0;i < _AllAssetsData._data.Count;i++) {
_AssetType[count] = _AllAssetsData._data[i]._expandname;
count++;
}
}
static void CreatePrefabData(){
for(int i = 0;i < _readyToFind.Count;i++){
string expandname = Path.GetExtension(_readyToFind[i]);
if(expandname == ".prefab"||expandname == ".unity"){
PrefabDependencies pd = new PrefabDependencies(_readyToFind[i]);
_prefabs.Add(pd);
}
_AllAssetsData.AddAssets(_readyToFind[i]);
}
}
static void RestPrefabDependencie(){
for (int i = 0; i < _prefabs.Count; i++) {
_prefabs[i].GetDependencies();
if(EditorUtility.DisplayCancelableProgressBar(" ","GetDependencie:"+i,(float)i/_prefabs.Count)){
EditorUtility.ClearProgressBar ();
return;
}
}
EditorUtility.ClearProgressBar ();
}
static void FindAllPath(string path){
string[] Directorys = new string[0];
try{
Directorys = Directory.GetFiles(path);
}catch{}
if(Directorys != null){
for(var i = 0;i < Directorys.Length;i++){
if(!_readyToFind.Contains(Directorys[i]))
_readyToFind.Add(Directorys[i]);
}
}
Directorys = Directory.GetDirectories (path);
for (int i = 0; i < Directorys.Length; i++) {
string newpath;
newpath = Path.GetDirectoryName(Directorys[i])+"/"+Path.GetFileName(Directorys[i]);
FindAllPath(newpath);
}
}
#endregion
}
public class AllAssetsData{
public List _data = new List();
public void GetDepend(int _id,List prefabs){
if(_id>=0&&_id<_data.count _data="" public="" void="" addassets="" path="" string="" expandname="Path.GetExtension(path);" if="" return="" int="" id="IsContainsKey" _data.add="" assetsuseddata="" iscontainskey="" for="" i="0;i" _data.count="" ongui="" _selectassettype="" set="">= 0 && _SelectAssetType < _data.Count) {
int x = 0;
for(int i = 0;i < _data[_SelectAssetType]._data.Count;i++){
if((_data[_SelectAssetType]._data[i]._usedPrefab.Count == 0 && set._onlyShowNoUsed)||
(_data[_SelectAssetType]._data[i]._usedPrefab.Count > 0 && !set._onlyShowNoUsed)
){
Color c = (x%2==0?new Color(0.8f,0.8f,0.8f):Color.white);
GUI.color = c;
EditorGUILayout.BeginVertical("box");
GUI.color = Color.white;
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Used:"+_data[_SelectAssetType]._data[i]._usedPrefab.Count+" ",GUILayout.Width(100));
// EditorGUILayout.LabelField(_data[_SelectAssetType]._data[i]._path);
// EditorGUILayout.ObjectField(_data[_SelectAssetType]._data[i].assetObj,typeof(GameObject));
EditorGUILayout.EndHorizontal();
_data[_SelectAssetType]._data[i].OnGUI();
EditorGUILayout.EndVertical();
x++;
}
}
}
// foreach (KeyValuePair value in _data) {
// List datas = value.Value._data;
// for(int i= 0;i < datas.Count;i++){
// EditorGUILayout.LabelField(datas[i]._path);
// datas[i].OnGUI();
// }
// }
}
}
public class AssetsUsedData{
public int _allAssets;
public int _allUsedAssets;
public int _allUnUsedAssets;
public string _expandname;
public AssetsUsedData(string expandname){
_expandname = expandname;
}
public List _data = new List();
public void AddData(string path){
path = "Assets"+path.Replace(Application.dataPath,"");
path = path.Replace ("\\", "/");
_data.Add (new AssetUsedData (path));
}
public void GetDepend(List prefabs){
for(int i = 0;i<_data.count if="" _data.clear="" editorutility.clearprogressbar="" return="" _data="" for="" i="0;" _data.count="" _allassets="_data.Count;" _allusedassets="0;"> 0){
_allUsedAssets ++;
}
}
_allUnUsedAssets = _allAssets - _allUsedAssets;
EditorUtility.ClearProgressBar ();
}
}
public class AssetUsedData{
public AssetUsedData(string path){
_path = path;
}
public string _path;
public Object _assetObj;
public void LoadAssetObj(){
if (_assetObj == null)
_assetObj = AssetDatabase.LoadAssetAtPath