AddComponent
Creates a new component and adds it to the specified GameObject.
読み終わるまでの所要時間 1 分最終更新 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
The GameObject to add the new component to.
Returns
Type | Description |
|---|---|
| T | Returns 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
The GameObject to add the new component to.
The type of component to create and add to the GameObject.
Returns
Type | Description |
|---|---|
| Component | Returns 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); } }}