版本: Unity 6 (6000.0)
语言English
  • C#

EditorUtility.CreateGameObjectWithHideFlags

建议修改

成功!

感谢您帮助我们提高 Unity 文档的质量。虽然我们无法接受所有提交,但我们确实阅读了用户提出的每个建议的更改,并在适用时进行更新。

关闭

提交失败

由于某些原因,您的建议更改无法提交。请<a>稍后再试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

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(); } }