unity3d销毁游戏对象_如何在Unity 3D中销毁游戏对象

unity3d销毁游戏对象

As important instantiating and modifying gameObjects is in a game, it's equally important to destroy them when they are not required.

由于在游戏中实例化和修改gameObjects很重要,因此在不需要它们时销毁它们同样重要。

Main reasons for destroying gameObjects include managing memory, getting rid of not-needed gameObjects (Like those that go beyond the camera and don't return), or even game-related events (Getting hit by a bullet).

销毁游戏对象的主要原因包括管理内存清除不需要的游戏对象 (就像那些超出摄像头且不会返回的对象),甚至是与游戏相关的事件(被子弹击中)。

Destroy(gameObject): That's the code which is used to destroy a gameObject. Fairly simple, isn't it? Let's rewind and have a slightly more deeper look at what's going on. If you try to explore the Destroy method in your IDE, you will see that the Destroy method has 2 variants(overloaded forms).

Destroy(gameObject):这是用于销​​毁游戏对象的代码。 很简单,不是吗? 让我们倒带,对发生的事情有更深入的了解。 如果您尝试在IDE中探索Destroy方法,则会看到Destroy方法具有2个变体(重载表格)。

The first overload form simply Destroys the gameObject that's input as a parameter, and nothing else.

第一个重载形式只是销毁作为参数输入的gameObject,而没有其他任何事情。

While, the second overload form, involves another parameter time, t as a float variable. This will wait for the time specified in seconds before destroying the gameObject. This variant of the Destroy method comes very handy when you want the gameObject to finish up some other stuff/task/calculation before destroying itself. For example, playing a dying animation or adding a score.

而第二种重载形式则涉及另一个参数time, t作为float变量。 这将在消灭gameObject之前等待指定的时间(以秒为单位) 。 当您希望gameObject在销毁自身之前完成一些其他工作/任务/计算时, Destroy方法的这种变体非常方便。 例如,播放垂死的动画或添加乐谱。

Till our last tutorial, we've successfully added a target that outputs a message to the console when hit by a bullet. Now let's make the target vanish, once its hit, shall we? How do we do that? We simply add the following lines to our already existing target's script:

直到上一教程为止 ,我们已经成功添加了一个目标,当目标被子弹击中时,该目标将向控制台输出一条消息。 现在,让目标消失,一旦击中,就可以了吗? 我们该怎么做? 我们只需在已经存在的目标脚本中添加以下几行:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TargetBehaviour : MonoBehaviour
{
    
    void onCollisionEnter2D(Collision2D col) 
    {
        // When target is hit
        if(col.gameObject.tag == "Bullet")
        {
            Debug.Log("Target was Hit!");
        }
        // object which collided
        Destroy(col.gameObject());
        // object with which it collided
        Destroy(gameObject);
    }
    
}

What's going on here? We're telling the script to destroy the gameObject included in col's data. In simpler terms, it means we're telling the script to destroy the bullet(fireball), since we don't want our bullet to keep going after it destroys our target.

这里发生了什么? 我们告诉脚本销毁col数据中包含的gameObject。 简而言之,这意味着我们要告诉脚本销毁子弹(火球),因为我们不希望子弹在销毁目标之后继续前进。

In the second Destroy statement (our magic line), the gameObject destroys itself. Remember that simply using the term gameObject references that gameObject which the script is attached to, in our case, the target. Save up the script, and fire up the game!

在第二个Destroy语句(我们的魔术行)中,gameObject销毁了自己。 请记住,仅使用术语gameObject引用脚本所附加到的gameObject(在我们的示例中为目标)。 保存脚本,然后启动游戏!

Destroying Game Objects

INFO: In editing mode.

信息:在编辑模式下。

Destroying Game Objects

INFO: In Play mode, after the target is hit

信息:在播放模式下,击中目标后

Once we hit the target, the bullet and the target disappear! Have a look at the Hierarchy, and you'll notice that the target and bullet's clone gameObjects both are missing. They have simply vanished out of existence once they get destroyed in-game. Since we placed the target in the scene while building it, it will always come back whenever we re-enter the scene, that is, whenever we enter Play mode.

一旦我们击中目标,子弹和目标就会消失! 看一下层次结构,您会注意到目标和项目符号的克隆gameObjects都丢失了。 一旦在游戏中被摧毁,它们就消失了。 由于我们在构建目标时将目标放置在场景中,因此只要我们重新进入场景(即,无论何时进入“播放”模式),目标都会始终返回。

So there you have it! Destroying gameObjects in itself is a very easy concept, since it involves only one line of code, but the real magic happens when you call the method along with other statements to make the object's destruction look and feel real and right.

所以你有它! 销毁gameObjects本身是一个非常简单的概念,因为它只涉及一行代码,但是真正的魔力发生在您调用该方法以及其他语句以使对象的销毁外观真实,正确时。

We have done quite a lot this time. We gave our character the ability to shoot bullets, and we've learned the concepts of prefabs and instantiation. Next, we gave our character a target to shoot, thus understanding collision detection, and finally, we made the targets destroy themselves once they got hit, finishing things off with object destruction.

这次我们做了很多事情。 我们赋予了角色射击子弹的能力,并且我们了解了预制和实例化的概念。 接下来,我们给角色一个可以射击的目标,从而了解碰撞检测,最后,一旦目标被击中,我们就使目标自行销毁,并完成物体销毁工作。

练习练习 (Practice Exercise)

  1. Try to give your target a Health value, meaning you have to shoot it multiple times to break it. This will test not only your logic in making games, but your ability to reason in basic programming logic as well.

    尝试给目标设定一个健康值,这意味着您必须多次射击才能破坏它。 这不仅会测试您制作游戏的逻辑,还会测试您在基本编程逻辑中的推理能力。

    HINT: A simple integer variable and an if statement can go a long way.

    提示:一个简单的integer变量和一个if语句可以大有帮助。

  2. Try to create an end-region for bullets out of the camera coverage area. If a bullet goes out of the camera region and touches this end region, it gets destroyed, instead of aimlessly going off into dead space. This is an efficient way of managing memory when you get into bigger, more complex games.

    尝试在相机覆盖区域之外为子弹创建一个末端区域。 如果子弹从摄像机区域中出来并碰到该末端区域,它将被摧毁,而不是漫无目的地飞向死角。 当您进入更大,更复杂的游戏时,这是管理内存的有效方法。

翻译自: https://www.studytonight.com/game-development-in-2D/destroying-game-objects

unity3d销毁游戏对象

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页