기술 자료

Unity Engine


Unity Engine


AddComponent

Creates a new component and adds it to the specified GameObject.
읽는 시간 1분최근 업데이트: 한 달 전

Definition

  • Type: Method
  • Namespace: UnityEditor
  • Assembly: UnityEditor

AddComponent<T>(GameObject)

Creates a new component and adds it to the specified GameObject.
public static T AddComponent<T>(GameObject gameObject) where T : Component

Parameters

gameObject

The GameObject to add the new component to.

Returns

Type

Description

TReturns the component that was created and added to the GameObject.

Examples

using UnityEngine;using UnityEditor;public class CreateComponentExample{ [MenuItem("ObjectFactoryExample/Add Camera to Selection")] public void AddDefaultComponentEditor() { if (Selection.activeGameObject != null) { Camera camera = ObjectFactory.AddComponent<Camera>(Selection.activeGameObject); } }}

AddComponent(GameObject, Type)

Creates a new component and adds it to the specified GameObject.
public static Component AddComponent(GameObject gameObject, Type type)

Parameters

gameObject

The GameObject to add the new component to.

type

The type of component to create and add to the GameObject.

Returns

Type

Description

ComponentReturns the component that was created and added to the GameObject.

Examples

using UnityEngine;using UnityEditor;public class CreateComponentExample{ [MenuItem("ObjectFactoryExample/Add Camera to Selection")] public void AddDefaultComponentEditor() { if (Selection.activeGameObject != null) { Camera camera = ObjectFactory.AddComponent<Camera>(Selection.activeGameObject); } }}