• unity执行顺序问题(如何再次执行start方法)


     

     分类:

    unity执行顺序的文章已经很多了,其实不用看文章,那么麻烦,一张图就搞定了!

    Look:

    这里看到最特殊最常用的应该就是OnEnable了。OnEnable是在Awake之后Start之前执行的,特殊之处就是他会在物体隐藏之后再次显示时再次调用,而Start和Awake是做不到这一点!

    为了证明宝宝没有说谎,请看实例:

    下面有一个sphere(默认隐藏)和一个cube,在按钮上绑定一脚本quite点击按钮会让cube隐藏让sphere显示,而按键盘O键会让cube显示让sphere隐藏。在cube上绑定了一个脚本TESTONE。


     

    按钮上绑定的脚本:

    [csharp] view plain copy
     
    1. using UnityEngine;  
    2. using System.Collections;  
    3.   
    4. public class quite : MonoBehaviour {  
    5.     public GameObject[] GO;  
    6.     // Use this for initialization  
    7.     void Start () {  
    8.       
    9.     }  
    10.       
    11.     // Update is called once per frame  
    12.     void Update () {  
    13.         if (Input.GetKey(KeyCode.O))//按键盘O键  
    14.         {  
    15.            // Debug.Log("cube出现");  
    16.             GO[1].SetActive(false);  
    17.             GO[0].SetActive(true);  
    18.         }  
    19.     }  
    20.     public void Clickthisbutton()//点击按钮  
    21.     {  
    22.       //  Debug.Log("球出现");  
    23.         GO[0].SetActive(false);  
    24.         GO[1].SetActive(true);  
    25.       //  Application.Quit();  
    26. }  
    27. }  

     

    然后再看在cube上的脚本;
    [csharp] view plain copy
     
    1. using UnityEngine;  
    2. using System.Collections;  
    3.   
    4. public class TESTONE : MonoBehaviour {  
    5.     void Awake()  
    6.     {  
    7.         Debug.Log("Awake---1cube");  
    8.     }  
    9.     void OnEnable()  
    10.     {  
    11.         Debug.Log("OnEnable---1cube");  
    12.     }  
    13.     // Use this for initialization  
    14.     void Start () {  
    15.         Debug.Log("START--1cube");  
    16.     }  
    17.       
    18.     // Update is called once per frame  
    19.     void Update () {  
    20.       
    21.     }  
    22. }  

     

    下面运行一下看下图的Log;cube上log的执行顺序很明显(这些方法全都只执行一次):

    然后点击按钮看下图:cube已经隐藏,而sphere出现,所有的log还是原来的。


    然后我们清理掉log,按键盘O键看下图;看到cube再次显示,但是log中只有OnEable方法执行了。怎么样宝宝没骗你们吧!!!

    那么如何再次执行AWake 或Start方法呢?不用想我肯定是开始说废话了,没错,那就是在OnEable方法里调用这两个方法(如果是在其他脚本写的OnEable方法那就要把那两个改成Public方法了)!好吧,这样其实在最开始就会执行两次Start和Awake方法。

    [csharp] view plain copy
     
    1. using UnityEngine;  
    2. using System.Collections;  
    3.   
    4. public class TESTONE : MonoBehaviour {  
    5.  public void Awake()  
    6.     {  
    7.         Debug.Log("Awake---1cube");  
    8.     }  
    9.   void OnEnable()  
    10.     {  
    11.         Debug.Log("OnEnable---1cube");  
    12.         Start();  
    13.         Awake();  
    14.     }  
    15.     // Use this for initialization  
    16.     public void Start () {  
    17.         Debug.Log("START--1cube");  
    18.   
    19.     }  
    20.       
    21.     // Update is called once per frame  
    22.     void Update () {  
    23.       
    24.     }  
    25. }  


    所以当遇到类似的情况就用宝宝的大法吧!哈哈哈!

  • 相关阅读:
    git的简单操作
    angularjs的基础
    针对IE8透明度设置及一些简单的兼容问题
    sublime 部分常用的快捷键
    Linux简单的操作
    AngularJS 简单的介绍
    html5的viewport与css3的媒体查询
    css的优先级的相关内容
    (转) vmware 切换桥接模式
    从nginx的编译安装,了解编译安装原理(转)
  • 原文地址:https://www.cnblogs.com/w-wfy/p/7281761.html
Copyright © 2020-2023  润新知