private void RestoreItems(bool restoreEquipped, ItemCollection itemCollection, InventoryBase inventory, HashSet <ItemType> alreadyAddedItemTypes) { for (int i = 0; i < data.items.Count; i++) { var itemData = data.items[i]; if (itemData.count == 0) { continue; } if (itemData.equipped != restoreEquipped) { continue; } var itemType = UCCUtility.GetItemType(itemCollection, itemData.itemID); inventory.PickupItemType(itemType, itemData.count, itemData.slot, true, restoreEquipped); var item = inventory.GetItem(itemData.slot, itemType); if (item == null) { continue; } for (int j = 0; j < item.ItemActions.Length; j++) { var usableItem = item.ItemActions[j] as UsableItem; if (usableItem == null) { continue; } for (int k = 0; k < itemData.itemActionData.Count; ++k) { if (usableItem.ID != itemData.itemActionData[k].id) { continue; } #if ULTIMATE_CHARACTER_CONTROLLER_SHOOTER var shootableWeapon = usableItem as ShootableWeapon; if (shootableWeapon != null) { // Temporarily fill clip so inventory.PickupItemType doesn't auto-reload: usableItem.SetConsumableItemTypeCount(shootableWeapon.ClipSize); } #endif if (debug) { Debug.Log("UCC Saver on " + name + " restoring item: " + usableItem.name + " (" + itemData.itemActionData[k].consumableCount + "/" + itemData.itemActionData[k].count + ")", this); } var consumableItemType = usableItem.GetConsumableItemType(); if (itemData.itemActionData[k].count > 0 && !alreadyAddedItemTypes.Contains(consumableItemType)) { inventory.PickupItemType(consumableItemType, itemData.itemActionData[k].count, -1, true, false); alreadyAddedItemTypes.Add(consumableItemType); } usableItem.SetConsumableItemTypeCount(itemData.itemActionData[k].consumableCount); } } } }
public override void ApplyData(string s) { if (string.IsNullOrEmpty(s)) { return; } var newData = SaveSystem.Deserialize <Data>(s); if (newData == null) { if (Debug.isDebugBuild) { Debug.LogWarning("UCC Saver on " + name + " received invalid data. Can't apply: " + s, this); } return; } data = newData; var character = GetComponent <UltimateCharacterLocomotion>(); // Restore attributes: if (saveAttributes) { var attributeManager = GetComponent <AttributeManager>(); if (attributeManager == null) { if (Debug.isDebugBuild) { Debug.LogWarning("UCC Saver can't load attributes. " + name + " doesn't have an Attribute Manager.", this); } } else { if (debug) { Debug.Log("UCC Saver on " + name + " restoring attributes", this); } var count = Mathf.Min(attributeManager.Attributes.Length, data.attributes.Count); for (int i = 0; i < count; i++) { attributeManager.Attributes[i].Value = data.attributes[i]; } } } // Restore inventory: if (saveInventory) { var inventory = GetComponent <InventoryBase>(); if (inventory == null) { if (Debug.isDebugBuild) { Debug.LogWarning("UCC Saver can't load inventory. " + name + " doesn't have an Inventory component.", this); } } else { // Clear inventory: var items = inventory.GetAllItems(); for (int i = 0; i < items.Count; i++) { var item = items[i]; if (item != null && item.ItemType != null) { for (int j = 0; j < item.ItemActions.Length; j++) { var usableItem = item.ItemActions[j] as UsableItem; if (usableItem != null) { var consumableItemType = usableItem.GetConsumableItemType(); if (consumableItemType != null) { var count = (int)inventory.GetItemTypeCount(consumableItemType); if (count > 0) { inventory.RemoveItem(consumableItemType, count, false); } } } } inventory.RemoveItem(item.ItemType, item.SlotID, false); } } var itemCollection = UCCUtility.FindItemCollection(character.gameObject); if (itemCollection == null) { Debug.LogError("Error: Unable to find ItemCollection."); return; } // Add saved items: (restore equipped items last so they end up equipped) HashSet <ItemType> alreadyAddedItemTypes = new HashSet <ItemType>(); RestoreItems(false, itemCollection, inventory, alreadyAddedItemTypes); RestoreItems(true, itemCollection, inventory, alreadyAddedItemTypes); } } // Restore position: if (savePosition) { if (CompareTag("Player") && SaveSystem.playerSpawnpoint != null) { if (debug) { Debug.Log("UCC Saver on " + name + " moving character to spawnpoint " + SaveSystem.playerSpawnpoint, this); } character.SetPositionAndRotation(SaveSystem.playerSpawnpoint.transform.position, SaveSystem.playerSpawnpoint.transform.rotation); } else { var currentScene = SceneManager.GetActiveScene().buildIndex; for (int i = 0; i < data.positions.Count; i++) { if (data.positions[i].scene == currentScene) { if (debug) { Debug.Log("UCC Saver on " + name + " (tag=" + tag + ") restoring saved position " + data.positions[i].position, this); } character.SetPositionAndRotation(data.positions[i].position, data.positions[i].rotation); break; } } } } // Restore perspective: if (savePerspective) { var camera = UnityEngineUtility.FindCamera(null); var cameraController = camera.GetComponent <Opsive.UltimateCharacterController.Camera.CameraController>(); if (cameraController != null && cameraController.CanChangePerspectives) { cameraController.SetPerspective(data.firstPerson, true); } } // Hide cursor: (In editor, UnityInput makes cursor visible when paused during scene transition.) var unityInput = GetComponent <UnityInput>(); if (unityInput != null) { unityInput.DisableCursor = true; } }