0%

unity如何查找某个脚本引用在了哪些物体上问题

问题

如平时合作如果遇到合作完成项目或者查看一些奇奇怪怪的项目时,资源过多,不方便查看各个脚本在哪个对象上引用,该怎么办。

解决方法

第一种:

在Hierarchy界面中的搜索栏中输入你想要搜索的脚本全名,即可找到哪些物件用了这个脚本,此方法也可以用来搜索哪些物体上有哪些物件,比如:Rigidbody等。

第二种:

在Unity工程中→选中你需要查看的脚本→鼠标右键→选中“Find References In Scene”→在Hierarchy窗口中查看

在Hierarchy窗口中,上方是添加当前查看的脚本的对象,下方Path是路径,是自下往上读,当然最上方搜索框中也显示选中对象的路径[ref:路径/../..]

第三种:

PS:建议打包导出的时候删除相关代码否则会报错,目前仅供windows系统版本Unity使用

创建脚本 FindMissingScriptsRecursively 和 MonoFinder

这两个脚本代码,一个是用来盛放要被找的那些物体,另一个是盛放你要来查找被物体挂载的脚本

盛放物体的代码:(FindMissingScriptsRecursively类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using UnityEngine;
using UnityEditor;
public class FindMissingScriptsRecursively : EditorWindow
{
static int go_count = 0, components_count = 0, missing_count = 0;

[MenuItem("Window/FindMissingScriptsRecursively")]
public static void ShowWindow()
{
EditorWindow.GetWindow(typeof(FindMissingScriptsRecursively));
}

public void OnGUI()
{
if (GUILayout.Button("Find Missing Scripts in selected GameObjects"))
{
FindInSelected();
}
}
private static void FindInSelected()
{
GameObject[] go = Selection.gameObjects;
go_count = 0;
components_count = 0;
missing_count = 0;
foreach (GameObject g in go)
{
FindInGO(g);
}
Debug.Log(string.Format("Searched {0} GameObjects, {1} components, found {2} missing", go_count, components_count, missing_count));
}

private static void FindInGO(GameObject g)
{
go_count++;
Component[] components = g.GetComponents<Component>();
for (int i = 0; i < components.Length; i++)
{
components_count++;
if (components[i] == null)
{
missing_count++;
string s = g.name;
Transform t = g.transform;
while (t.parent != null)
{
s = t.parent.name + "/" + s;
t = t.parent;
}
Debug.Log(s + " has an empty script attached in position: " + i, g);
}
}
// Now recurse through each child GO (if there are any):
foreach (Transform childT in g.transform)
{
//Debug.Log("Searching " + childT.name + " " );
FindInGO(childT.gameObject);
}
}
}

盛放脚本的代码:(MonoFinder类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;

//查找节点及所有子节点中,是否有指定的脚本组件
public class MonoFinder : EditorWindow
{
Transform root = null;
MonoScript scriptObj = null;
int loopCount = 0;

List<Transform> results = new List<Transform>();

[MenuItem("Level4/Finder/MonoFinder")]//路径可变
static void Init()
{
EditorWindow.GetWindow(typeof(MonoFinder));
}

void OnGUI()
{
GUILayout.Label("节点:");
root = (Transform)EditorGUILayout.ObjectField(root, typeof(Transform), true);
GUILayout.Label("脚本类型:");
scriptObj = (MonoScript)EditorGUILayout.ObjectField(scriptObj, typeof(MonoScript), true);
if (GUILayout.Button("Find"))
{
results.Clear();
loopCount = 0;
Debug.Log("开始查找.");
FindScript(root);
}
if (results.Count > 0)
{
foreach (Transform t in results)
{
EditorGUILayout.ObjectField(t, typeof(Transform), false);
}
}
else
{
GUILayout.Label("无数据");
}
}

void FindScript(Transform root)
{
if (root != null && scriptObj != null)
{
loopCount++;
Debug.Log(".." + loopCount + ":" + root.gameObject.name);
if (root.GetComponent(scriptObj.GetClass()) != null)
{
results.Add(root);
}
foreach (Transform t in root)
{
FindScript(t);
}
}
}
}

添加完之后,会在Unity上部分菜单栏中,按照路径【Level4→Finder→MonoFinder】和【window→FindMissingScriptsRecursively】,分别找到对应脚本的功能。

效果图

img

原文地址:

https://blog.csdn.net/alayeshi/article/details/52039314