克隆/复制现有的GameObject及其子对象 [英] Clone/duplicate an existing GameObject and its children

查看:1674
本文介绍了克隆/复制现有的GameObject及其子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a C# way in Unity to duplicate an existing GameObject and all of its children? In my case, I have an empty GameObject with a number of Text objects as children and I would like to create a copy of them all, including relative positions, text values, font, colors, etc....

预制件无法轻松工作,因为我想复制对象(包括其当前状态).

Prefabs won't work easily because I want to copy the object including its current state.

推荐答案

Instantiate 函数用于克隆任何GameObject及其层次结构.

The Instantiate function is used to clone any GameObject and its hierarchy.

public GameObject rootObj;

void Start()
{
    GameObject duplicate = Instantiate(rootObj);
}

这篇关于克隆/复制现有的GameObject及其子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆