Version: 2017.1

GameObjectConstructor

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

参数

name 创建 GameObject 所使用的名称。
components 在创建时要添加到 GameObjectComponents 列表。

描述

创建一个名为 name 的新游戏对象。

始终将 Transform 添加到正被创建的 /GameObject/。 创建无脚本参数的 GameObject 将添加 Transform,但 其他都不添加。同样,仅具有一个 string 参数的版本仅添加 它和 Transform。最后,第三个版本允许指定该名称, 但也允许以数组的形式传入组件。

// Creates a game object named "Player" and
// adds a rigidbody and box collider to it.

using UnityEngine;

public class ExampleScript : MonoBehaviour { void Start() { GameObject player; player = new GameObject("Player"); player.AddComponent<Rigidbody>(); player.AddComponent<BoxCollider>(); } }