C# 如何按名称或标签查找游戏对象,并将其逐个添加到阵列中?
您好,我正在尝试通过标记从层次结构中查找游戏对象(实例化)。我想将游戏对象逐个添加到数组中C# 如何按名称或标签查找游戏对象,并将其逐个添加到阵列中?,c#,unity3d,C#,Unity3d,您好,我正在尝试通过标记从层次结构中查找游戏对象(实例化)。我想将游戏对象逐个添加到数组中 GameObject []insta1; for (int i = 0; i < count2; i++) { insta1= GameObject.FindGameObjectsWithTag("Vid2"); } GameObject[]insta1; 对于(int i=0;i
GameObject []insta1;
for (int i = 0; i < count2; i++)
{
insta1= GameObject.FindGameObjectsWithTag("Vid2");
}
GameObject[]insta1;
对于(int i=0;i
上面的代码正在工作,但我希望下面给出类似的内容,比如将它们保存在适当位置,然后逐个执行并执行单独的操作
for (int i = 0; i < count2; i++)
{
insta1[i]= GameObject.FindGameObjectsWithTag("Vid2");
}
for(int i=0;i
FindGameObjectsWithTag已返回GameObject[],如中所示:
因此,您可以简单地执行以下操作:
GameObject[] foundObjects = GameObject.FindGameObjectsWithTag("Vid2");
然后在它们上面循环:
foreach (GameObject foundObject in foundObjects) {
//Do Whatever you want with the gameObject
}
foundObjects[index]
或者只需通过以下方式访问它们:
foreach (GameObject foundObject in foundObjects) {
//Do Whatever you want with the gameObject
}
foundObjects[index]
FindGameObjectsWithTag已返回GameObject[],如中所示: 因此,您可以简单地执行以下操作:
GameObject[] foundObjects = GameObject.FindGameObjectsWithTag("Vid2");
然后在它们上面循环:
foreach (GameObject foundObject in foundObjects) {
//Do Whatever you want with the gameObject
}
foundObjects[index]
或者只需通过以下方式访问它们:
foreach (GameObject foundObject in foundObjects) {
//Do Whatever you want with the gameObject
}
foundObjects[index]
就性能而言,您可能希望这样实现它 FindGameObjectsWithTag速度慢取决于场景,因为它需要遍历场景中的游戏对象并检查它们的标记 为此,我们将使用单例模式来确保我们只有一个“MyClass”游戏对象的实例 有关singleton模式的更多信息,您可能还需要阅读以下内容: 这一点更容易理解: 示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyClass : MonoBehaviour
{
// This is basic a singleton implementation
static MyClass sharedInstance = null;
public static MyClass GetInstance()
{
if(sharedInstance == null)
{
// Create a "MyClassSingleton" gameObject if for the first time
GameObject g = new GameObject("MyClassSingleton");
// Attach "MyClass" component and set our instance
sharedInstance = g.AddComponent<MyClass>();
}
return sharedInstance;
}
[SerializeField]
GameObject myGameObjectPrefab;
List<GameObject> myGameObjectList;
void Awake()
{
// Intialize our list
myGameObjectList = new List<GameObject>();
}
public void SpawnGameObject(Vector3 position)
{
// Instantiate the gameObject
GameObject g = GameObject.Instantiate(myGameObjectPrefab);
// Set the position of the gameObject
g.transform.position = position;
// Add the instantiated gameObject to our list
myGameObjectList.Add(g);
}
public List<GameObject> GetGameObjectList()
{
return myGameObjectList;
}
void Update()
{
// For spawning the game objects
if(Input.GetKeyDown(KeyCode.Alpha1))
{
SpawnGameObject(Vector3.zero);
}
if(Input.GetKeyDown(KeyCode.Alpha2))
{
List<GameObject> list = GetGameObjectList();
if(list != null)
{
Debug.Log("COUNT: " + list.Count);
}
}
}
}
如何获取所有游戏对象(来自其他类):
List gameObjectList=MyClass.GetInstance().GetGameObjectList();
注意:您可以从不同的类调用这两个方法(只需确保它们的访问修饰符设置为“public”)。从性能角度看,您可能希望这样实现它 FindGameObjectsWithTag速度慢取决于场景,因为它需要遍历场景中的游戏对象并检查它们的标记 为此,我们将使用单例模式来确保我们只有一个“MyClass”游戏对象的实例 有关singleton模式的更多信息,您可能还需要阅读以下内容: 这一点更容易理解: 示例:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyClass : MonoBehaviour
{
// This is basic a singleton implementation
static MyClass sharedInstance = null;
public static MyClass GetInstance()
{
if(sharedInstance == null)
{
// Create a "MyClassSingleton" gameObject if for the first time
GameObject g = new GameObject("MyClassSingleton");
// Attach "MyClass" component and set our instance
sharedInstance = g.AddComponent<MyClass>();
}
return sharedInstance;
}
[SerializeField]
GameObject myGameObjectPrefab;
List<GameObject> myGameObjectList;
void Awake()
{
// Intialize our list
myGameObjectList = new List<GameObject>();
}
public void SpawnGameObject(Vector3 position)
{
// Instantiate the gameObject
GameObject g = GameObject.Instantiate(myGameObjectPrefab);
// Set the position of the gameObject
g.transform.position = position;
// Add the instantiated gameObject to our list
myGameObjectList.Add(g);
}
public List<GameObject> GetGameObjectList()
{
return myGameObjectList;
}
void Update()
{
// For spawning the game objects
if(Input.GetKeyDown(KeyCode.Alpha1))
{
SpawnGameObject(Vector3.zero);
}
if(Input.GetKeyDown(KeyCode.Alpha2))
{
List<GameObject> list = GetGameObjectList();
if(list != null)
{
Debug.Log("COUNT: " + list.Count);
}
}
}
}
如何获取所有游戏对象(来自其他类):
List gameObjectList=MyClass.GetInstance().GetGameObjectList();
注意:您可以从不同的类调用这两个方法(只需确保它们的访问修饰符设置为“public”)。
FindGameObjectsWithTag
已返回数组,使用它有什么问题?FindGameObjectsWithTag
已返回数组,使用它有什么问题?