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

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

问题描述

Unity 中是否有 C# 方法来复制现有游戏对象及其所有子对象?就我而言,我有一个空的 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 不能轻松工作,因为我想复制包括其当前状态的对象.

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

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

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