C# (Unity 2D)在实例化的预置离开屏幕时销毁它?

C# (Unity 2D)在实例化的预置离开屏幕时销毁它?,c#,android,unity3d,destroy,C#,Android,Unity3d,Destroy,我在Unity2D(4.3)中制作了一个2D游戏,我需要销毁那些在预设离开屏幕时被实例化的预设。我已经编写了一些代码来生成这些对象,但是我想在它们离开屏幕时删除它们。 这是我到目前为止编写的代码 要生成预置(C#),请执行以下操作: void更新(){ 浮动y=随机范围(-4.53f,2.207f); 如果(xScreen.height | | screenPosition.y

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

要生成预置(C#),请执行以下操作:

void更新(){
浮动y=随机范围(-4.53f,2.207f);
如果(x<2000){
实例化(障碍,新向量3(y,x*6.0f,0),四元数标识);
x++;
}
//Debug.Log(x);
}
要销毁预制件(C#),请执行以下操作:

/*************************************************************************************************
*获得实例化的障碍
*并在出口处销毁它
*节省内存
**************************************************************************************************/
游戏对象克隆=(游戏对象)实例化(障碍);
/*if(clone.transform.position.y==-11)
{
破坏(克隆);
Debug.Log(“销毁”);
}*/
Vector2屏幕位置=Camera.main.WorldToScreenPoint(transform.position);
if(screenPosition.y>Screen.height | | screenPosition.y<0)
{
摧毁(游戏对象);
Debug.Log(“销毁”);
}
但是,销毁对象的代码不起作用,但也不会出现错误。它会在预置离开屏幕后输出“销毁”,所以我知道销毁它们的代码有问题


谢谢

您可以制作一个组件,当位置超出摄像头时,该组件会破坏自身,然后将该组件连接到障碍物上

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++;
    }
}
void Update(){
浮动y=随机范围(-4.53f,2.207f);
如果(x<2000){
游戏对象克隆=(游戏对象)实例化(障碍,新向量3(y,x*6.0f,0),四元数.identity);
clone.AddComponent(typeof(destroy));
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);
    }
}
公共类:单一行为{
无效更新(){
Vector2屏幕位置=Camera.main.WorldToScreenPoint(transform.position);
if(screenPosition.y>Screen.height | | screenPosition.y<0)
摧毁(这个游戏对象);
}
}

您可以制作一个组件,当位置超出摄像头时,该组件会破坏自身,然后将该组件连接到障碍物上

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++;
    }
}
void Update(){
浮动y=随机范围(-4.53f,2.207f);
如果(x<2000){
游戏对象克隆=(游戏对象)实例化(障碍,新向量3(y,x*6.0f,0),四元数.identity);
clone.AddComponent(typeof(destroy));
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);
    }
}
公共类:单一行为{
无效更新(){
Vector2屏幕位置=Camera.main.WorldToScreenPoint(transform.position);
if(screenPosition.y>Screen.height | | screenPosition.y<0)
摧毁(这个游戏对象);
}
}

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

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

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

//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;
}
public bool IsOutOfScreen(游戏对象o,摄像头=null)
{
布尔结果=假;
渲染器ren=o.GetComponent();
如果(任){
如果(cam==null)cam=Camera.main;
Vector2 sdim=精神扫描(o,cam);
Vector2位置=凸轮世界扫描点(o.变换位置);
向量2 min=pos-sdim;
矢量2最大值=位置+sdim;
如果(最小x>Screen.width | |最大x<0f | |
最小y>屏幕高度| |最大y<0f){
结果=真;
}
}
否则{
//TODO:抛出异常还是什么
}
返回结果;
}
公共矢量2 SpriteScreenSize(游戏对象o,摄像头=null)
{
如果(cam==null)cam=Camera.main;
Vector2 sdim=新Vector2();
渲染器ren=o.GetComponent()作为渲染器;
如果(任)
{            
sdim=cam.WorldToScreenPoint(ren.bounds.max)-
cam.WorldToScreenPoint(ren.bounds.min);
}
返回sdim;
}

您可以使用以下功能检测对象是否在屏幕外,然后根据游戏逻辑销毁它或其他任何东西

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;
}
public bool IsOutOfScreen(游戏对象o,摄像头=null)
{
布尔结果=假;
渲染器ren=o.GetComponent();
如果(任){
如果(cam==null)cam=Camera.main;
Vector2 sdim=精神扫描(o,cam);
Vector2位置=凸轮世界扫描点(o.变换位置);
向量2 min=pos-sdim;
矢量2最大值=位置+sdim;
如果(最小x>Screen.width | |最大x<0f | |
最小y>屏幕高度| |最大y<0f){
结果=真;
}
}
否则{
//TODO:抛出异常还是什么
}
返回结果;
}
公共矢量2 SpriteScreenSize(游戏对象o,摄像头=null)
{
如果(cam==null)cam=Camera.main;
Vector2 sdim=新Vector2();
渲染器ren=o.GetComponent()作为渲染器;
如果(任)
{            
sdim=cam.WorldToScreenPoint(ren.bounds.max)-
凸轮轴