[Unity中如何查找腳本掛載在哪個物體上(以2017.2版本為例)](https://blog.csdn.net/weixin_44560436/article/details/86626299)
Unity中如何查找腳本掛載在哪個物體上(以2018版本為例)
Windows->gennal->Test Runner
2:彈出一個下面這個窗口

點擊上圖紅框后在project敞口下的Assets目錄下自動生成tests文件夾且tests文件夾下又個tests的腳本
[asd](file:///D:/important/2018.4.8c3/Editor/Data/Documentation/en/ScriptReference/EditorWindow.html)

```
using UnityEngine;
using UnityEditor;
/// <summary>
/// 盛放物體代碼
/// </summary>
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);
}
}
}
```
```
using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
/////////////////////////////////////////////////////////////////////////////
///盛放腳本的代碼 查找節點及所有子節點中,是否有指定的腳本組件
/////////////////////////////////////////////////////////////////////////////
public class MonoFinder : EditorWindow
{
Transform root = null;
MonoScript scriptObj = null;
int loopCount = 0;
List<Transform> results = new List<Transform>();
[MenuItem("Seek/Finder/MonoFinder")]
//[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的菜單里會多出一個Seek(如圖)的選項,然后點擊它,會出現一個彈窗。上面那個節點(如圖)就是盛放物體的,下面那個腳本類型(如圖)就是放腳本的。
例如下面的案例中,我要查找一個名字叫MainPanel的腳本在Canves這個物體里有多少被掛載了。直接把對應的東西拖進去,然后點擊find就會發現在MainPanel這個子物體里有這個腳本。

- C#
- 基礎 System
- 命名規范
- 變量
- 數據類型
- 值類型
- 簡單類型
- 整數類型
- 字符類型
- 浮點類型
- 布爾類型
- 枚舉類型
- 結構體類型
- 引用類型
- 類類型
- 對象(Object)類型
- 字符串(String)類型
- 方法屬性
- 動態(Dynamic)類型
- 數組類型
- 接口Interface
- 委托類型delegate
- 裝箱和拆箱
- 指針類型
- 值類型與引用類型的區別
- Var類型
- 類型轉換
- 隱式轉換
- 顯式轉換
- 常量
- 常用函數
- 流程控制
- 循環
- 跳轉語句
- 數組
- 數組查找
- 添加組元素
- 復制與克隆數組
- 刪除數組元素
- 數組排序與反轉
- 數組排序
- 冒泡排序
- 直接插入排序
- 選擇排序法
- 數組對象
- 哈希表
- 其他
- 遞歸
- 屬性與方法及方法重載
- 結構與類
- 類
- 構造函數與析構函數
- 繼承
- 多態
- 泛型
- demo
- 漢字字符(串)比較
- 創建指定大小文件與讀取大文件
- 小截屏軟件功能
- 子窗體返回主窗體
- 判斷是否為數字
- 獲得字符串實際長度(包括中文字符)
- unity
- script腳本
- 腳本的生命周期
- 調試
- Unity Manual5.4
- Unity手冊
- Working In Unity
- Unity 2D
- Graphics(圖形)
- Physics(物理系統)
- Scripting(腳本)
- Multiplayer and Networking(多玩家和聯網)
- Audio(音頻)
- Animation(動畫)
- UI
- Navigation and Pathfinding(導航和尋路)
- Unity Services(Unity服務)
- Virtual Reality(虛擬現實VR)
- Open-source repositories(開源代碼庫)
- Platform-specific(特定于平臺的信息)
- Legacy Topics(舊版主題)
- Expert Guides(專家指南)
- 重要的類
- Object
- GameObject(重要)
- Component
- Transform(重要)
- Rigidbody
- ParticleSystem
- Behaviour
- Camera
- Animator
- AudioSource
- Animation
- AudioListener
- Light
- MonoBehaviour事件行為(重要)
- Terrain
- Collider
- Renderer
- Texture
- Mesh
- material
- Time
- Prefab預制件
- 功能demo
- 層級未知查找子物體
- 查找hp最小的敵人
- 查找最近的敵人
- 小項目之英雄無敵
- 界面操作
- Scene窗口
- Game窗口
- project窗口
- Hierarchy窗口
- 動畫
- Animation重要API
- 2D
- Sprite(creator) Sprite Renderer
- Sprite Editor
- Sprite Packer(遺留功能)
- 排序組組件
- 切片精靈
- 精靈遮罩
- 精靈圖集
- Tilemap
- 2D物理系統
- 全局設置(Project setting)
- 2D剛體(Rigidbody 2D)
- 碰撞(事件)消息
- 2D碰撞體(Collider 2D)
- 2D圓形碰撞體(Circle Collider 2D)
- 2D盒型碰撞體(BoxCollider 2D)
- 2D多邊形碰撞體( Polygon Collider 2D)
- 2D邊界碰撞體(EdgeCollider 2D)
- 2D膠囊碰撞體(CapsuleCollider 2D)
- 2D復合碰撞體(Composite Collider 2D)
- 2D物理材質(摩擦和彈性)(Physics Material 2D)
- 2D關節
- 2D距離關節(Distance Joint 2D)
- 2D固定關節(Fixed Joint 2D)
- 2D摩擦關節(Friction Joint 2D)
- 2D鉸鏈關節(Hinge Joint 2D)
- 2D相對關節(Relative Joint 2D)
- 2D滑動關節(Slider Joint 2D)
- 2D彈簧關節(Spring Joint 2D)
- 2D目標關節(Target Joint 2D)
- 2D車輪關節(Wheel Joint 2D)
- 2D恒定力(Constant Force 2D)
- 2D 效應
- 2D區域效應(AreaEffector 2D)
- 2D浮力效應(BuoyancyEffector 2D)
- 2D點效應(PointEffector 2D)
- 2D平臺效應(PlatformEffector 2D)
- 2D表面效應(SurfaceEffector 2D)
- 精靈動畫
- 2D動畫事件
- 重要類
- rigidbody2d
- 小竅門
- Unity中如何查找腳本掛載在哪個物體上