HideFlags在Unity中的应用
HideFlags的作用(来自UnityEngine官方注释):
public enum HideFlags
{
//
// Summary:
// A normal, visible object. This is the default.
None = 0x0,
//
// Summary:
// The object will not appear in the hierarchy.
HideInHierarchy = 0x1,
//
// Summary:
// It is not possible to view it in the inspector.
HideInInspector = 0x2,
//
// Summary:
// The object will not be saved to the scene in the editor.
DontSaveInEditor = 0x4,
//
// Summary:
// The object is not be editable in the inspector.
NotEditable = 0x8,
//
// Summary:
// The object will not be saved when building a player.
DontSaveInBuild = 0x10,
//
// Summary:
// The object will not be unloaded by Resources.UnloadUnusedAssets.
DontUnloadUnusedAsset = 0x20,
//
// Summary:
// The object will not be saved to the scene. It will not be destroyed when a new
// scene is loaded. It is a shortcut for HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor
// | HideFlags.DontUnloadUnusedAsset.
DontSave = 0x34,
//
// Summary:
// A combination of not shown in the hierarchy, not saved to to scenes and not unloaded
// by The object will not be unloaded by Resources.UnloadUnusedAssets.
HideAndDontSave = 0x3D
}
可用于在Hierarchy和右键菜单中修改物体HideFlags的编辑器脚本:
using UnityEngine;
using UnityEditor;
public class HierarchyExtension{
[InitializeOnLoadMethod]
static void InitializeOnEnterPlayMode()
{
EditorApplication.hierarchyWindowItemOnGUI = delegate (int instanceID, Rect selectionRect)
{
if (Selection.activeGameObject && instanceID == Selection.activeGameObject.GetInstanceID())
{
currentSelectedObject = Selection.activeGameObject;
float width = 60f;
float height = 20f;
selectionRect.x += (selectionRect.width - width);
selectionRect.width = width;
selectionRect.height = height;
string buttonString = Selection.activeGameObject.hideFlags.ToString();
if (GUI.Button(selectionRect, buttonString))
{
HideFlags hideFlags = Selection.activeGameObject.hideFlags == HideFlags.None ? HideFlags.NotEditable : HideFlags.None;
Selection.activeGameObject.hideFlags = hideFlags;
Debug.LogFormat("Set HideFlags: {0}, GameObject: {1}", hideFlags, Selection.activeGameObject.name);
}
}
};
}
static GameObject currentSelectedObject { get; set; }
[MenuItem("GameObject/Hide Flags/None", false, 0)]
public static void SetHideFlagsNone()
{
if (currentSelectedObject)
{
currentSelectedObject.hideFlags = HideFlags.None;
Debug.LogFormat("Set HideFlags: None, GameObject: {0}", currentSelectedObject.name);
}
else
{
Debug.Log("Set HideFlags: NULL, GameObject: NULL, No selected gameobject!");
}
}
[MenuItem("GameObject/Hide Flags/Not Editable", false, 1)]
public static void SetHideFlagsNotEditable()
{
if (currentSelectedObject)
{
currentSelectedObject.hideFlags = HideFlags.NotEditable;
Debug.LogFormat("Set HideFlags: NotEditable, GameObject: {0}", currentSelectedObject.name);
}
else
{
Debug.Log("Set HideFlags: NULL, GameObject: NULL, No selected gameobject!");
}
}
[MenuItem("GameObject/Hide Flags/Hide In Inspector", false, 2)]
public static void SetHideFlagsDontSaveInEditor()
{
if (currentSelectedObject)
{
currentSelectedObject.hideFlags = HideFlags.HideInInspector;
Debug.LogFormat("Set HideFlags: HideInInspector , GameObject: {0}", currentSelectedObject.name);
}
else
{
Debug.Log("Set HideFlags: NULL, GameObject: NULL, No selected gameobject!");
}
}
}
Unity版本:2017.4.27f1
参考资料
Unity - Scripting API: HideFlags
留言
發佈留言
歡迎在此寫下你的感想。