2

SetActiveRecursively (モーメント = 1 秒) を使用して、Unity で点滅するオブジェクトを作成するにはどうすればよいですか。

私の例(変更の場合):

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;        
        }   
    }
}
4

4 に答える 4

6

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);
}
于 2014-02-05T13:49:28.083 に答える
1

コルーチンと新しい 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);
}
}
}

PS: これは一般的に悪いプログラミング手法と見なされる無限ループのように見えますが、この場合、オブジェクトが破棄されると MonoBehaviour が破棄されるため、非常にうまく機能します。また、永久にフラッシュする必要がない場合は、要件に基づいて終了条件を追加できます。

于 2015-01-11T07:10:37.947 に答える