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

时间:2014-04-22 11:13:48

标签: c# android unity3d destroy

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

生成预制件(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");
    }

但是,销毁对象的代码不起作用,但也没有出现错误。它输出&#34; Destroy&#34;在预制件离开屏幕之后,所以我知道破坏它们的代码有问题。

由于

3 个答案:

答案 0 :(得分:7)

当位置不在相机位置时,您可以制作一个会破坏自我的组件,然后将此组件附加到障碍物上。

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

答案 1 :(得分:1)

你可以在屏幕的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
}

答案 2 :(得分:0)

您可以使用以下功能检测对象何时不在屏幕上,然后根据您的游戏逻辑销毁它或其他任何内容。

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;
}
相关问题