【发布时间】:2021-04-29 12:06:37
【问题描述】:
我正在创建一些由许多不同矩形组成的线。线条和矩形都是由代码创建的。 我用矩形的顶点做一些逻辑,并在我的实例化矩形中更新该信息。这真的很好用。
然后在每个矩形上我有 2 个脚本,一个存储数据,另一个存储协程,以在线条和矩形移动位置后满足某些条件时使矩形闪烁或停止闪烁。
它不起作用,它可能必须使用其他一些外部脚本甚至字典或列表来完成,但由于我是协程的新手,我想知道是否可以通过简单地从每个实例化对象中做到这一点在游戏过程中检查这些 bool 和 int 变量。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rectanguloAnim : MonoBehaviour{
rectangulo rect; //script containing data from this rectangle
Color32 colOrig;
Material material;
IEnumerator currentFlashCoroutine;
void Start()
{
rect = GetComponent<rectangulo>();
colOrig = GetComponent<rectangulo>().colorOriginal;
material = GetComponent<Renderer>().material;
}
void Update()
{
// this bool and int change when the line and rect are interacted with
if (rect.isIntermediate == true || rect.activeDirection != 0)
{
if (currentFlashCoroutine != null)
{
StopCoroutine(currentFlashCoroutine);
}
currentFlashCoroutine = ChangeColor(material, colOrig, Color.white);
StartCoroutine(currentFlashCoroutine);
}
else
{
if (currentFlashCoroutine != null)
{
StopCoroutine(currentFlashCoroutine);
}
}
}
IEnumerator ChangeColor(Material toChange, Color32 startColor, Color32 endColor)
{
float t = 0;
float colorDuration = Random.Range(2.49f, 6f);
while (t < colorDuration)
{
float timeDuration = Random.Range(.55f, .95f);
if (t > timeDuration)
{
t = 0;
}
t += Time.deltaTime;
toChange.color = Color.Lerp(startColor, endColor, t / colorDuration);
yield return null;
}
}
}
【问题讨论】:
标签: c# unity3d instantiation coroutine