unity 3d 简单协程—最简单的小球的显示与隐藏的实现。
1.打开unity。
2.创建3D Object 中的 Plane(平面)。
3.创建3D Object 中的 Sphere(球),坐标归零,y坐标填0.5。
4.创建空物体 Create Empty ,坐标归零。
Hierarchy 菜单显示如下:
5.选中 GameObject ,点击Add Component 输入名称HideAndShow,创建脚本。
创建完成,打开。全盘复制如下代码,保存。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HideAndShow : MonoBehaviour {
public GameObject sphere;
void Start () {
//开始隐藏协程
StartCoroutine("Hide");
}
//等待2秒后隐藏
IEnumerator Hide() {
yield return new WaitForSeconds(2);
sphere.SetActive(false);
//开始显示协程
StartCoroutine("Show");
}
//等待2秒后显示
IEnumerator Show() {
yield return new WaitForSeconds(2);
sphere.SetActi