unity實(shí)現(xiàn)物體延時(shí)出現(xiàn)
本文實(shí)例為大家分享了unity實(shí)現(xiàn)物體延時(shí)出現(xiàn)的具體代碼,供大家參考,具體內(nèi)容如下
新建一個(gè)cube和plane,隱藏cube,腳本掛在plane上。
1. update計(jì)時(shí)器實(shí)現(xiàn)
using System.Collections; using System.Collections.Generic; using UnityEngine; //一個(gè)隱藏的物體等待t秒后顯示,updata計(jì)時(shí)器實(shí)現(xiàn) public class activeShow : MonoBehaviour { public GameObject cube; public int t; private float m_timer=0; // Use this for initialization void Start () { } // Update is called once per frame void Update () { m_timer+=Time.deltaTime; if(m_timer>5){ cube.SetActive(true); m_timer=0; } } }
2. invoke實(shí)現(xiàn)
using System.Collections; using System.Collections.Generic; using UnityEngine; 一個(gè)隱藏的物體等待t秒后顯示,Invoke實(shí)現(xiàn) public class ShowT : MonoBehaviour { public GameObject cube; public int t;//等待時(shí)間 // Use this for initialization void Start () { Invoke("ActiveShow", t); } // Update is called once per frame void Update () { } public void ActiveShow(){ cube.SetActive(true); } }
3. invokeRepeating實(shí)現(xiàn)(這個(gè)是用來湊數(shù)的)
void Start () { InvokeRepeating("ActiveShow", t,1000); }
4. 協(xié)程實(shí)現(xiàn)
using System.Collections; using System.Collections.Generic; using UnityEngine; //一個(gè)隱藏的物體等待t秒后顯示,協(xié)程實(shí)現(xiàn) public class HideInSeconds : MonoBehaviour { public GameObject cube; IEnumerator ie; // Use this for initialization void Start () { ie=waitFourSeconds(); StartCoroutine(ie); } // Update is called once per frame void Update () { } IEnumerator waitFourSeconds(){ yield return new WaitForSeconds(4.0f); cube.SetActive(true); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
LINQ基礎(chǔ)之Intersect、Except和Distinct子句
這篇文章介紹了LINQ使用Intersect、Except和Distinct子句的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04Microsoft Expression Web 簡體中文正式版 官方下載地址
Microsoft Expression Web 簡體中文正式版 官方下載地址...2007-07-07c#學(xué)習(xí)教程之JSON文件及解析實(shí)例
json作為互聯(lián)網(wǎng)上輕量便捷的數(shù)據(jù)傳輸格式,越來越受到重視,下面這篇文章主要給大家介紹了關(guān)于c#學(xué)習(xí)教程之JSON文件及解析的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08C#的靜態(tài)工廠方法與構(gòu)造函數(shù)相比有哪些優(yōu)缺點(diǎn)
這篇文章主要介紹了C#的靜態(tài)工廠方法與構(gòu)造函數(shù)對比的優(yōu)缺點(diǎn),文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07C#中事務(wù)處理和非事務(wù)處理方法實(shí)例分析
這篇文章主要介紹了C#中事務(wù)處理和非事務(wù)處理方法,較為詳細(xì)的分析了C#中事務(wù)處理與非事務(wù)處理的使用技巧,對于使用C#進(jìn)行數(shù)據(jù)庫程序開發(fā)有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07將excel數(shù)據(jù)轉(zhuǎn)換成dataset示例
這篇文章主要介紹了不借助第三方插件的情況下將Excel中的數(shù)據(jù)轉(zhuǎn)換成DataSet的方法,需要的朋友可以參考下2014-02-02