一,几种Invoke调用方式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class API07Invoke : MonoBehaviour {
// Use this for initialization
void Start () {
//Invoke("Attack", 2);
//延时2s调用Attack
//InvokeRepeating("Attack", 4, 2);
//延时4s开始调用,每2s调用一次
//CancelInvoke();
//不指定参数,取消this脚本里的所有invoke,指定参数取消对应invoke
//IsInvoking();
//不指定参数,判断this脚本里的是否有invoke在等待队列中,指定参数判断对应invoke
}
void Attack() {
Debug.Log("Attack!!!");
}
}
二,正常程序运行方式
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class API08Coroutine : MonoBehaviour {
public GameObject cube;
// Use this for initialization
void Start() {
Debug.Log("123");
ChangeColor();
Debug.Log("321");
}
void ChangeColor() {
Debug.Log("456");
GameObject cube2 = GameObject.Instantiate(cube);//可忽略,下同
cube2.name = "cubu2";
cube2.transform.position += new Vector3(2, 0, 0);
cube2.GetComponent<MeshRenderer>().material.color = Color.magenta;
cube.GetComponent<MeshRenderer>().material.color = Color.gray;
Debug.Log("654");
}
}
三,协程运行方式:
协程,在主程序之外创建的程序,在协程开启后协程与主程同时运行,不存在先后关系
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class API08Coroutine : MonoBehaviour {
public GameObject cube;
// Use this for initialization
void Start() {
Debug.Log("123");
StartCoroutine(ChangeColor());
//协程方法开启后继续执行下面的代码Debug.Log("321"),不会停下来等待
Debug.Log("321");
}
//协程方法定义
//1.返回值 IEnumerator
//2.返回参数是使用yield return
//3.协程方法的调用StartCoroutine(method())
IEnumerator ChangeColor() {
Debug.Log("456");
GameObject cube2 = GameObject.Instantiate(cube);
cube2.name = "cubu2";
cube2.transform.position += new Vector3(2, 0, 0);
cube2.GetComponent<MeshRenderer>().material.color = Color.magenta;
cube.GetComponent<MeshRenderer>().material.color = Color.gray;
Debug.Log("654");
yield return null;
}
}