[英]Unity: Instantiate prefabs from array, set a tag to them all, wait 2 seconds and destroy every tagged object
我正在嘗試創建一個腳本(在 javascript 中,但最好使用 C#)來執行以下步驟:
(1) 創建主相機的克隆; (1) 從編輯器中設置的數組實例化預制件; (2) 為所有這些實例化的游戲對象設置一個標簽(相同的); (3)等待1秒; (4) 銷毀每個持有 (2) 中設置的標簽的游戲對象,並銷毀克隆的相機。
我想出了以下代碼,但它不起作用。 任何人都可以幫忙嗎?
代碼更新
using UnityEngine;
using System.Collections;
public class PreLoader : MonoBehaviour {
//Array of Objects To Spawn
public GameObject[] objectsToSpawn;
// Use this for initialization
void Start ()
{
StartCoroutine(SpawnItemsFromArray());
}
IEnumerator SpawnItemsFromArray() //You can name this however you like
{
//Instantiates another camera as a GameObject (easy access to Components and removal of Camera)
GameObject cam1 = Instantiate(GameObject.FindWithTag("MainCamera"), Vector3.zero, Quaternion.FromToRotation(new Vector3(0, 0, 0), new Vector3(0, 0, 1))) as GameObject;
//Gets AudioListener Component and disables it
cam1.GetComponent<AudioListener>().enabled = false;
float space = 1;
float originalSpace = -3;
for (int i = 0; i < objectsToSpawn.Length; i++)
{
//You'll have to Instantiate as a GameObject to change its tag
GameObject go = Instantiate(objectsToSpawn[i], new Vector3(originalSpace, 0, 10.0f), transform.rotation) as GameObject;
originalSpace += space;
//Changes the tag to "Holder"
go.tag = "Holder";
//This will change the tag of the GameObject that this script is attached too
//gameObject.tag = "Holder";
}
//Waits for 2 Sec
yield return new WaitForSeconds(2);
//This will destroy (new Camera) and all that have tag "Holder"
GameObject[] goDestroy = GameObject.FindGameObjectsWithTag("Holder");
foreach (GameObject goHolder in goDestroy) Destroy(goHolder);
Destroy(cam1);
}
}
據我所知yield WaitForSeconds(2);
僅適用於IEnumerator
。 這是用StartCoroutine(**IEnumeratorMethodName**());
調用的StartCoroutine(**IEnumeratorMethodName**());
您要求提供 C# 代碼,所以我希望這會有所幫助:(祝您好運)
void Start ()
{
StartCoroutine(SpawnItemsFromArray());
}
IEnumerator SpawnItemsFromArray() //You can name this however you like
{
//Instantiates another camera as a GameObject (easy access to Components and removal of Camera)
GameObject cam1 = Instantiate(GameObject.FindWithTag("MainCamera"), Vector3.zero, Quaternion.FromToRotation(new Vector3(0, 0, 0), new Vector3(0, 0, 1))) as GameObject;
//Gets AudioListener Component and disables it
cam1.GetComponent<AudioListener>().enabled = false;
//If you added this script to the Main Camera uncomment line below and add the appropriate script name to it
//cam1.GetComponent<**ScriptName**>().enabled = false; //This will make sure the new instantiated Camera doesn't run this script (Otherwise lots of cameras/GameObjects will be spawned
float space = 1;
float originalSpace = -3;
for (int i = 0; i < objectsToSpawn.Length; i++)
{
//You'll have to Instantiate as a GameObject to change its tag
GameObject go = Instantiate(objectsToSpawn[i], new Vector3(originalSpace, 0, 10.0f), transform.rotation) as GameObject;
originalSpace += space;
//Changes the tag to "Holder"
go.tag = "Holder";
//This will change the tag of the GameObject that this script is attached too
//gameObject.tag = "Holder";
}
//Waits for 2 Sec
yield return new WaitForSeconds(2);
//This will destroy (new Camera) and all that have tag "Holder"
GameObject[] goDestroy = GameObject.FindGameObjectsWithTag("Holder");
foreach (GameObject goHolder in goDestroy) Destroy(goHolder);
Destroy(cam1);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.