简体   繁体   English

Unity2D:禁用订单中的实例化预制件

[英]Unity2D: Disable instantiated prefabs in an order

I created a script that instantiate a variety of different prefabs (also can instantiate more than one of the same prefab) that becomes a child of a parent (gameobject) I called slot. 我创建了一个脚本,该脚本可实例化各种不同的预制件(也可以实例化同一预制件中的多个预制件),从而成为我称为插槽的父代(游戏对象)的子代。 Once spawn each prefab has a set transformation position to a certain area, so say if I wanted my script to spawn in 3 of the same prefab (say prefab1) in to my scene, all three prefab1 would have the same transform position (it would be layered on top of each other) and also would be a child to the slot. 生成每个预制件后,都会在某个区域设置一个固定的变换位置,因此,如果我要让脚本在场景中的同一预制件(例如prefab1)的3个中生成,则所有三个prefab1都将具有相同的变换位置( (彼此叠放),并且也是该广告位的孩子。 But say if I spawn in 4 of prefab2 then prefab2 would have a different start position compared to prefab1, but is set under the same parent as prefab1...hope I'm making sense! 但是说,如果我在prefab2的4个中生成,则prefab2与prefab1会有不同的开始位置,但是被设置为与prefab1相同的父对象...希望我很有意义! What I want to do: is there a way to setactive false all instantiated prefabs but leave one of all the different prefabs that was spawn, for instance: 我想做的是:有没有一种方法可以对所有实例化的预制件都设置为false,但是保留所有生成的预制件中的一个,例如:

在此处输入图片说明

I want to setactive false all of the prefab1 and prefab2 within this black box I drew above the image and leave the one prefab1 & prefab 2 setactive to true, if the prefab1/prefab2 is deleted by collision then I would like the next prefab1/prefab2 to be setactive to true and so on. 我想将我在图像上方绘制的黑匣子内的所有prefab1和prefab2设置为false,如果将prefab1 / prefab2设置为true,则我希望将下一个prefab1 / prefab2设置为true。设置为true等。 Again I hope I'm making sense... this is my code for spawning in my prefabs: 再一次,我希望我有道理...这是我在预制件中生成的代码:

 void Update () {
   for (int x = 0; x < slotController; x++) {
            var item = Instantiate (ItemPrefab) as GameObject;
            itemPrefab.transform.position = transform.position;
            itemPrefab.transform.SetParent (slot.transform);
            itemPrefab.SetActive (true);
            if (slot.childCount < 0) {
                slotHolder.SetActive (false);
            }
        }
    }

You can use collections and LINQ to do things like this. 您可以使用集合和LINQ来做这样的事情。 Here is an example. 这是一个例子。 The "Thing" class would be your prefabs. “事物”类将是您的预制件。

You basically have a List collection for each type of prefab you have, and a dictionary that stores all those lists, and has a key to access them with. 基本上,您拥有每种预制件的List集合,还有一个存储所有这些列表的字典,并具有用于访问它们的密钥。

(By the way, the Update method means your code will happen every frame so that may not be a good place for it) (顺便说一下,Update方法意味着您的代码将在每一帧发生,因此可能不是一个好地方)

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    public static void Main()
    {
        Dictionary<int, List<Thing>> prefabs = new Dictionary<int, List<Thing>>();

        // add a few items to the lists
        prefabs.Add(1, new List<Thing>(){ new Thing("Thing1"), new Thing("Thing1") });
        prefabs.Add(2, new List<Thing>(){ new Thing("Thing2"), new Thing("Thing2"), new Thing("Thing2") });

        var keys = prefabs.Keys;
        // set everything inactive except the first one
        foreach(int i in keys){
            prefabs[i].Skip(1).ToList().ForEach(x => x.active = false);
        }

        // print out the active state
        foreach(int i in keys){
            foreach(Thing t in prefabs[i]){
                Console.WriteLine("prefab " + i + "  active = " + t.active);
            }
        }
    }

    class Thing {
        public bool active = true;
        public String name = "";

        public Thing(String name){
        this.name = name;
        }
    }
}

Result: 结果:

prefab 1  active = True
prefab 1  active = False
prefab 2  active = True
prefab 2  active = False
prefab 2  active = False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM