统一:从数组中实例化预制件,为它们设置一个标签,等待2秒并摧毁每个标记的对象
本文关键字:等待 标签 一个 2秒 对象 摧毁 实例化 数组 设置 统一 | 更新日期: 2023-09-27 17:59:10
我正在尝试创建一个脚本(使用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**());
你要求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);
}