查看: 1667|回复: 0
打印 上一主题 下一主题

unity3d中脚本运行顺序的个人研究(荐)

[复制链接]

435

主题

2

听众

6371

积分

高级设计师

Rank: 6Rank: 6

纳金币
6372
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-8-29 10:49:26 |只看该作者 |倒序浏览
为一个场景对象添加上三个脚本,在场景开始运行时,脚本中的Start方法将会按照脚本添加的顺序来依次运行,而Update方法却是完全相反

      比如:有A1、A2、A3三个脚本,它们的添加顺序是A1、A3、A2,那么在场景运行时,程序运行顺序是A1.Start()、A3.Start()、 A2.Start()、A2.Update()、A3.Update()、A1.Update()……,即使A1.Start()或A2.Update ()运算时间超过其它的方法,那么其它的方法也会等待其运行完成后才会开始运行之……



下面是三个脚本文件的例程:



jxxh01.cs



using UnityEngine;

using System.Collections;



public class jxxh01 : MonoBehaviour

{



// Use this for initialization

void Start()

{



       for (int temp02 = 0; temp02 < 5; temp02++)

       {

         for (int temp01 = 0; temp01 < 5; temp01++)

         {

            int a01 = 0;

            for (int temp = 0; temp < 50000; temp++)

            {

                   a01++;



                   a = a01;

            }

            a01 = 0;

         }

       }







       print(Time.time + " This is jxxh01 : " + a);



}



public int ForValue=10000;



int a = 0;



// Update is called once per frame

void Update () {





       for (int temp = 0; temp < ForValue; temp++) { a = temp; }

       print(Time.time + " This is jxxh01 : " + a);



}

}









jxxh02.cs







using UnityEngine;



using System.Collections;







public class jxxh02 : MonoBehaviour



{







       // Use this for initialization



void Start()



{







       for (int temp = 0; temp < 10000; temp++)



       {



         a++;



       }



       print(Time.time + " This is jxxh02 : " + a);







}







public int ForValue = 100;







int a = 0;



      



       // Update is called once per frame



       void Update () {







      



       print(Time.time + " This is jxxh02 : " + a);







       }



}



jxxh03.cs









using UnityEngine;



using System.Collections;







public class jxxh03 : MonoBehaviour



{







       // Use this for initialization



void Start()



{







       for (int temp = 0; temp < 10000; temp++)



       {



         a++;



       }



       print(Time.time + " This is jxxh03 : " + a);







}







public int ForValue = 100;







int a = 0;



      



       // Update is called once per frame



       void Update () {







      



       print(Time.time + " This is jxxh03 : " + a);







       }



}
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2024-9-22 12:46 , Processed in 0.098636 second(s), 31 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部