public static GameObject SpawnFromPool(PrefabTag Tag, Vector3 Position, Quaternion Rotation) { if (!Instance.poolDictionary.ContainsKey(Tag)) { Debug.LogWarning("Pool with name: \"" + Tag + "\" doesn't exist."); return(null); } var objectToSpawn = Instance.poolDictionary[Tag].Dequeue(); objectToSpawn.SetActive(true); objectToSpawn.transform.position = Position; objectToSpawn.transform.rotation = Rotation; // Call object on spawn method var objectPools = objectToSpawn.GetComponents <IPooledObjects>(); objectPools?.ForEach(Object => Object.OnObjectSpawn()); Instance.poolDictionary[Tag].Enqueue(objectToSpawn); return(objectToSpawn); }
private void AddPrefab(PrefabTag tag, Vector2? screenPosition = null) { if (tag == null) { return; } MyMwcObjectBuilder_PrefabBase prefabBuilder = null; MyPrefabContainer container = MyEditor.Static.GetEditedPrefabContainer(); if (MyFakes.ENABLE_OBJECT_COUNTS_LIMITS) { //here test if we can even add entity if (MyEntities.GetObjectsCount() >= MyEditorConstants.MAX_EDITOR_ENTITIES_LIMIT) { MyAudio.AddCue2D(MySoundCuesEnum.GuiEditorObjectMoveInvalid); MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, MyTextsWrapperEnum.MaxLargeShipsCountReached, MyTextsWrapperEnum.MessageBoxCaptionError, MyTextsWrapperEnum.Ok, null)); return; } } prefabBuilder = MyPrefabFactory.GetInstance().CreatePrefabObjectBuilder(tag.PrefabModule, tag.PrefabId, tag.FactionAppearance); if(container != null && prefabBuilder is MyMwcObjectBuilder_PrefabFoundationFactory && container.ContainsPrefab(PrefabTypesFlagEnum.FoundationFactory)) { // prefab container can contains only one foundation factory MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, MyTextsWrapperEnum.PrefabContainerAlreadyContainsFoundationFactory, MyTextsWrapperEnum.YouCantAddFoundationFactory, MyTextsWrapperEnum.Ok, null)); return; } if (container == null) { List<MyMwcObjectBuilder_PrefabBase> prefabBuilders = new List<MyMwcObjectBuilder_PrefabBase>(); prefabBuilders.Add(prefabBuilder); MyMwcObjectBuilder_PrefabContainer prefabContainerBuilder = new MyMwcObjectBuilder_PrefabContainer( null, MyMwcObjectBuilder_PrefabContainer_TypesEnum.INSTANCE, prefabBuilders, MyClientServer.LoggedPlayer.GetUserId(), MyMwcObjectBuilder_FactionEnum.Euroamerican, new MyMwcObjectBuilder_Inventory(new List<MyMwcObjectBuilder_InventoryItem>(), 1000)); MyEditor.Static.CreateFromObjectBuilder(prefabContainerBuilder, Matrix.Identity, screenPosition); } else { MyEditor.Static.CreateFromObjectBuilder(prefabBuilder, Matrix.Identity, screenPosition); } }