Esempio n. 1
0
        public void FindAllPrefabs(ICollection <PrefabType> prefabs, PrefabInstancePolicy defaultInstancePolicy)
        {
            using (var prefabsAssets = ListPool <GameObject> .Get()) {
                FindAllPrefabs(prefabsAssets);

                foreach (var p in prefabsAssets)
                {
                    // the convention/rule of FindResourcesByComponentType is that the prefab must have a component whose type matches its file name

                    var type = TypeUtils.Find(p.name);
                    if (type == null)
                    {
                        continue;
                    }
                    var c = p.GetComponent(type);
                    if (c == null)
                    {
                        continue;
                    }

                    prefabs.Add(new PrefabType
                    {
                        prefab         = c,
                        prefabType     = type,
                        instancePolicy = defaultInstancePolicy
                    });
                }
            }
        }
        public static void FindPrefabInstances(
            this ManagesPrefabInstances manager,
            ICollection <PrefabInstance> instances,
            PrefabInstancePolicy defaultInstancePolicy,
            bool ensureCreated = false,
            AddInstanceDelegate addInstanceDelegate = null,
            Transform parent = null
            )
        {
            using (var foundItems = ListPool <GameObject> .Get())
                using (var foundObjects = ListPool <GameObject> .Get())
                    using (var prefabTypes = ListPool <PrefabType> .Get())
                    {
                        (parent ?? (manager as Component).transform).GetComponentsInDirectChildren(foundItems, true);

                        manager.GetPrefabTypes(prefabTypes);

                        if (ensureCreated && foundItems.Count == 0)
                        {
                            foreach (var pt in prefabTypes)
                            {
                                if (pt.prefab == null)
                                {
                                    Debug.LogWarning("[" + Time.frameCount + "] encountered null prefab for type " + pt.prefabType);
                                    continue;
                                }

                                var prefab = pt.prefab as GameObject ??
                                             (pt.prefab is Component)?(pt.prefab as Component).gameObject: null;

                                if (prefab == null)
                                {
                                    Debug.LogWarning("[" + Time.frameCount + "] unable to find prefab GameObject");
                                    continue;
                                }
                                foundItems.Add(addInstanceDelegate(prefab));
                            }
                        }

                        foreach (var c in foundItems)
                        {
                            foundObjects.Add(c.gameObject);
                        }

                        manager.ObjectsToPrefabInstances(foundObjects, prefabTypes, instances, defaultInstancePolicy);
                    }
        }
        public static void ObjectsToPrefabInstances(this ManagesPrefabInstances mpi, ICollection <GameObject> objects, IList <PrefabType> prefabTypes, ICollection <PrefabInstance> prefabInstances, PrefabInstancePolicy instancePolicy)
        {
            foreach (var i in objects)
            {
                PrefabType pt;
                MatchInstanceToPrefab(i, prefabTypes, out pt);

                prefabInstances.Add(new PrefabInstance {
                    prefab         = pt.prefab,
                    instance       = i.gameObject,
                    instancePolicy = instancePolicy
                });
            }
        }