Clone/duplicate 一个现有的游戏对象及其子对象

Clone/duplicate an existing GameObject and its children

Unity 中是否有 C# 方法来复制现有游戏对象及其所有子对象?在我的例子中,我有一个空的 GameObject,其中包含许多作为子对象的 Text 对象,我想为它们创建一个副本,包括相对位置、文本值、字体、颜色等....

Prefabs 不会很容易工作,因为我想复制包含其当前状态的对象。

Instantiate 函数用于克隆任何游戏对象及其层次结构。

public GameObject rootObj;

void Start()
{
    GameObject duplicate = Instantiate(rootObj);
}
using System.Collections;

使用System.Collections.Generic; 使用 UnityEngine;

public class 主要:MonoBehaviour { public 游戏对象香港;

void Start()
{
    hk = new GameObject("I am hk");
}

void Update()
{
    hk = new GameObject("I am hk");
}

}