unity3d 根据tag查找物体编辑器工具

一直有这样的需求,现在写下来.备注

 

  1 /// <summary>
  2 /// 查找场景中具有某些标签的可见物体,返回物体列表 
  3 /// </summary>
  4 
  5 using UnityEngine;
  6 using System.Collections;
  7 using UnityEditor;
  8 using System.Collections.Generic;
  9 
 10 public class TagFinder : EditorWindow
 11 {
 12 
 13     public int tagCount = 0;
 14     private int maxTagCount = 10;
 15     private int minTagCount = 1;
 16     public List<GameObject> list = new List<GameObject> ();
 17     private bool ifShowList = true;
 18     private Vector2 scrollVec;
 19     private string[] tags;
 20     private bool ifShowWarning = false;
 21 
 22     [MenuItem ("Tag/Tag Finder %t")]
 23     static void Init ()
 24     {
 25         // Get existing open window or if none, make a new one:
 26         TagFinder window = (TagFinder)EditorWindow.GetWindow (typeof(TagFinder));
 27     }
 28 
 29 
 30 
 31     void OnEnable ()
 32     {
 33 
 34         tags = new string[maxTagCount];
 35         ifShowList = false;
 36     }
 37 
 38     void OnGUI ()
 39     {
 40         GUILayout.Space (4);
 41 
 42         GUILayout.BeginHorizontal ();
 43         GUILayout.Label ("要查找物体的Tag:", EditorStyles.boldLabel);
 44         tagCount = EditorGUILayout.IntSlider (tagCount, minTagCount, maxTagCount);
 45         GUILayout.EndHorizontal ();
 46 
 47         GUILayout.Space (4);
 48 
 49         EditorGUI.indentLevel = 1;
 50     
 51         for (int i = 0; i<tagCount; i++) {
 52             tags [i] = EditorGUILayout.TagField ("Tag" + i.ToString (), tags [i]);
 53 //            Debug.Log ("tags" + i.ToString () + "is " + tags [i]);
 54         }
 55 
 56         GUILayout.Space (4);
 57 
 58         if (GUILayout.Button ("获取物体列表")) {
 59             if (list.Count > 0) {
 60                 list.Clear ();
 61             }
 62             for (int i = 0; i<tagCount; i++) {
 63                 if (tags [i] == null || tags [i].Equals (string.Empty)) {
 64                     continue;
 65                 }
 66                 GameObject[] os = GameObject.FindGameObjectsWithTag (tags [i]);
 67                 
 68                 
 69                 if (os == null) {
 70                     continue;
 71                 } else {
 72                     for (int j= 0; j<os.Length; j++) {
 73                         if(!list.Contains(os[j]))
 74                             list.Add (os [j]);
 75                     }
 76                 }
 77             }
 78 
 79 //            ifShowList = !ifShowList;
 80             ifShowList = true;
 81         }
 82 
 83 
 84 
 85 
 86         if (ifShowList) {
 87 
 88             GUILayout.Space(4);
 89             GUILayout.BeginHorizontal();
 90 
 91             GUILayout.Label ("你要找的所有物体如下:", EditorStyles.boldLabel);
 92             GUILayout.Label ("总数:" + list.Count.ToString(), EditorStyles.boldLabel);
 93             if (GUILayout.Button ("清除物体列表")) {
 94                 if (list != null)
 95                 {
 96                     list.Clear ();
 97                 }
 98                 ifShowList = false;
 99             }
100             GUILayout.EndHorizontal();
101             EditorGUI.indentLevel = 1;
102             float height = list.Count == 0 ? 10f : 300f ;
103             scrollVec = EditorGUILayout.BeginScrollView (scrollVec, GUILayout.Height (height));
104 
105             GUILayout.Space (4);
106             
107             for (int i = 0; i<list.Count; i++) {
108                 EditorGUILayout.ObjectField (list [i], typeof(GameObject), false);
109             }
110             EditorGUILayout.EndScrollView ();
111 
112             if(list.Count == 0)
113             {
114                 ifShowWarning = true;
115             }
116             else
117             {
118                 ifShowWarning = false;
119             }
120             if(ifShowWarning)
121             {
122                 EditorGUILayout.HelpBox("Oops,没有找到你想要东东哦 !",MessageType.Warning);
123             }
124         }
125     }
126     void OnInspectorUpdate()
127     {
128         Repaint();
129     }
130 
131 
132 
133 }

 

转载于:https://www.cnblogs.com/leesymbol/p/5016534.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值