纳金网

标题: 延迟执行脚本语句 [打印本页]

作者: 狂风大尉    时间: 2014-6-26 20:01
标题: 延迟执行脚本语句

在Unity3D中,有yield语句它负责延迟操作,yield return WaitForSeconds(3.0); //等待 3 秒
查看unity3d脚本手册,用法需要在相应的格式。

下面代码含义就是,加载图片显示等待6秒后进入场景level1中。
  1. using UnityEngine;
  2.         using System.Collections;
  3.         
  4.         public class init : MonoBehaviour {
  5.         
  6.                 // Use this for initialization
  7.                 public Texture img;
  8.                 private bool bl = false;
  9.                 public float waitTime ;
  10.                 void Start () {
  11.                  StartCoroutine( wait(6.0f) );
  12.         
  13.                 }
  14.                
  15.                 // Update is called once per frame
  16.                 void Update () {
  17.                  if (bl)
  18.                  Application.LoadLevel(1);
  19.         
  20.                 }
  21.         
  22.                 void OnGUI()
  23.                 {
  24.                  GUI.DrawTexture(new Rect(0,0,1024,768),img);
  25.                 }
  26.         
  27.                 IEnumerator wait(float time)
  28.                 {
  29.                  yield return new WaitForSeconds(waitTime);
  30.                  bl = true;
  31.                 }
  32.         }
复制代码

作者: hyui    时间: 2014-6-26 21:23
感谢分享这个!
作者: HIDEOKOJIMA    时间: 2014-6-26 22:38
感谢分享这个!
作者: wucnj    时间: 2014-6-27 13:18
感谢分享!!!




欢迎光临 纳金网 (http://course.narkii.com/club/) Powered by Discuz! X2.5