ObjectFactory.AddComponent

Unity · Версия 2020.3 LTS · Scripting · ObjectFactory

ObjectFactory.AddComponent

Объявление

public static Component AddComponent(GameObject gameObject, Type type);

public static T AddComponent(GameObject gameObject);

Параметры

gameObject GameObject, в который нужно добавить новый компонент.
type Тип компонента для создания и добавления в GameObject.

Возвращает

Component Возвращает компонент, который был создан и добавлен в GameObject.

Описание

Создает новый компонент и добавляет его к указанному GameObject.

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

Обновлено 17 ноября 2026 г.