简体   繁体   English

创建 Unity 预制件的克隆

[英]Creating a clone of a Unity prefab

I am trying to load a prefab from a file and create a clone of the loaded file without instantiating it.我正在尝试从文件加载预制件并创建加载文件的克隆而不实例化它。 Changing the data values of the loaded clones script, should then not change the prefabs values.更改加载的克隆脚本的数据值,不应更改预制件值。

I am in a way trying to create a clone of a cloned and loaded prefab.我正在尝试创建克隆和加载预制件的克隆。

Loading a prefab into unity using Resources.Load:使用 Resources.Load 将预制件加载到 unity 中:

 enemy = new Player();
 enemy.playerColor = Color.red;
 enemy.Name = "enemy";
 enemy.unitList.AddResources.Load("Prefab/Soldier", typeof(GameObject)) as GameObject);

Loading a prefab with the instantiate clone function:使用实例化克隆功能加载预制件:

enemy = new Player();
enemy.playerColor = Color.red;
enemy.Name = "enemy";
enemy.unitList.Add(Instantiate(Resources.Load("Prefab/Soldier", typeof(GameObject)) as GameObject) as GameObject);

Changing the loaded prefab:更改加载的预制件:

public void updateUnit(string prop, float val)
{
    switch (prop)
    {
        case "Movespeed":
            Movespeed += (int)val;
            break;
        case "SpawnRate":
            SpawnRate += val;
            break;
    }
}

The current problem is that using the instantiate function on the prefab, creates the clone on the screen.当前的问题是在预制件上使用实例化功能,在屏幕上创建克隆。 Whilst using the loaded prefab results in changes in the script in the prefab使用加载的预制件会导致预制件中的脚本发生变化

1: create a gameobject and assign the prefab to it any way you want 1:创建一个游戏对象并以您想要的任何方式为其分配预制件

private GameObject toclone;

2:create a gameobject to clone onto, so you can call it later 2:创建一个游戏对象来克隆,这样你就可以稍后调用它

private GameObject clone;

3:run this command to summon it. 3:运行这个命令来召唤它。 replace Position with a vector3 and Rotation with a quaternion (if you want to use vector3 for rotation, use Quaternion.Euler)将 Position 替换为 vector3,将 Rotation 替换为四元数(如果要使用 vector3 进行旋转,请使用 Quaternion.Euler)

clone = Instantiate(toclone, Position, Rotation, Quaternion.Identity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM