![](/img/trans.png)
[英]How do I play an animation for twenty-seven seconds or more and load a scene in unity3d
[英]How do I pause GameObject for a couple of seconds and unpause after a couple of seconds in unity3d
我需要gameobject
在場景中自行暫停7f或8f,並自行在2f處取消暫停。 我擁有的腳本是讓我暫停輸入。 這是我的腳本:
{
sing UnityEngine; using System.Collections; public class star : MonoBehaviour { GameObject[] pauseObjects; void Start () { pauseObjects = GameObject.FindGameObjectsWithTag("Player"); } void pauseGameobject() { if() { start coroutine("wait"); } } public ienumenator wait() { time.timescale = 0; yield return new waitforsceonds(7); time.timesale = 1; } void pauseGameobject() { if() { start coroutine("wait"); } } public ienumenator wait() { time.timescale = 0; yield return new waitforsceonds(7); time.timesale = 1; }
}
暫時停頓一下是什么意思,目前還不清楚,不過我將廣泛回答,以幫助您。
如果您想從外部暫停單個游戲對象,則可以使用以下代碼停用它並相應地激活它: gameObject.SetActive(false);
相反,如果您想在內部暫停游戲對象,則可以設置布爾值,無論是否進行更新測試,它都是正確的:
using UnityEngine;
using System.Collections;
bool update = false
public class ActiveObjects : MonoBehaviour
{
void Start ()
{
//Do stuff
}
void Update ()
{
if(update){
//Do stuff
}
//decide wether or not to pause the game object
}
}
如果要暫停游戲,可以將Time.timeScale
設置為0
,或者只是暫停所有游戲對象。
在這里,您可以找到如何制作計時器,您所要做的就是使用timeLeft -= Time.deltaTime;
倒數一個變量timeLeft -= Time.deltaTime;
。
希望我能幫助你,
亞歷克斯
編輯:好的,這是腳本,請記住,我無法對其進行測試;)
using UnityEngine;
using System.Collections;
public class star : MonoBehaviour {
GameObject[] pauseObjects;
public float timer = 7;
float t = 0;
bool pause = false;
void Start () {
pauseObjects = GameObject.FindGameObjectsWithTag("Player");
t = timer;
}
void Update() {
if(pause){
if(t<0){
t=timer;
pause = false;
time.timescale = 1;
}else{
t -= Time.deltaTime;
time.timescale = 0;
}
}
}
您可以使用協程在update()
循環中插入延遲。 協程使用“屈服”的生成器,而不是“返回”的函數/方法。 這允許的是異步操作的代碼,同時仍以線性方式編寫。
您最可能需要的內置協程是WaitForSeconds 。 要啟動協程,您只需調用StarCoroutine()
並傳入IEnumerator
類型的任何方法即可。 此方法將定期yield
。 在以下示例中, WaitForSeconds(5)
將在5秒后屈服。 也可以使用小數秒,以浮點數表示,例如2.5將是兩秒半。
using UnityEngine;
using System.Collections;
public class WaitForSecondsExample : MonoBehaviour {
void Start() {
StartCoroutine(Example());
}
IEnumerator Example() {
Debug.Log(Time.time); // time before wait
yield return new WaitForSeconds(5);
Debug.Log(Time.time); // time after wait
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.