码迷,mamicode.com
首页 > 其他好文 > 详细

StartCoroutine/StopCoroutineInvoke

时间:2016-01-19 15:53:44      阅读:292      评论:0      收藏:0      [点我收藏+]

标签:

 

using UnityEngine;
using System.Collections;

public class CoroutineTest : MonoBehaviour {

    void Start () {
        print("Starting " + Time.time);
        StartCoroutine(WaitAndPrint(0.2F));
        print("Before WaitAndPrint Finishes " + Time.time);
    }

    IEnumerator WaitAndPrint(float waitTime) {
        print("StartCoroutine1 " + Time.time);
        StartCoroutine("DoSomething", 2.0f);
        print("StartCoroutine2 " + Time.time);
        yield return new WaitForSeconds(waitTime);
        print("StopCoroutine1 " + Time.time);
        StopCoroutine("DoSomething");
        print("StopCoroutine2 " + Time.time);
    }

    IEnumerator DoSomething(float someParameter) {
        while (true) {
            print("DoSomething Loop");
            yield return null;
        }
    }

    void Projectile() {
        print("Projectile " + Time.time);
    }

    void LaunchProjectile() {
        print("LaunchProjectile " + Time.time);
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown(KeyCode.Space) && !IsInvoking("LaunchProjectile")) {
            InvokeRepeating("LaunchProjectile", 2f, 0.5f);
            Invoke("Projectile", 2f);
        }
        if (Input.GetKeyDown(KeyCode.X)) {
            Debug.Log("CancelInvoke"); 
            CancelInvoke("LaunchProjectile");
        }
    }
}

 

#region Assembly UnityEngine.dll, v0.0.0.0
// D:\Program\Unity Project\test\Library\UnityAssemblies\UnityEngine.dll
#endregion

using System;
using System.Collections;
using UnityEngine.Internal;
using UnityEngine.Scripting;

namespace UnityEngine {
    // Summary:
    //     MonoBehaviour is the base class every script derives from.
    [RequiredByNativeCode]
    public class MonoBehaviour : Behaviour {
        [WrapperlessIcall]
        public MonoBehaviour();

        // Summary:
        //     Disabling this lets you skip the GUI layout phase.
        public bool useGUILayout { get; set; }

        // Summary:
        //     Cancels all Invoke calls on this MonoBehaviour.
        public void CancelInvoke();
        //
        // Summary:
        //     Cancels all Invoke calls with name methodName on this behaviour.
        //
        // Parameters:
        //   methodName:
        [WrapperlessIcall]
        public void CancelInvoke(string methodName);
        //
        // Summary:
        //     Invokes the method methodName in time seconds.
        //
        // Parameters:
        //   methodName:
        //
        //   time:
        [WrapperlessIcall]
        public void Invoke(string methodName, float time);
        //
        // Summary:
        //     Invokes the method methodName in time seconds, then repeatedly every repeatRate
        //     seconds.
        //
        // Parameters:
        //   methodName:
        //
        //   time:
        //
        //   repeatRate:
        [WrapperlessIcall]
        public void InvokeRepeating(string methodName, float time, float repeatRate);
        //
        // Summary:
        //     Is any invoke pending on this MonoBehaviour?
        public bool IsInvoking();
        //
        // Summary:
        //     Is any invoke on methodName pending?
        //
        // Parameters:
        //   methodName:
        [WrapperlessIcall]
        public bool IsInvoking(string methodName);
        //
        // Summary:
        //     Logs message to the Unity Console (identical to Debug.Log).
        //
        // Parameters:
        //   message:
        public static void print(object message);
        //
        // Summary:
        //     Starts a coroutine.
        //
        // Parameters:
        //   routine:
        public Coroutine StartCoroutine(IEnumerator routine);
        //
        // Summary:
        //     Starts a coroutine named methodName.
        //
        // Parameters:
        //   methodName:
        //
        //   value:
        [ExcludeFromDocs]
        public Coroutine StartCoroutine(string methodName);
        //
        // Summary:
        //     Starts a coroutine named methodName.
        //
        // Parameters:
        //   methodName:
        //
        //   value:
        [WrapperlessIcall]
        public Coroutine StartCoroutine(string methodName, object value);
        [WrapperlessIcall]
        public Coroutine StartCoroutine_Auto(IEnumerator routine);
        //
        // Summary:
        //     Stops all coroutines running on this behaviour.
        [WrapperlessIcall]
        public void StopAllCoroutines();
        public void StopCoroutine(Coroutine routine);
        //
        // Summary:
        //     Stops the first coroutine named methodName, or the coroutine stored in routine
        //     running on this behaviour.
        //
        // Parameters:
        //   methodName:
        //     Name of coroutine.
        //
        //   routine:
        //     Name of the function in code.
        public void StopCoroutine(IEnumerator routine);
        //
        // Summary:
        //     Stops the first coroutine named methodName, or the coroutine stored in routine
        //     running on this behaviour.
        //
        // Parameters:
        //   methodName:
        //     Name of coroutine.
        //
        //   routine:
        //     Name of the function in code.
        [WrapperlessIcall]
        public void StopCoroutine(string methodName);
    }
}

 

  • yield; The coroutine will continue after all Update functions have been called on the next frame.
    yield:协程在所有的Update函数于下一帧调用后继续执行。
  • yield WaitForSeconds(2); Continue after a specified time delay, after all Update functions have been called for the frame
    yield WaitForSeconds(2):在一个指定的时间延迟后继续执行,在所有的Update函数被调用之后。
  • yield WaitForFixedUpdate(); Continue after all FixedUpdate has been called on all scripts
    yield WaitForFixedUpdate():在所有脚本上所有的FixedUpdate被调用之后继续执行。
  • yield WWW Continue after a WWW download has completed.
    yield WWW:在WWW加载完成之后继续执行。
  • yield StartCoroutine(MyFunc); Chains the coroutine, and will wait for the MyFunc coroutine to complete first.
    yield StartCoroutine(MyFunc):用于链接协程,此将等待MyFunc协程完成先

         yield  WaitForEndOfFrame();  用于本帧渲染完

     yield return new WaitForEndOfFrame(); // 本帧渲染完后

StartCoroutine/StopCoroutineInvoke

标签:

原文地址:http://www.cnblogs.com/panyingying/p/5142376.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!