紅綠燈

暖暖De幸福發表於2024-07-20
[Tooltip("嚴格按照紅、黃、綠排序")]
public Material[] colors;

int index = 0;

// Start is called before the first frame update
void Start()
{
    ChangeColor();
}

// Update is called once per frame
void Update()
{

}

private void ChangeColor()
{
    var mesh = GetComponent<MeshRenderer>();
    mesh.material = colors[index];


    if (index == 0)
    {
        Invoke("ChangeColor", 8); //紅到黃、間隔8秒
    }
    else if (index == 1)
    {
        Invoke("ChangeColor", 3); //黃到綠,間隔3秒
    }
    else if (index == 2)
    {
        Invoke("ChangeColor", 5); //綠到黃、間隔5秒
    }

    index++;

    if (index % colors.Length == 0)
    {
        index = 0;
    }
}

相關文章