我希望我的问题不重复; 我有一个团结的问题。 我写了一个透明度(淡入/淡出循环)对象的类。这是正常的。 我在场景中放了两个按钮。他们在画布的地方。 运行之后,即使我在画布中写了一个透明度的命令,其中的所有内容都转到透明度(淡入和淡出循环)。 我不知道。 请帮忙。
myclass
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading;
using System;
public class Transparent : MonoBehaviour {
private float duration = .7f;
public float waitTime;
IEnumerator co2;
// Update is called once per frame void
public void start_tranparecncy()
{
this.co2=this.blink();
this.StartCoroutine (this.co2);
}
IEnumerator blink() {
//Color textureColor = this.transform.GetComponent<SpriteRenderer> ().material.color;
Color textureColor = this.transform.GetComponent<Image>().material.color;
//textureColor.a = Mathf.PingPong(Time.time, duration) / duration;
//this.GetComponent<SpriteRenderer>().material.color = textureColor;
while (true) { // this could also be a condition indicating "alive or dead"
// we scale all axis, so they will have the same value,
// so we can work with a float instead of comparing vectors
textureColor.a=Mathf.PingPong (Time.time, duration) / duration;
this.transform.transform.GetComponent<Image> ().material.color = textureColor;
// reset the timer
yield return new WaitForSeconds (waitTime);
}
//end of if(this.transform.childCount =0)
}
void stop_Transparency ()
{
this.StopCoroutine (this.co2);
}
}
答案 0 :(得分:0)
好的,我发现你的问题主要是
this.transform.transform.GetComponent<Image>();
由于双变换,它到达父级的一个上层,这是你的画布本身。因此,画布中的所有内容都会失去alpha值。
还编辑了一下你的代码。而不是使用材质颜色,直接改变了图像组件的alpha颜色值。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Threading;
using System;
public class Transparent : MonoBehaviour
{
private float duration = .7f;
public float waitTime;
IEnumerator co2;
public void start_tranparecncy()
{
this.co2 = this.blink();
this.StartCoroutine(this.co2);
}
IEnumerator blink()
{
//Color textureColor = this.transform.GetComponent<SpriteRenderer> ().material.color;
Color textureColor = this.transform.GetComponent<Image>().color;
//textureColor.a = Mathf.PingPong(Time.time, duration) / duration;
//this.GetComponent<SpriteRenderer>().material.color = textureColor;
while (true)
{ // this could also be a condition indicating "alive or dead"
// we scale all axis, so they will have the same value,
// so we can work with a float instead of comparing vectors
textureColor.a = Mathf.PingPong(Time.time, duration) / duration;
this.transform.GetComponent<Image>().color = textureColor;
// reset the timer
yield return new WaitForSeconds(waitTime);
}
//end of if(this.transform.childCount =0)
}
void stop_Transparency()
{
this.StopCoroutine(this.co2);
}
}
我已将此脚本附加到画布中的按钮。同时在同一个按钮start_tranparecncy()
中运行On Click()
功能。
按下按钮后,只有按钮的alpha值正在改变。
如果您愿意,可以更改On Click()
并添加其他按钮。