首页 技术 正文
技术 2022年11月16日
0 收藏 897 点赞 4,986 浏览 1817 个字

设某个组件为NewBehaviour

OnEnabled被调用的情况:

1、GameObject添加组件的时候,即AddComponet<NewBehaviour> ;

2、包含3、已添加NewBehaviour的GameObject由未激活到已激活的时候,即setActive(true) ;

4、其中2、3会调用该GameObject及其所有子对象的OnEnabled。

OnDisabled被调用的情况:

1、GameObject被销毁;

2、NewBehaviour被销毁;

3、已添加NewBehaviour的GameObject由已激活到未激活的时候,即setActive(false);

4、NewBehaviour由不可用到可用的时候,即enabled=false。

其中1、3会调用该GameObject及其所有子对象的OnEnabled。

一个简单的测试代码:

    using UnityEngine;    using System.Collections;      public class NewBehaviour : MonoBehaviour {        ;        void Awake()        {            Debug.Log ("Awake");        }        // Use this for initialization        void Start () {            ) {                Debug.Log ("NB");                NewBehaviour nb = new NewBehaviour ();                Debug.Log ("SetActive");                gameObject.SetActive (false);                gameObject.SetActive (true);                Debug.Log ("enabled");                this.enabled = false;                this.enabled = true;                Debug.Log ("Destroy");                Destroy (this);                idx = ;            }            ) {                Debug.Log ("Add");                gameObject.AddComponent<NewBehaviour> ();                Debug.Log ("Destroy go");                Destroy (gameObject);                idx = ;            }            ) {                Debug.Log ("Instantiate");                GameObject obj = Resources.Load<GameObject> ("New Prefab");                Instantiate (obj);                idx = ;            }        }          // Update is called once per frame        void Update () {          }          void OnEnable()        {            Debug.Log ("OnEnable" + idx);        }        void OnDisable()        {            Debug.Log("OnDisable" + idx);        }    }  

此外,场景里新建一个GameObject,添加NewBehaviour,在Assets里新建Resources目录,将新建的GameObject保存为Prefab。

点击运行,打印结果:

    Awake    OnEnable0    NB    [Warning]You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent().  Alternatively, your script can inherit from ScriptableObject or no base class at all    SetActive    OnDisable0    OnEnable0    enabled    OnDisable0    OnEnable0    Destroy    OnDisable0    Add    Awake    OnEnable1    Destroy go    OnDisable1    Instantiate    Awake    OnEnable2  

取消运行后,会增加一行:

OnDisable3  

由此我们可以验证上面的论点,顺便得出以下结论:

1、Awake在OnEnable之前,OnEnable在Start之前。

2、New一个Component不仅会报警告,并且并不会调用OnEnable、OnDisable等方法。

最后补充几点:

1、被销毁时先调用OnDisable再调用OnDestroy。

2、当前组件会在Start执行完之后被销毁,具体销毁时机不明……总之不会执行Update。

3、如果把Destroy换成DestroyImmediate会报错,因为当前组件已经被销毁,gameObject无法被引用到。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,036
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,522
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,370
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,151
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,784
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,866