C# 如何修复统一';s文本alpha闪烁脚本,用于在单击鼠标按钮时停止闪烁
此脚本使unity文本alpha通道从0.1变为1。 我想在单击鼠标按钮时使此文本平滑地变为0 我试着修好它C# 如何修复统一';s文本alpha闪烁脚本,用于在单击鼠标按钮时停止闪烁,c#,unity3d,C#,Unity3d,此脚本使unity文本alpha通道从0.1变为1。 我想在单击鼠标按钮时使此文本平滑地变为0 我试着修好它 public float minAlpha = 0.1f; // The minimum value of the alpha channel, from 0. public float maxAlpha = 0.9f; // The maximum value of the alpha channel, up to 1. public float timerAlpha
public float minAlpha = 0.1f; // The minimum value of the alpha channel, from 0.
public float maxAlpha = 0.9f; // The maximum value of the alpha channel, up to 1.
public float timerAlpha = 0.0f; // The conditional time for changing the magnitude of the alpha channel value.
private float minAlphaZ;
private float maxAlphaZ;
private float timerAlphaZ;
private float alphazxc;
private bool timertr = false;
private Color rezcolor;
void Start()
{
alphazxc = minAlpha;
minAlphaZ = minAlpha;
maxAlphaZ = maxAlpha;
timerAlphaZ = timerAlpha / 100.0f; // The conditional time.
rezcolor = this.GetComponent<Text>().color; // The current value of the color of the text.
}
private void FixedUpdate()
{
if (timertr == false) // Increase the minimum value of the alpha channel, to the maximum.
{
minAlphaZ += timerAlphaZ * Time.deltaTime;
alphazxc = minAlphaZ;
if (minAlphaZ >= maxAlpha)
{
timertr = true;
minAlphaZ = minAlpha;
}
}
if (timertr == true) // Decrease the maximum value of the alpha channel, to the minimum.
{
maxAlphaZ -= timerAlphaZ * Time.deltaTime;
alphazxc = maxAlphaZ;
if (maxAlphaZ <= minAlpha)
{
timertr = false;
maxAlphaZ = maxAlpha;
}
}
this.GetComponent<Text>().color = new Color(rezcolor.r, rezcolor.g, rezcolor.b, alphazxc);
}
}
public float minAlpha=0.1f;//alpha通道的最小值,从0开始。
公共浮点数maxAlpha=0.9f;//alpha通道的最大值,最多为1。
公共浮点时间RALPHA=0.0f;//更改alpha通道值大小的条件时间。
私人浮式minAlphaZ;
专用浮点maxAlphaZ;
私人浮动时间Ralphaz;
私人浮动alphazxc;
private bool timertr=false;
私人色彩;
void Start()
{
alphazxc=minAlpha;
minAlphaZ=minAlpha;
maxAlphaZ=maxAlpha;
timerAlphaZ=timerAlpha/100.0f;//条件时间。
rezcolor=this.GetComponent().color;//文本颜色的当前值。
}
私有void FixedUpdate()
{
if(timertr==false)//将alpha通道的最小值增加到最大值。
{
minAlphaZ+=timerAlphaZ*Time.deltaTime;
alphazxc=minAlphaZ;
如果(minAlphaZ>=maxAlpha)
{
timertr=true;
minAlphaZ=minAlpha;
}
}
if(timertr==true)//将alpha通道的最大值减至最小。
{
maxAlphaZ-=timerAlphaZ*Time.deltaTime;
alphazxc=maxaphaz;
如果(maxAlphaZ而不是自己做这些沉重的事情,你可以使用
用于在1秒内淡入0的示例代码
GetComponent<Text>().DoFade(0f,1.0f); // first is your target alpha value & second is the time.
GetComponent().DoFade(0f,1.0f);//第一个是目标alpha值,第二个是时间。
同样,你也可以从0变为1。首先FixedUpdate
仅用于物理。在所有其他情况下,你应该使用Update
,它被称为逐帧更新,而不是在特定的固定时间间隔内
始终重新调用GetComponent
也是非常低效的,而是只存储一次,以后再重用引用
对于您正在尝试做的事情,最好使用更易于控制和维护的
然后还有一个提示:使用检查器的属性来明确不能输入错误的值
// if possible reference this already via the Inspector
[SerializeField] private Text text;
[Range(0f, 1f)] public float minAlpha = 0.1f;
[Range(0f, 1f)] public float maxAlpha = 0.9f;
[Tooltip("The fade duration in seconds")]
public float fadeOutDuration = 1.0f;
[Tooltip("blinking speed in blinks per second")]
public float blinkingSpeed = 1.0f;
// for controlling the end of blinking on click
private bool blink;
private void Start()
{
if(!text) text = GetComponent<Text>();
// start blinking
StartCoroutine(BlinkingRoutine())
}
private IEnumerator BlinkingRoutine()
{
// get the original color and setup min, max and fadeout color once
originalColor = text.color;
var maxColor = new Color(originalColor.r, originalColor.g, originalColor.b, maxAlpha);
var minColor = new Color(originalColor.r, originalColor.g, originalColor.b, minAlpha);
var fadeOutColor = new Color(originalColor.r, originalColor.g, originalColor.b, 0.0f);
blink = true;
var passedTime = 0f;
while(blink)
{
// always loops forth and back between 0 and 1
lerpFactor = Mathf.PingPong(passedTime, 1);
// optionaly add ease-in and ease-out
//lerpFactor = Mathf.SmoothStep(0, 1, lerpFactor);
// change the order of minColor and maxColor
// depending on the one you want to start with
text.color = Color.Lerp(maxColor, minColor, lerpFactor);
// add time passed since last frame multiplied by blinks/second
passedTime += Time.deltaTime * blinkingSpeed;
// allow this frame to be rendered and continue
// from here in the next frame
yield return null;
}
// set alpha to max
text.color = new Color(originalColor.r, originalColor.g, originalColor.b, maxAlpha);
// start fading out
passedTime = 0f;
while(passedTime < fadeOutDuration)
{
var lerpFactor = passedTime / fadeDuration;
// optionally add ease-in and ease-out
//lerpFactor = Mathf.SmoothStep(0f, 1f, lerpFactor);
text.color = Color.Lerp(maxColor, fadeOutColor, lerpFactor);
passedTime += Time.deltaTime;
yield return null;
}
// just to be sure set it to 0 hard
text.color = fadeOutColor;
// optionally hide or destroy this gameObject when done
//gameObject.SetActive(false);
//Destroy(gameObject);
}
public void HandleClick()
{
// if is blinking this stops blinking and fades out
blink = false;
}
//如果可能,请通过Inspector引用此内容
[序列化字段]专用文本;
[范围(0f,1f)]公共浮点数=0.1f;
[范围(0f,1f)]公共浮点数maxAlpha=0.9f;
[工具提示(“淡入淡出持续时间,以秒为单位”)]
公共浮点数衰减持续时间=1.0f;
[工具提示(“每秒闪烁的闪烁速度”)]
公共浮动闪烁速度=1.0f;
//用于控制单击时闪烁的结束
私人布林克;
私有void Start()
{
如果(!text)text=GetComponent();
//开始闪烁
开始例行程序(闪烁例行程序())
}
私有IEnumerator闪烁例程()
{
//获取原始颜色并设置最小、最大和淡出颜色一次
originalColor=text.color;
var maxColor=新颜色(originalColor.r、originalColor.g、originalColor.b、maxapha);
var minColor=新颜色(originalColor.r、originalColor.g、originalColor.b、minAlpha);
var fadeOutColor=新颜色(原色.r,原色.g,原色.b,0.0f);
闪烁=真;
var passedTime=0f;
while(眨眼)
{
//始终在0和1之间来回循环
lerpFactor=Mathf.PingPong(通过时间,1);
//可选地添加轻松进入和轻松退出
//lerpFactor=Mathf.SmoothStep(0,1,lerpFactor);
//更改minColor和maxColor的顺序
//取决于你想从哪一个开始
text.color=color.Lerp(maxColor、minColor、lerpFactor);
//将上一帧后经过的时间乘以闪烁/秒
passedTime+=Time.deltaTime*闪烁速度;
//允许渲染此帧并继续
//下一帧从这里开始
收益返回空;
}
//将alpha设置为最大值
text.color=新颜色(originalColor.r、originalColor.g、originalColor.b、maxapha);
//开始淡出
passedTime=0f;
while(经过的时间<衰减持续时间)
{
变量lerpFactor=经过的时间/衰减;
//(可选)添加“缓入”和“缓出”
//lerpFactor=Mathf.SmoothStep(0f,1f,lerpFactor);
text.color=color.Lerp(maxColor、fadeOutColor、lerpFactor);
passedTime+=Time.deltaTime;
收益返回空;
}
//只是为了确保将其硬设置为0
text.color=淡出颜色;
//完成后可选择隐藏或销毁此游戏对象
//gameObject.SetActive(false);
//摧毁(游戏对象);
}
公共无效HandleClick()
{
//如果正在闪烁,则停止闪烁并淡出
闪烁=假;
}
timerAlphaZ=0
不会有任何更改(minAlphaZ+=timerAlphaZ*Time.deltaTime;
)