(Unity 2D)当实例化预制件离开屏幕时,将其销毁

本文关键字:屏幕 离开 2D Unity 实例化 | 更新日期: 2023-09-27 17:59:16

我在Unity 2D(4.3)中制作一个2D游戏,我需要销毁当这些预制件离开屏幕时实例化的预制件。我已经写了一些代码来生成对象,但我想在它们离开屏幕时删除它们。这是我迄今为止写的代码。

生成预制件(C#):

void Update () {
    float y = Random.Range(-4.53f, 2.207f);
    if(x < 2000) {
        Instantiate(obstacle, new Vector3(y, x * 6.0f, 0),Quaternion.identity);
        x++;
    }
    //Debug.Log(x);
}

销毁预制件(C#):

    /*************************************************************************************************
     * GET INSTANTIATED OBSTACLE
     * AND DESTROY IT ON EXIT
     * TO SAVE MEMORY
    **************************************************************************************************/
    GameObject clone = (GameObject)Instantiate (obstacle);
    /*if(clone.transform.position.y == -11)
    {
        Destroy(clone);
        Debug.Log("Destroy");
    }*/
    Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
    if (screenPosition.y > Screen.height || screenPosition.y < 0)
    {
        Destroy(gameObject);
        Debug.Log("Destroy");
    }

但是,销毁对象的代码不起作用,但也没有出现错误。它确实在预制件离开屏幕后输出"销毁",所以我知道销毁它们的代码有问题。

感谢

(Unity 2D)当实例化预制件离开屏幕时,将其销毁

您可以制作一个组件,当位置在相机之外时,该组件会自我摧毁,然后将该组件连接到障碍物上。

void Update() {
    float y = Random.Range(-4.53f, 2.207f);
    if(x < 2000) {
        GameObject clone = (GameObject)Instantiate(obstacle, new Vector3(y, x * 6.0f, 0),Quaternion.identity);
        clone.AddComponent(typeof(DestroyMySelf));
        x++;
    }
}

这个组件附着在障碍物上会自我毁灭。

public class DestroyMySelf : MonoBehaviour {
    void Update() {
        Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
        if (screenPosition.y > Screen.height || screenPosition.y < 0)
        Destroy(this.gameObject);
    }
}

您可以在屏幕的4面上制作4个四边形,并用它们连接boxCollider并检查其isTrigger。之后,将以下脚本添加到每个四边形中,以检查是否有东西在其OnTriggerEnter中与之碰撞,在那里你可以检查实例化对象的标签,也可以销毁与之碰撞的每个对象(取决于游戏)。使用下方的代码

//for 3d games
void OnTriggerEnter(Collider other) {
//you may check the tag of the 'other' object here to make sure if its your instantiated object
//if(other.gameObject.tag=="yourInstantiatedObjectTag")
    Destroy(other.gameObject);//dont forget to check the isTrigger of the quad or else the event will not trigger
}
//for 2d games
void OnTriggerEnter2D(Collider other) {
//you may check the tag of the 'other' object here to make sure if its your instantiated object
//if(other.gameObject.tag=="yourInstantiatedObjectTag")
    Destroy(other.gameObject);//dont forget to check the isTrigger of the quad or else the event will not trigger
}

您可以使用以下功能来检测对象何时离开屏幕,然后根据游戏逻辑销毁它或其他任何东西。

public bool IsOutOfScreen(GameObject o, Camera cam = null)
{
    bool result = false;
    Renderer ren = o.GetComponent<Renderer>();
    if(ren){
        if (cam == null) cam = Camera.main;
        Vector2 sdim = SpriteScreenSize(o,cam);
        Vector2 pos = cam.WorldToScreenPoint(o.transform.position);
        Vector2 min = pos - sdim;
        Vector2 max = pos + sdim;
        if( min.x > Screen.width || max.x < 0f || 
            min.y > Screen.height || max.y < 0f) {
                result = true;
        }
    }
    else{
        //TODO: throw exception or something
    }
    return result;
}
public Vector2 SpriteScreenSize(GameObject o, Camera cam = null)
{
    if (cam == null) cam = Camera.main;
    Vector2 sdim = new Vector2();
    Renderer ren = o.GetComponent<Renderer>() as Renderer;
    if (ren)
    {            
        sdim = cam.WorldToScreenPoint(ren.bounds.max) -
            cam.WorldToScreenPoint(ren.bounds.min);
    }
    return sdim;
}