关于gameobject销毁,文件enabled = false ,,协程执行情况的测试

本文详细探讨了在Unity引擎中,如何通过`Destroy(gameObject)`和`enabled = false`来销毁游戏对象,并研究了这两种方法对协程(Coroutine)执行的影响。在测试代码中,启动了一个无限循环的协程,然后在不同条件下销毁或禁用游戏对象,观察协程的运行状态。实验结果显示,`Destroy(gameObject)`会立即停止所有协程,而`enabled = false`则会使游戏对象及其组件暂停,但协程会继续执行直到完成当前循环。这对于理解和优化Unity项目的资源管理至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using UnityEngine;
using System.Collections;

public class colliderCnt : MonoBehaviour {

    // Use this for initialization
    Collider[] collider;
    void Start () {
        collider = GetComponents<Collider >();
        //Debug.Log (collider.Length);
        Debug.Log (transform.childCount);
        //Invoke ("f");
        //Debug.Log ("One");
        StartCoroutine ("f");
        Destroy (gameObject,8);
        //Destroy (gameObject);
        //GetComponent<colliderCnt> ().enabled = false;
    }
    IEnumerator f()
    {
        //Debug.Log ("Two");
        while (true) {
            yield return new WaitForSeconds(2);
            Debug.Log("On");
            f ();
            //StartCoroutine(f());// create a new coroutine !!
        }
    }
    // Update is called once per frame
    void Update () {
    
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值