简体   繁体   English

(Unity 2D)在离开屏幕时销毁实例化的预制件吗?

[英](Unity 2D) Destroy instantiated prefab when it goes off screen?

Im making a 2D Game in Unity 2D(4.3), and I need to destroy the prefabs that get instantiated when those prefabs go off the screen. 我在Unity 2D(4.3)中制作2D游戏,我需要销毁在预制件离开屏幕时实例化的预制件。 I have written some code to spawn the Objects, but then I want to delete those prefabs when they go off screen. 我已经编写了一些代码以生成对象,但是当它们离开屏幕时,我想删除这些预制件。 Here is the code I have written so far. 这是我到目前为止编写的代码。

To Generate prefab (C#): 生成预制件(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);

}

To destroy the prefab(C#): 销毁预制件(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");
    }

However, the code to destroy the object is not working, but is not getting an error either. 但是,销毁对象的代码不起作用,但也没有收到错误。 It does output "Destroy" after the prefabs go off screen so I know its something wrong with the code to destroy them. 预制件离开屏幕后,它会输出“销毁”,因此我知道销毁它们的代码有问题。

Thanks 谢谢

You can make a component that will destroy self when position is out of camera, then attach this component to the obstacle. 您可以制作一个组件,当位置超出摄影机时会破坏自身,然后将该组件附加到障碍物上。

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

And this component attach to the obstacle will destroy self. 并且此组件附着在障碍物上会破坏自我。

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

You can make 4 quads on the 4 sides of screens and with them you attach boxCollider and check its isTrigger. 您可以在屏幕的4个侧面制作4个四边形,并通过它们附加boxCollider并检查其isTrigger。 After that add the below script to each quad that checks that if something collides with it in its OnTriggerEnter and in there you may check the tag of the instantiated object or you may destroy each object that collides with it(depends on the game). 之后,将以下脚本添加到每个四边形中,以检查是否有人在其OnTriggerEnter中发生碰撞,然后在其中检查实例化对象的标签,或者销毁与该对象碰撞的每个对象(取决于游戏)。 Use the code below 使用下面的代码

//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
}

You can use the following function to detect when the object is out of screen, and then destroy it or anything else according to your game logic. 您可以使用以下功能来检测对象何时超出屏幕,然后根据您的游戏逻辑销毁它或其他任何东西。

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

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

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