跳到主要內容

你可能不知道的超冷門Unity功能#1 WaitForSeconds

遇此行時暫停,直到指定時間後才繼續執行下一行程式碼。
其實我根本沒用過幾次,不知道各位大大都怎麼使用它?

關鍵句 :

yield WaitForSeconds( 5 );

範例 :

using UnityEngine;
using System.Collections;

public class WaitForSecondsExample : MonoBehaviour {
    
    void Start() {
        StartCoroutine(Example());
    }
    
    IEnumerator Example() {
        print(Time.time);
        yield return new WaitForSeconds(5);
        print(Time.time);
    }
}

影片操作 :

留言

  1. 之前有做過一個定期抓廣告頁面的AdPlugin時有用到。
    那時遇到一個問題,unity原本的WaitForSecond好像會因為timescale受影響。
    後來改用這個方式解決:
    http://answers.unity3d.com/questions/301868/yield-waitforseconds-outside-of-timescale.html
    用法跟原本的差不多。

    回覆刪除

張貼留言

這個網誌中的熱門文章

你可能不知道的超冷門Unity功能#2 NavMeshAgent.steeringTarget

在使用NavMeshAgent尋路系統的時候,內建豐富的位移效果常讓人感到頭痛。其實NavMeshAgent有一項鮮少有人知道的功能允許使用者自己取用下一個座標點,省去許多控制複雜參數的麻煩。 關鍵句 : .steeringTarget 範例 : using UnityEngine; using System.Collections; public class PlayCtrl : MonoBehaviour { private NavMeshAgent agent;//尋路系統 public Transform In;//最終目標 private Vector3 Look_pos;//中途目標 void Start () { agent = GetComponent<NavMeshAgent>(); } void Update () { agent.destination = In.position; Look_posa = agent.steeringTarget; } } 影片操作 :

你可能不知道的超冷門Unity功能#3 OnWillRenderObject

OnWillRenderObject這個東西能判定該物件有沒有被攝影機看到,可以拿來判定一個東西是否在視野內。 關鍵句 : void OnWillRenderObject() { } 範例 : using UnityEngine; using System.Collections; public class ExampleClass1 : MonoBehaviour { private ParticleSystem ps; void Start() { ps = GetComponent<ParticleSystem>(); } void OnWillRenderObject() { if (Camera.current.name == "MiniMapcam") ps.enableEmission = true; else ps.enableEmission = false; } } PS : 使用這個範例記得要把攝影機取名為 MiniMapcam 才有效果喔 影片操作 :