如何使用setActiverecursistalife(moment = 1秒)以统一创建一个闪烁的对象。
我的示例(用于更改):
public GameObject flashing_Label;
private float timer;
void Update()
{
while(true)
{
flashing_Label.SetActiveRecursively(true);
timer = Time.deltaTime;
if(timer > 1)
{
flashing_Label.SetActiveRecursively(false);
timer = 0;
}
}
}
使用InvokerePeating:
public GameObject flashing_Label;
public float interval;
void Start()
{
InvokeRepeating("FlashLabel", 0, interval);
}
void FlashLabel()
{
if(flashing_Label.activeSelf)
flashing_Label.SetActive(false);
else
flashing_Label.SetActive(true);
}
看看Unity Waitforseconds函数。
通过传递int param。(秒),您可以切换游戏对象。
bool fadein = true;
IEnumerator Toggler()
{
yield return new WaitForSeconds(1);
fadeIn = !fadeIn;
}
然后通过 StartCoroutine(Toggler())
调用此功能。
您可以使用Coroutines和New Unity 4.6 GUI轻松实现这一目标。在此处查看本文,其中误入文本。您可以轻松地修改游戏对象
眨眼文本-TGC
如果您只需要代码,那么您就可以去
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class FlashingTextScript : MonoBehaviour {
Text flashingText;
void Start(){
//get the Text component
flashingText = GetComponent<Text>();
//Call coroutine BlinkText on Start
StartCoroutine(BlinkText());
}
//function to blink the text
public IEnumerator BlinkText(){
//blink it forever. You can set a terminating condition depending upon your requirement
while(true){
//set the Text's text to blank
flashingText.text= "";
//display blank text for 0.5 seconds
yield return new WaitForSeconds(.5f);
//display “I AM FLASHING TEXT” for the next 0.5 seconds
flashingText.text= "I AM FLASHING TEXT!";
yield return new WaitForSeconds(.5f);
}
}
}
p.s:即使它似乎是一个无限的循环,通常被认为是一种不良的编程实践,在这种情况下,它的效果也很好,因为一旦物体被摧毁,单obehaviour将被销毁。另外,如果您不需要永远闪烁,则可以根据您的要求添加终止条件。
简单的方法是使用InvokerePeating()和cancelInvoke()方法。
- InvokerePeating(" BlinkText",0,0.3)每0.03个时间间隔都会反复调用blinktext()。
- 取消效力(" blinktext")将停止重复调用。
这是一个示例:
//Call this when you want to start blinking
InvokeRepeating("BlinkText", 0 , 0.03f);
void BlinkText() {
if(Title.gameObject.activeSelf)
Title.gameObject.SetActive(false);
else
Title.gameObject.SetActive(true);
}
//Call this when you want to stop blinking
CancelInvoke("BlinkText");
Unity文档