如何通过名称或标记查找游戏对象并将它们逐个添加到数组中?

时间:2018-10-24 13:17:04

标签: c# unity3d

嗨,我想通过标记从层次结构中找到游戏对象(实例化)。我想将游戏对象一一添加到数组中。

GameObject []insta1;

 for (int i = 0; i < count2; i++)
        {
            insta1= GameObject.FindGameObjectsWithTag("Vid2");


        }

上面的代码正在工作,但是我想要下面给出的类似内容,例如将它们保存在适当位置,然后将它们一个接一个并执行单独的操作。

for (int i = 0; i < count2; i++)
        {
            insta1[i]= GameObject.FindGameObjectsWithTag("Vid2");


        }

2 个答案:

答案 0 :(得分:2)

FindGameObjectsWithTag已经返回了GameObject [],如https://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html

所以您可以简单地做:

GameObject[] foundObjects = GameObject.FindGameObjectsWithTag("Vid2");

然后在它们上循环:

foreach (GameObject foundObject in foundObjects) {
  //Do Whatever you want with the gameObject
}

或通过以下方式访问它们:

foundObjects[index]

答案 1 :(得分:2)

明智地使用性能,您可能希望改为这样实现。

FindGameObjectsWithTag 的速度取决于您的场景,因为它需要浏览场景中存在的gameObject并检查其标记。

为此,我们将使用 Singleton模式,以确保我们只有1个“ MyClass” gameObject实例。

有关单例模式的更多信息,您可能还需要阅读以下内容: http://csharpindepth.com/Articles/General/Singleton.aspx

这个更容易理解: https://codeburst.io/singleton-design-pattern-implementation-in-c-62a8daf3d115

示例:

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);
            }
        }
    }
}

如何生成(从其他类中):

MyClass.GetInstance().SpawnGameObject(Vector3.zero);

如何(从其他类中)获取所有gameObjects:

List<GameObject> gameObjectList = MyClass.GetInstance().GetGameObjectList();

注意:您可以从不同的类调用这两个方法(只需确保将其访问修饰符设置为“ public”)即可。