Unity学习笔记--代码动态创建游戏对象的三种方法

方法
new GameObject()
Object.Instantiate
GameObject.CreatePrimitive

一、new GameObject()。

重载方法

在这里插入图片描述

public class Example : MonoBehaviour
{
    private void Start()
    {
        GameObject go1 = new GameObject();
        go1.name = "go1";
        go1.AddComponent<Rigidbody>();

        GameObject go2 = new GameObject("go2");
        go2.AddComponent<Rigidbody>();

        GameObject go3 = new GameObject("go3", typeof(Rigidbody), typeof(BoxCollider));
    }
}

二、Object.Instantiate

重载方法

在这里插入图片描述

注意:这种方法需要一个预制体当作模板,也就是第一个参数。

  • 在运行时实例化预制件
public class ExampleClass : MonoBehaviour
{
    public Transform prefab;
    void Start()
    {
        for (int i = 0; i < 10; i++)
        {
            Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
        }
    }
}
  • Instantiate 也可以直接克隆脚本实例。 将克隆整个游戏对象层级视图,并返回克隆的脚本实例。
using UnityEngine;
using System.Collections;

public class Missile : MonoBehaviour
{
    public int timeoutDestructor;

    // ...other code...
}


public class ExampleClass : MonoBehaviour
{
    // Instantiate a Prefab with an attached Missile script
    public Missile projectile;

    void Update()
    {
        // Ctrl was pressed, launch a projectile
        if (Input.GetButtonDown("Fire1"))
        {
            // Instantiate the projectile at the position and rotation of this transform
            Missile clone = Instantiate(projectile, transform.position, transform.rotation);

            // Set the missiles timeout destructor to 5
            clone.timeoutDestructor = 5;
        }
    }
}

注:克隆对象后,您还可以使用 GetComponent 设置附加到克隆对象的特定组件的属性。

当然Instantiate也支持泛型。
在这里插入图片描述
通过使用泛型,我们不需要将结果转换为特定类型。

using UnityEngine;

public class Missile : MonoBehaviour
{
    // ...other code...
}

public class InstantiateGenericsExample : MonoBehaviour
{
    public Missile missile;

    void Start()
    {
        Missile missileCopy = Instantiate<Missile>(missile);
    }
}

三、GameObject.CreatePrimitive

这个方法是可以创建一个定制的游戏对象,比如Cube。
目前只支持Cube,Sphere,Plane,Quad,Cylinder,Capsule。

如果你想生成一个特定的游戏对象,那么你就可以使用这种方法,而不是先new GameObject然后再AddComponent。
在这里插入图片描述

重载方法

在这里插入图片描述

using UnityEngine;

public class Example : MonoBehaviour
{
    // Create a plane, sphere and cube in the Scene.

    void Start()
    {
        GameObject plane  = GameObject.CreatePrimitive(PrimitiveType.Plane);

        GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
        cube.transform.position = new Vector3(0, 0.5f, 0);

        GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere.transform.position = new Vector3(0, 1.5f, 0);

        GameObject capsule = GameObject.CreatePrimitive(PrimitiveType.Capsule);
        capsule.transform.position = new Vector3(2, 1, 0);

        GameObject cylinder = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        cylinder.transform.position = new Vector3(-2, 1, 0);
    }
}
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

就一枚小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值