前言
最近需要做一个小工具,快速选取Hierarchy窗口中符合条件的游戏对象
方法
首先我们得知道Unity提供共给我们的API:Selection
其中有一个:Selection.objects,如果我们设置它为我们过滤出来的符合条件的游戏对象,那就可以在Unity的Hierarchy窗口中高亮显示,并且被选中了。
代码
话不多说,直接上代码了
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using System;
namespace AssetTools
{
#if UNITY_EDITOR
public class PhysicsTools
{
protected const string ROOT_MENU_ITEM_NAME = "Tools/Physics/";
}
public class PhysicsResetColTools : PhysicsTools
{
private const string RESET_COL_MENU_ITEM_NAME = ROOT_MENU_ITEM_NAME + "ResetCollider";
[MenuItem(RESET_COL_MENU_ITEM_NAME, false, -5)]
public static void ResetCollider()
{
Debug.Log("Start Reset Collider");
foreach (GameObject cur_select_go in Selection.gameObjects)
{
CapsuleCollider cap_col = cur_select_go.GetComponent<CapsuleCollider>();
SphereCollider sph_col = cur_select_go.GetComponent<SphereCollider>();
BoxCollider box_col = cur_select_go.GetComponent<BoxCollider>();
if (cap_col)
{
cap_col.direction = 0; //x-axis
cap_col.height = 0.2f;
cap_col.radius = 0.08f;
}
if (sph_col)
{
sph_col.radius = 0.14f;
}
if (box_col)
{
box_col.size = new Vector3(0.1f, 0.1f, 0.1f);
}
}
Debug.Log("End Reset Collider");
}
}
public class PhysicsSelectColTools : PhysicsTools
{
private const string SELECT_COL_MENU_ITEM_NAME = ROOT_MENU_ITEM_NAME + "SelectCollider/";
private const string CAP_COL = "Cap";
private const string SPH_COL = "Sph";
private const string BOX_COL = "Box";
private const string ALL_COL = "All";
//----------------------------------------Body + Extra----------------------------------------//
[MenuItem(SELECT_COL_MENU_ITEM_NAME + CAP_COL, false, -10)]
public static void SelectCapCollider()
{
Debug.Log("Start Select CapsuleCollider");
List<GameObject> col_gos = new List<GameObject>();
foreach (GameObject cur_select_go in Selection.gameObjects)
{
GetValidGameObjectsRecursion(cur_select_go.transform, cur_go => cur_go.GetComponent<CapsuleCollider>() != null, ref col_gos);
}
foreach (GameObject go in col_gos)
{
Debug.Log(go);
}
Selection.objects = col_gos.ToArray();
Debug.Log("End Select CapsuleCollider");
}
[MenuItem(SELECT_COL_MENU_ITEM_NAME + SPH_COL, false, 0)]
public static void SelectSphCollider()
{
Debug.Log("Start Select SphereCollider");
List<GameObject> col_gos = new List<GameObject>();
foreach (GameObject cur_select_go in Selection.gameObjects)
{
GetValidGameObjectsRecursion(cur_select_go.transform, cur_go => cur_go.GetComponent<SphereCollider>() != null, ref col_gos);
}
foreach (GameObject go in col_gos)
{
Debug.Log(go);
}
Selection.objects = col_gos.ToArray();
Debug.Log("End Select SphereCollider");
}
[MenuItem(SELECT_COL_MENU_ITEM_NAME + BOX_COL, false, 0)]
public static void SelectBoxCollider()
{
Debug.Log("Start Select BoxCollider");
List<GameObject> col_gos = new List<GameObject>();
foreach (GameObject cur_select_go in Selection.gameObjects)
{
GetValidGameObjectsRecursion(cur_select_go.transform, cur_go => cur_go.GetComponent<BoxCollider>() != null, ref col_gos);
}
foreach (GameObject go in col_gos)
{
Debug.Log(go);
}
Selection.objects = col_gos.ToArray();
Debug.Log("End Select BoxCollider");
}
[MenuItem(SELECT_COL_MENU_ITEM_NAME + ALL_COL, false, 0)]
public static void SelectBodyExtraAllollider()
{
Debug.Log("Start Select All Collider");
List<GameObject> col_gos = new List<GameObject>();
foreach (GameObject cur_select_go in Selection.gameObjects)
{
GetValidGameObjectsRecursion(cur_select_go.transform, cur_go => cur_go.GetComponent<Collider>() != null, ref col_gos);
}
foreach (GameObject go in col_gos)
{
Debug.Log(go);
}
Selection.objects = col_gos.ToArray();
Debug.Log("End Select All Collider");
}
private static void GetValidGameObjectsRecursion(Transform root, Func<GameObject, bool> check_valid_func, ref List<GameObject> gos)
{
if (check_valid_func(root.gameObject))
{
gos.Add(root.gameObject);
}
for (int i = 0; i < root.childCount; i++)
{
GetValidGameObjectsRecursion(root.GetChild(i), check_valid_func, ref gos);
}
}
}
public class PhysicsReplaceColTools : PhysicsTools
{
private const string REPLACE_COL_MENU_ITEM_NAME = ROOT_MENU_ITEM_NAME + "ReplaceCollider/";
private const string REPLACE_CAP_COL_MENU_ITEM_NAME = REPLACE_COL_MENU_ITEM_NAME + "Capsule";
private const string REPLACE_SPH_COL_MENU_ITEM_NAME = REPLACE_COL_MENU_ITEM_NAME + "Sphere";
private const string REPLACE_BOX_COL_MENU_ITEM_NAME = REPLACE_COL_MENU_ITEM_NAME + "Box";
[MenuItem(REPLACE_CAP_COL_MENU_ITEM_NAME, false, 0)]
public static void ReplaceCapCollider()
{
Debug.Log("Start Replace CapsuleCollider");
GameObject[] gos = Selection.gameObjects;
foreach (var go in gos)
{
DestoryCol(go);
AddNormalCapCollider(go);
}
Debug.Log("End Replace CapsuleCollider");
}
[MenuItem(REPLACE_SPH_COL_MENU_ITEM_NAME, false, 0)]
public static void ReplaceSphCollider()
{
Debug.Log("Start Replace SphereCollider");
GameObject[] gos = Selection.gameObjects;
foreach (var go in gos)
{
DestoryCol(go);
AddNormalSphCollider(go);
}
Debug.Log("End Replace SphereCollider");
}
[MenuItem(REPLACE_BOX_COL_MENU_ITEM_NAME, false, 0)]
public static void ReplaceBoxCollider()
{
Debug.Log("Start Replace BoxCollider");
GameObject[] gos = Selection.gameObjects;
foreach (var go in gos)
{
DestoryCol(go);
AddNormalBoxCollider(go);
}
Debug.Log("End Replace BoxCollider");
}
private static void DestoryCol(GameObject go)
{
Collider col = go.GetComponent<Collider>();
if (!ReferenceEquals(col, null))
{
UnityEngine.Object.DestroyImmediate(col);
}
}
private static void AddNormalCapCollider(GameObject go)
{
CapsuleCollider cap_col = go.AddComponent<CapsuleCollider>();
cap_col.direction = 0; //x-axis
cap_col.height = 0.2f;
cap_col.radius = 0.08f;
}
private static void AddNormalSphCollider(GameObject go)
{
SphereCollider sph_col = go.AddComponent<SphereCollider>();
sph_col.radius = 0.14f;
}
private static void AddNormalBoxCollider(GameObject go)
{
BoxCollider box_col = go.AddComponent<BoxCollider>();
box_col.size = new Vector3(0.1f, 0.1f, 0.1f);
}
}
#endif
}
大家可以手动修改代码,达到自己想要的效果
正常来说只需要修改Lambda函数就好了