Unity3D 在Hierarchy窗口中显示物体所属Layer

我们想要查看场景中物体的Layer时,需要在Hierarchy窗口中点击GameObject,然后才能在Inspector中显示Layer等相关属性,

当场景物体多的时候,这个操作还是比较麻烦的。那么有没有一种方式可以方便直观查看所属Layer呢。

今天我们就利用EditorApplication.hierarchyWindowItemOnGUI 这个接口来实现直接在Hierarchy窗口中显示物体Layer。

先来看下最终效果,Hierarchy窗口中,GameObject右侧的名称就是该物体所在的Layer名称。

实现如下:

using UnityEditor;
using UnityEngine;

// <summary>
/// Hierarchy Window Layer Info
/// http://diegogiacomelli.com.br/unitytips-hierarchy-window-layer-info/
/// </summary>
[InitializeOnLoad]
public static class HierarchyWindowLayerInfo
{
	static readonly int IgnoreLayer = LayerMask.NameToLayer("Default");

	static readonly GUIStyle _style = new GUIStyle()
	{
		fontSize = 9,
		alignment = TextAnchor.MiddleRight
	};

	static HierarchyWindowLayerInfo()
	{
		EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
	}

	static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect)
	{
		var gameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

		if (gameObject != null)
		{
			EditorGUI.LabelField(selectionRect, LayerMask.LayerToName(gameObject.layer), _style);
		}
	}
}

静态类添加[InitializeOnLoad]属性,在Unity编辑器启动的时候就可以执行 同名静态构造函数static HierarchyWindowLayerInfo()

EditorApplication.hierarchyWindowItemOnGUI 接口有两个参数,第一个为object的 InstanceId,第二个为对应的显示区域。

Delegate to be called for every visible list item in the HierarchyWindow on every OnGUI event.

hierarchy窗口中的每个可见物体的OnGUI事件触发时,这个函数都会被调用。

通过这个接口,我们可以实现其他很多定制需求。

官方文档 https://docs.unity3d.com/ScriptReference/EditorApplication.HierarchyWindowItemCallback.html

QHierarchy is an editor extension that adds several often used functions to hierarchy window: - Displaying the icon of a GameObject - Showing / hiding a GameObject - Locking / unlocking a GameObject - Prevent selection of locked GameObject - Displaying tag and layer of a GameObject - Displaying color label for a GameObject - Displaying the icon of MonoBehaviour script attached to a GameObject - GameObject can be showed / hidden only for edit-time, and the visibility state will return during play-time - Displaying the number of children of a GameObject - Displaying the number of vertices and triangles of the GameObject (can display the number including all children) - Change the colors of icons and labels - Displaying custom icon for any layer - Displaying custom icon for any tag - Displaying prefab connection status - Displaying static flags icon of GameObject - Displaying the error icon (MonoBehaviour script missing / Reference property is null / String property is empty / Callback of event is missing) - Displaying icons of all scripts that attached to a GameObject - Showing / hiding wireframe objects - Enable / Disable MeshRenderer - Order of icons can be changed - Displaying the list of GameObjects in the form of a tree - Option to add right indent (useful if you use other plugins that add another icons to the hierarchy window) - Any feature can be disabled - Source Code Included. Website | Documentation | Contacts Compatible with Unity 4.6.0+ / 5.0.1+ / 2017.1+ / 2018.1+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值