查看: 612|回复: 0
打印 上一主题 下一主题

[其他] Unity3D开发之查找面板上某个脚本

[复制链接]
may    

8830

主题

81

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
52352
精华
343

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2015-6-28 02:27:44 |只看该作者 |倒序浏览

有时候我们需要知道某个脚本在场景上面哪里用到,或者那个脚本被删除了但又没有把相关游戏场景的关联东西删掉,那样我们就要一个脚本来查找一下了:

ps:下面两个脚本都要放到assets/Editor下面哦。。

查找missing的脚本:
  1. using UnityEngine;

  2. using UnityEditor;

  3. public class FindMissingScriptsRecursively : EditorWindow

  4. {

  5. static int go_count = 0, components_count = 0, missing_count = 0;

  6. [MenuItem(“Window/FindMissingScriptsRecursively”)]

  7. public static void ShowWindow()

  8. {

  9. EditorWindow.GetWindow(typeof(FindMissingScriptsRecursively));

  10. }

  11. public void OnGUI()

  12. {

  13. if (GUILayout.Button(“Find Missing Scripts in selected GameObjects”))

  14. {

  15. FindInSelected();

  16. }

  17. }

  18. private static void FindInSelected()

  19. {

  20. GameObject[] go = Selection.gameObjects;

  21. go_count = 0;

  22. components_count = 0;

  23. missing_count = 0;

  24. foreach (GameObject g in go)

  25. {

  26. FindInGO(g);

  27. }

  28. Debug.Log(string.Format(“Searched {0} GameObjects, {1} components, found {2} missing”, go_count, components_count, missing_count));

  29. }

  30. private static void FindInGO(GameObject g)

  31. {

  32. go_count++;

  33. Component[] components = g.GetComponents<Component>();

  34. for (int i = 0; i < components.Length; i++)

  35. {

  36. components_count++;

  37. if (components[i] == null)

  38. {

  39. missing_count++;

  40. string s = g.name;

  41. Transform t = g.transform;

  42. while (t.parent != null)

  43. {

  44. s = t.parent.name +“/”+s;

  45. t = t.parent;

  46. }

  47. Debug.Log (s + “ has an empty script attached in position: ” + i, g);

  48. }

  49. }

  50. // Now recurse through each child GO (if there are any):

  51. foreach (Transform childT in g.transform)

  52. {

  53. //Debug.Log(“Searching ” + childT.name  + “ ” );

  54. FindInGO(childT.gameObject);

  55. }

  56. }

  57. }
复制代码
查找某个脚本的代码
  1. using UnityEngine;

  2.  using System.Collections;

  3.  using System.Collections.Generic;

  4.  using UnityEditor;

  5.  /////////////////////////////////////////////////////////////////////////////

  6.  //查找节点及所有子节点中,是否有指定的脚本组件

  7.  /////////////////////////////////////////////////////////////////////////////

  8.  public class MonoFinder : EditorWindow {

  9.  Transform root = null;

  10.  MonoScript scriptObj = null;

  11.  int loopCount = 0;

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

  13.  [MenuItem(“Level4/Finder/MonoFinder”)]

  14.  static void Init(){

  15.  EditorWindow.GetWindow(typeof(MonoFinder));

  16.  }

  17.  void OnGUI(){

  18.  GUILayout.Label(“节点:”);

  19.  root = (Transform)EditorGUILayout.ObjectField(root,typeof(Transform),true);

  20.  GUILayout.Label(“脚本类型:”);

  21.  scriptObj = (MonoScript)EditorGUILayout.ObjectField(scriptObj,typeof(MonoScript),true);

  22.  if(GUILayout.Button(“Find”)){

  23.  results.Clear();

  24.  loopCount = 0;

  25.  Debug.Log(“开始查找.”);

  26.  FindScript(root);

  27.  }

  28.  if(results.Count > 0){

  29.  foreach(Transform t in results){

  30.  EditorGUILayout.ObjectField(t,typeof(Transform),false);

  31.  }

  32.  }else{

  33.  GUILayout.Label(“无数据”);

  34.  }

  35.  }

  36.  void FindScript(Transform root){

  37.  if(root != null && scriptObj != null){

  38.  loopCount ++;

  39.  Debug.Log(“..”+loopCount+“:”+root.gameObject.name);

  40.  if( root.GetComponent(scriptObj.GetClass()) != null){

  41.  results.Add(root);

  42.  }

  43.  foreach(Transform t in root){

  44.  FindScript(t);

  45.  }

  46.  }

  47.  }

  48.  }
复制代码
:
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-8-7 21:42 , Processed in 0.061282 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部