进入Unity先建立三个floder
建立六个Cube,在建立一个空物体挂载脚本
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class paoma : MonoBehaviour
{
private GameObject[] _cubes;
private Color[] _colors;
void Start()
{
_colors = new Color[] { Color.black, Color.blue, Color.green, Color.red, Color.yellow, Color.white };
_cubes = GameObject.FindGameObjectsWithTag("MyCube");
StartCoroutine("changeColorEnumerator");
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Q))
{
StopCoroutine("changeColorEnumerator");
}
}
IEnumerator changeColorEnumerator()
{
int j = 0;
while (true)
{
for (int i = 0; i < _colors.Length; i++)
{
_cubes[i].GetComponent<MeshRenderer>().material.color = _colors[(i + j) % 6];
}
yield return new WaitForSeconds(3);
j++;
if (j >= int.MaxValue - 7)
{
j = 0;
}
}
}
}