EditorUtility.CreateGameObjectWithHideFlags

切换到手册
public static GameObject CreateGameObjectWithHideFlags (string name, HideFlags flags, params Type[] components);

描述

使用 HideFlags 和指定组件创建游戏对象。

这与按常规方式创建 GameObject 非常类似,区别在于它 会立即设置指定的 HideFlags

“显示示例外观的编辑器窗口。”

using UnityEngine;
using UnityEditor;

public class CreateGOHideFlagsExample : EditorWindow { string objName = "GameObject"; int instanceID = 0; bool create = true; GameObject go = null; bool hideHierarchy = false;

[MenuItem("Example/GameObject Flags")] static void Init() { // Get existing open window or if none, make a new one: CreateGOHideFlagsExample window = (CreateGOHideFlagsExample)EditorWindow.GetWindow(typeof(CreateGOHideFlagsExample)); window.Show(); }

void OnGUI() { create = EditorGUILayout.Toggle("Create a GO:", create); GUI.enabled = create;

objName = EditorGUILayout.TextField("GameObject Name:", objName); if (GUILayout.Button("Create")) { GameObject created = EditorUtility.CreateGameObjectWithHideFlags(objName, hideHierarchy ? HideFlags.HideInHierarchy : 0);

instanceID = created.GetInstanceID(); Debug.Log("Created GameObject ID: " + instanceID); }

GUI.enabled = !create;

EditorGUILayout.BeginHorizontal();

instanceID = EditorGUILayout.IntField("Instance ID:", instanceID);

if (GUILayout.Button("Search & Update flags")) { go = null; go = EditorUtility.InstanceIDToObject(instanceID) as GameObject; if (go) go.hideFlags = hideHierarchy ? HideFlags.HideInHierarchy : 0; }

EditorGUILayout.EndHorizontal();

if (!go) EditorGUILayout.LabelField("Object: ", (go == null) ? "No object was found" : go.name);

GUI.enabled = true; hideHierarchy = EditorGUILayout.Toggle("HideInHierarchy", hideHierarchy); }

void OnInspectorUpdate() { Repaint(); } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961