我有游戏对象,我需要将它们放入列表中,如下所示:
List<GameObject> unityGameObjects = new List<GameObject>();
我有游戏对象,我需要将它们放入列表中,如下所示:
List<GameObject> unityGameObjects = new List<GameObject>();
您首先创建您的列表。
List<GameObject> unityGameObjects = new List<GameObject>();
您需要引用要添加到列表中的 GameObject。您可以通过查找 GameObject 或创建它的新实例来完成此操作。
GameObject g = GameObject.Find("NameOfGameObject");
unityGameObjects.Add(g);
您不能调用new
GameObjects,因为它们是 MonoBehaviours,但您可以从预制件中实例化它们。
GameObject g = Instantiate(prefab);
unityGameObjects.Add(g);
unityGameObjects.Add(new GameObject());
MyList.AddRange( new List<GameObject> { new GameObject { name = "Frank", tag = "3" ,layer= 2, active = isActiveAndEnabled, hideFlags = HideFlags.HideInHierarchy, isStatic = false },
new GameObject { name = "Jill", tag = "3" },
new GameObject { name = "Dave", tag = "5" },
new GameObject { name = "Jack", tag = "8" },
new GameObject { name = "Judith", tag = "12" },
new GameObject { name = "Robert", tag = "14" },
new GameObject { name = "Adam", tag = "1" } } );
我找到了我的问题的解决方案,因此我会问我的问题,也许它对其他人有帮助:
unityGameObjects.Add(GameObject.Find(unityObject.Value));