获取网站访客qq代码,网络专业的网站建设价格,vs2010 网站开发,无锡网站建设外包优势背景#xff1a; 有时候我们需要找出项目中所有的引用到某个脚本的地方#xff08;比如Prefabs/Scene GameObjects等#xff09;。当项目比较大时#xff0c;手工寻找还是非常费时费力的事情。本文尝试通过插件自动搜索。 分析#xff1a; 基本的思路是#xff1a;首先筛… 背景 有时候我们需要找出项目中所有的引用到某个脚本的地方比如Prefabs/Scene GameObjects等。当项目比较大时手工寻找还是非常费时费力的事情。本文尝试通过插件自动搜索。 分析 基本的思路是首先筛选出项目中全部Prefab加载每个Prefab并判断是否有挂载目标脚本然后载入每个场景判断场景中每个物体是否有挂载目标脚本最后列出结果。 实现 1在右键菜单项中添加菜单 新建一个类命名为 FindScriptRef 并继承自 EditorWindow 。添加如下方法 [MenuItem(“Assets/Find All Reference”)]
public static void ShowWindow()
{//Show existing window instance. If one doesn’t exist, make one.EditorWindow.GetWindow(typeof(FindScriptRef));
}这段代码会在菜单中添加一个名为“Find All Reference”的菜单项。选中菜单项会打开一个FindScriptRef窗口实例。当然此时窗口中没有任何内容。 2窗口基本显示逻辑 void OnGUI()
{if (Selection.activeObject null){GUILayout.Label(“select a script file from Project Window.”);return;}
//判断选中项是否为脚本
var name Selection.activeObject.name;
System.Reflection.Assembly assembly System.Reflection.Assembly.GetExecutingAssembly();
var dict System.IO.Path.GetDirectoryName(assembly.Location);
assembly System.Reflection.Assembly.LoadFile(System.IO.Path.Combine(dict, Assembly-CSharp.dll));
var selectType assembly.GetType(name);
if (string.IsNullOrEmpty(name) || selectType null)
{GUILayout.Label(select a script file from Project Window.);return;
}GUILayout.BeginVertical();
GUILayout.BeginHorizontal();//列出脚本名称和“Find”按钮
GUILayout.Label(name);
bool click GUILayout.Button(Find);
GUILayout.EndHorizontal();
GUILayout.Space(10);//列出搜索结果
if (findResult ! null findResult.Count 0)
{GUILayout.BeginScrollView(Vector2.zero, GUIStyle.none);foreach (string path in findResult){GUILayout.Label(path);}GUILayout.EndScrollView();
}if (click)
{Find(selectType);
}
GUILayout.EndVertical();
} 然后实现Find方法搜索指定Type的全部引用 void Find(System.Type type){
//step 1:find ref in assets//filter all GameObject from assetsso-called Prefab
var guids AssetDatabase.FindAssets(t:GameObject);findResult new Liststring();var tp typeof(GameObject);foreach (var guid in guids)
{var path AssetDatabase.GUIDToAssetPath(guid);//load Prefabvar obj AssetDatabase.LoadAssetAtPath(path, tp) as GameObject;//check whether prefab contains script with type typeif (obj ! null){var cmp obj.GetComponent(type);if (cmp null){cmp obj.GetComponentInChildren(type);}if (cmp ! null){findResult.Add(path);}}
}//step 2: find ref in scenes//save current scene
string curScene EditorApplication.currentScene;
EditorApplication.SaveScene();//find all scenes from dataPath
string[] scenes Directory.GetFiles(Application.dataPath, *.unity, SearchOption.AllDirectories);//iterates all scenes
foreach (var scene in scenes)
{EditorApplication.OpenScene(scene);//iterates all gameObjectsforeach (GameObject obj in FindObjectsOfTypeGameObject()){var cmp obj.GetComponent(type);if (cmp null){cmp obj.GetComponentInChildren(type);}if (cmp ! null){findResult.Add(scene.Substring(Application.dataPath.Length) Assets: obj.name);}}
}//reopen current scene
EditorApplication.OpenScene(curScene);
Debug.Log (finish);
} 此时切换到Unity在Project窗口选中一个scrpit右键选择『Find All Reference』在打开的窗口选择『Find』按钮即可看到下面列出了所有引用了这个脚本的位置如果项目过于庞大可能需要等待一会儿。 代码下载[FindScriptRef.cs]({{ site.url }}/files/FindScriptRef.cs “FindScriptRef.cs”)