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

猜你喜欢

转载自blog.csdn.net/qq_52855744/article/details/124862386