Unity's timed call

Source: Internet
Author: User

1.Invoke (string methodname,float time)

Call the MethodName function at a certain time

using UnityEngine; using System.Collections; public class example : MonoBehaviour {      public Rigidbody projectile;      void LaunchProjectile() {          Rigidbody instance = Instantiate(projectile);          instance.velocity = Random.insideUnitSphere * 5 ;      }      public void Awake() {          Invoke( "LaunchProjectile" , 2 );      } }2.InvokeRepeating (String methodname,float time,float repeatrate)

Call the MethodName function every once in a while

Invokes the method MethodName in time seconds.

Call the MethodName method in time seconds; The method that specifies the method name according to the timing

After the first invocation repeats calling that function every repeatrate seconds.

It is called once every repeatrate time from the first call.

using UnityEngine; using System.Collections; public class example : MonoBehaviour {      public Rigidbody projectile;      void LaunchProjectile() {          Rigidbody instance = Instantiate(projectile);          instance.velocity = Random.insideUnitSphere * 5 ;      }      public void Awake() {          InvokeRepeating( "LaunchProjectile" , 2 , 0 .3F);  //2秒后,没0.3f调用一次      } }3.CancelInvoke (String methodName)

Cancel all calls in this script

Cancels all Invoke calls in this monobehaviour.

Cancels all calls on this monobehaviour.

public class example : MonoBehaviour {      public Rigidbody projectile;      void Update() {          if (Input.GetButton( "Fire1" ))              CancelInvoke();      }      void LaunchProjectile() {          instance = Instantiate(projectile);          instance.velocity = Random.insideUnitSphere * 5 ;      }      public void Awake() {          InvokeRepeating( "LaunchProjectile" , 2 , 0 .3F);      } } function LaunchProjectile () {      instance = Instantiate(projectile);      instance.velocity = Random.insideUnitSphere * 5 ; }4. (bool) isinvoking (String methodName)

using UnityEngine; using System.Collections; public class example : MonoBehaviour {      public Rigidbody projectile;      void Update() {          if (Input.GetKeyDown(KeyCode.Space) && !IsInvoking( "LaunchProjectile" ))  //如果这个方法不在调用并且等待了2秒              Invoke( "LaunchProjectile" , 2 );      }      void LaunchProjectile() {          Rigidbody instance = Instantiate(projectile);          instance.velocity = Random.insideUnitSphere * 5 ;      } }

Unity's timed call

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.