返回介绍

Object.Destroy 销毁

发布于 2019-12-18 15:38:09 字数 2601 浏览 1024 评论 0 收藏 0

JavaScript => static function Destroy(obj: Object, t: float = 0.0F): void;
C# => static void Destroy(Object obj, float t = 0.0F);

Description

Removes a gameobject, component or asset.

删除一个游戏对象,组件或者资源。

The object obj will be destroyed now or if a time is specified t seconds from now. If obj is a Component it will remove the component from the GameObject and destroy it. If obj is a GameObject it will destroy the GameObject, all its components and all transform children of the GameObject. Actual object destruction is always delayed until after the current Update loop, but will always be done before rendering.

物体obj现在被销毁或在指定了t时间过后销毁。如果obj是组件,它将从GameObject销毁组件component。如果obj是GameObject它将销毁GameObject全部它的组件和GameObject全部transform子物体。实际物体的销毁总是延迟到当前更新循环后,但总是渲染之前完成。

JavaScript:

	// Kills the game object
	Destroy (gameObject);
 
	// Removes this script instance from the game object
	Destroy (this);
 
	// Removes the rigidbody from the game object
	Destroy (rigidbody);
 
	// Kills the game object in 5 seconds after loading the object
	Destroy (gameObject, 5);
 
	// When the user presses Ctrl, it will remove the script 
	// named FooScript from the game object
	function Update () {
		if (Input.GetButton ("Fire1") && GetComponent (FooScript))
			Destroy (GetComponent (FooScript));
	}

C#:

DestroyTest.cs
using UnityEngine;
using System.Collections;
 
public class DestroyTest : MonoBehaviour {
 
	void Start () {
		// 销毁这个对象
		Destroy (gameObject);
 
		// 从此对象删除这个脚本实例
		Destroy (this);
 
		// 删除此对象的Rigidbody组件
		Destroy (rigidbody);
 
		// 5秒后销毁对象
		Destroy (gameObject, 5);
	}
 
	// 当按下Ctrl键删除Rigidbody组件
	void Update () {
		if (Input.GetButton ("Fire1") && GetComponent <Rigidbody>())
			Destroy (GetComponent <Rigidbody>());
	}
}

Destroy is inherited from the UnityEngine.Object base class. Javascript users should consider making a call to UnityEngine.Object.Destroy, rather than Object.Destroy to avoid references being resolved to the .Net System.Object class.

Destroy是继承于UnityEngine.Object基类,JavaScript的用户应考虑调用UnityEngine.Object.Destroy,而不是Object.Destroy避免引用被解析为System.Object类。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文